views:

1121

answers:

1

I'm trying CTest in CMake in order to automatically run some of my tests using make test target. The problem is CMake does not "understand" that the test I'm willing to run has to be built since it is part of the project.

So I'm looking for a way to explicitly specify this dependency.

+8  A: 

It's a bug in CMake that this doesn't work out of the box. A workaround is to do the following:

add_test(TestName ExeName)
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND}
                  DEPENDS ExeName)

Then you can run make check and it will compile and run the test. If you have several tests, then you would have to use DEPENDS exe1 exe2 exe3 ... in the above line.

rq
perfect answer, thank you :)
claferri
so I guess that the "make test" target will remain unused as it seems you have to choose a different target name ine the add_custom_target command?
claferri
Yep. The only difference between "make test" and "make check" is the former shows "Running tests..." first and doesn't check any build dependencies.
rq
@rq - but how can I do this with multiple projects (when one CMakeLists.txt is sub-project of other) so each one would define `check` target and they may collide
Artyom
@Artyom - in that case you're probably better of just using the equivalent "make all test". In fact, this is what I do anyway.
rq