I am trying to structure my project to include the production sources (in src subfolder) and tests (in test subfolder). I am using CMake to build this. As a minimal example I have the following files:
CMakeLists.txt:
cmake_minimum_required (VERSION 2.8) 
project (TEST) 
add_subdirectory (src) 
add_subdirectory (test) 
src/CMakeLists.txt:
add_executable (demo main.cpp sqr.cpp) 
src/sqr.h
#ifndef SQR_H
#define SQR_H
double sqr(double);    
#endif // SQR_H
src/sqr.cpp
#include "sqr.h"
double sqr(double x) { return x*x; }
src/main.cpp - uses sqr, doesn't really matter
test/CMakeLists.txt:
find_package(Boost COMPONENTS system filesystem unit_test_framework REQUIRED)
include_directories (${TEST_SOURCE_DIR}/src) 
ADD_DEFINITIONS(-DBOOST_TEST_DYN_LINK) 
add_executable (test test.cpp ${TEST_SOURCE_DIR}/src/sqr.cpp) 
target_link_libraries(test
                      ${Boost_FILESYSTEM_LIBRARY}
                      ${Boost_SYSTEM_LIBRARY}
                      ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
                      )
enable_testing()
add_test(MyTest test)
test/test.cpp:
#define BOOST_TEST_MODULE SqrTests
#include <boost/test/unit_test.hpp>
#include "sqr.h"
BOOST_AUTO_TEST_CASE(FailTest)
{
    BOOST_CHECK_EQUAL(5, sqr(2));
}
BOOST_AUTO_TEST_CASE(PassTest)
{
    BOOST_CHECK_EQUAL(4, sqr(2));
}
A few questions:
- Does this structure make sense? What are the best practises when structuring this code? (I am coming from C# and java, and there it is easier in a sense)
- I don't like the fact that I have to list all the files from the srcfolder in thetest/CMakeLists.txtfile. If this was a library project, I would just link the library. Is there a way to avoid listing all the cpp files from the other project?
- What are the lines enable_testing()andadd_test(MyTest test)doing? I haven't seen any effect. How can I run the tests from CMake (or CTest)?
- So far I just ran cmake .in the root folder, but this created a mess with temporary files everywhere. How can I get the compilation results in a reasonable structure?
 
     
     
     
    