From KitwarePublic
Jump to: navigation, search

How to emulate GNU Autotools 'make check'

Those of you familiar with the GNU Autotools probably know that the all target in a Makefile generated by autoconf/automake does not cause a (re)build of your test programs (assuming you've listed them as check_PROGRAMS). This is IMHO a very nice feature, because the normal build is not bogged down by the compilation of many test programs, or worse, comes to a grinding halt due to a compilation error in one of the test programs you didn't have time to fix yet. When the time has come to run your test suite, you simply type make check and your test programs will be (re)build and run.

This feature can be emulated in CMake.

Define a custom target

First you need to define a custom target check. You only need to do this once, so doing this in your toplevel CMakeLists.txt file is probably a good idea.

add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND})

eg, you could add set(CMAKE_CTEST_COMMAND ctest -V) to see the printouts from the tests.

Add a test program

To add a test program <testprog>, use the following commands

add_executable(<testprog> EXCLUDE_FROM_ALL ...)
add_test(<testprog> <testprog>)
add_dependencies(check <testprog>)

The option EXCLUDE_FROM_ALL is essential here; it causes <testprog> to be ignored when the all target is built.


If you organize all your test programs in one directory, you can use the EXCLUDE_FROM_ALL option with the add_subdirectory command. That way, you don't need to specify this option with every add_executable command.