[CMake] CMake unit tests and the enable_testing() macro

Nils Gladitz nilsgladitz at gmail.com
Fri Oct 24 14:39:31 EDT 2014


On 24.10.2014 20:28, Zé wrote:
> I'm test-driving CMake for a new project I've just started and I'm 
> currently trying to set up a unit test framework.  The project is a 
> dynamic library written in C++, the unit tests are based on Boost 
> test, and the project tree is organized essentially as follows:
>
> .
> ├── CMakeLists.txt
> ├── mylibrary
> │   ├── CMakeLists.txt
> │   └── sample.c++
> └── unit_tests
>     ├── CMakeLists.txt
>     └── test_sample.c++
>
>
>
> I believe that I've managed to get things up and running, but in the 
> process I've stumbled on an issue.
>
> Apparently, if I call the enable_testing() macro within 
> ${Project}/unit_tests/CMakeLists.txt, calling make test does nothing, 
> and the following error message is shown:
>
>     make: *** No rule to make target `test'.  Stop.
>
>
> Yet, if enable_testing() is called from ${Project}/CMakeLists.txt, 
> calling make test runs as expected.
>
> Am I missing something?

"Enable testing for current directory and below."

For the Makefile generator this means that test targets are created for 
the build directory that corresponds to the current source directory and 
directories below.

If your only enable_testing() call is in <SOURCE_DIR>/unit_tests you 
should be able to run "make test" in the corresponding 
<BUILD_DIR>/unit_tests directory but not from <BUILD_DIR>.

In theory you can use this to run tests selectively depending on which 
directory they are in.

Nils


More information about the CMake mailing list