Two sets of tests in Cmake

2019-06-01 05:32发布

问题:

I have two sets of tests (functional and unit tests) and I want to be able to specify which set to run through cmake.

One set of tests are my unittests that I want to run by doing "make test".

Another set of tests are my functional tests that I would like to run by doing "make functionaltests".

Currently both are part of ctest, in that I run both suites through add_test. My CMakeLists.txt file is like this:

 FOREACH(functional_test ${functional_tests})
     ADD_TEST(NAME functional_test COMMAND f_test.sh functional_test)
 ENDFOREACH(functional_test)

 FOREACH(unit_test ${unit_tests})
     ADD_TEST(NAME unit_test COMMAND u_test.sh unit_test)
 ENDFOREACH(unit_test)

I want to leverage ctest for both suites because it gives me a nice, readable format for the test suite (which tests passed and which failed).

I would prefer to not have to create a custom executable, create a target called functionaltests for it, and try to mimic how ctest prints out test results.

回答1:

When you run ctest you can give it a regular expression to choose what tests to run. So you can name the tests in your CMakeLists file according to a pattern which supports this:

 set(functional_tests "test1" "test2" "test3")
 set(unit_tests "test1" "test2" "test3")

 FOREACH(test ${functional_tests})
     ADD_TEST(NAME functional_${test} COMMAND f_test.sh ${test})
 ENDFOREACH()

 FOREACH(test ${unit_tests})
     ADD_TEST(NAME unit_${test} COMMAND u_test.sh ${test})
 ENDFOREACH()

Now you can run the functional tests:

ctest -R functional_

Or the unit tests:

ctest -R unit_

If you want to create targets so that you can execute these through make, you can do:

add_custom_target(unit_tests COMMAND ${CMAKE_CTEST_COMMAND} -R unit_)
add_custom_target(functional_tests COMMAND ${CMAKE_CTEST_COMMAND} -R functional_)

Then you can run:

make unit_tests
make functional_tests

You may want to add dependencies to the custom commands so that they cause whatever executable you're testing to rebuild if necessary.