title: CMake: Project structure with unit tests
body: <p>I am trying to structure my project to include the production sources (in <code>src</code> subfolder) and tests (in <code>test</code> subfolder). I am using CMake to build this. As a minimal example I have the following files:</p>
  
  <p>CMakeLists.txt:</p>
  
  <pre><code>cmake_minimum_required (VERSION 2.8) 
  project (TEST) 
  
  add_subdirectory (src) 
  add_subdirectory (test) 
  </code></pre>
  
  <p>src/CMakeLists.txt:</p>
  
  <pre><code>add_executable (demo main.cpp sqr.cpp) 
  </code></pre>
  
  <p>src/sqr.h</p>
  
  <pre><code>#ifndef SQR_H
  #define SQR_H
  double sqr(double);    
  #endif // SQR_H
  </code></pre>
  
  <p>src/sqr.cpp</p>
  
  <pre><code>#include "sqr.h"
  double sqr(double x) { return x*x; }
  </code></pre>
  
  <p>src/main.cpp  - uses sqr, doesn't really matter</p>
  
  <p>test/CMakeLists.txt:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>test/test.cpp:</p>
  
  <pre><code>#define BOOST_TEST_MODULE SqrTests
  #include &lt;boost/test/unit_test.hpp&gt;
  
  #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));
  }
  </code></pre>
  
  <p>A few questions:</p>
  
  <ol>
  <li>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)</li>
  <li>I don't like the fact that I have to list all the files from the <code>src</code> folder in the <code>test/CMakeLists.txt</code> file. 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?</li>
  <li>What are the lines <code>enable_testing()</code> and <code>add_test(MyTest test)</code> doing? I haven't seen any effect. How can I run the tests from CMake (or CTest)?</li>
  <li>So far I just ran <code>cmake .</code> in the root folder, but this created a mess with temporary files everywhere. How can I get the compilation results in a reasonable structure?</li>
  </ol>