Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake: Project structure with unit tests

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:

  1. 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)
  2. I don't like the fact that I have to list all the files from the src folder in the test/CMakeLists.txt 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?
  3. What are the lines enable_testing() and add_test(MyTest test) doing? I haven't seen any effect. How can I run the tests from CMake (or CTest)?
  4. 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?
like image 930
Grzenio Avatar asked Jan 21 '13 20:01

Grzenio


People also ask

Does CMake facilitate unit tests?

¶ CMake facilitates testing your software through special testing commands and the CTest executable. First, we will discuss the key testing commands in CMake. To add testing to a CMake-based project, simply include(CTest) and use the add_test command.

What does add_test do CMake?

Options To add_test(...) COMMAND specifies the command to run for the test. Essentially, this can be any command that would normally run in your terminal. You can also pass the name of an executable target, and CMake will replace it with the full location of the executable.

What does Add_subdirectory do in CMake?

Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.


1 Answers

For questions 1 & 2, I would recommend making a library from your non-test files excluding main.cpp (in this case just src/sqr.cpp and src/sqr.h), and then you can avoid listing (and more importantly re-compiling) all the sources twice.

For question 3, these commands add a test called "MyTest" which invokes your executable "test" without any arguments. However, since you've added these commands to test/CMakeLists.txt and not your top-level CMakeLists.txt, you can only invoke the test from within the "test" subdirectory of your build tree (try cd test && ctest -N). If you want the test to be runnable from your top-level build directory, you'd need to call add_test from the top-level CMakeLists.txt. This also means you have to use the more verbose form of add_test since your test exe isn't defined in the same CMakeLists.txt

In your case, since you're running cmake in the root folder, your build tree and your source tree are one and the same. This is known as an in-source build and isn't ideal, which leads to question 4.

The preferred method for generating the build tree is to do an out-of-source build, i.e. create a directory somewhere outside of your source tree and execute cmake from there. Even creating a "build" directory in the root of your project and executing cmake .. would provide a clean structure which won't interfere with your source tree.

One final point is to avoid calling executables "test" (case-sensitive). For reasons why, see this answer.

To achieve these changes, I'd do the following:

CMakeLists.txt:

cmake_minimum_required (VERSION 2.8) project (TEST) add_subdirectory (src)  add_subdirectory (test) enable_testing () add_test (NAME MyTest COMMAND Test) 


src/CMakeLists.txt:

add_library (Sqr sqr.cpp sqr.h) add_executable (demo main.cpp) target_link_libraries (demo Sqr) 


test/CMakeLists.txt:

find_package (Boost COMPONENTS system filesystem unit_test_framework REQUIRED) include_directories (${TEST_SOURCE_DIR}/src                      ${Boost_INCLUDE_DIRS}                      ) add_definitions (-DBOOST_TEST_DYN_LINK) add_executable (Test test.cpp) target_link_libraries (Test                        Sqr                        ${Boost_FILESYSTEM_LIBRARY}                        ${Boost_SYSTEM_LIBRARY}                        ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}                        ) 
like image 62
Fraser Avatar answered Sep 22 '22 13:09

Fraser