Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force serial execution for specific targets in CMake

In my CMake project I have several targets which simply run a certain set of unit tests (for example, runTestsForA, runTestsForB and runTestsForC). I also have a target, tests, that depends on all of these unit test targets, so I can run them with a single command.

I'm using CLion is my IDE, which tries to use parallel make builds by default (which I want and am also doing on the Continuous Integration server). However, it looks like the tests are running in parallel too now and some tests are not made for this (they use a local loopback to do some magic with sockets), which causes them to fail.. sometimes.

That is why I would like to force serial execution for some/all of the dependencies of my tests target. Unfortunately the CMake documentation did not help me, when I was searching information on how to do this. Which brings me to my questions: is this at all possible and how can it be done if it is?

like image 571
Arno Moonen Avatar asked May 01 '15 07:05

Arno Moonen


People also ask

What is CTest command?

The "ctest" executable is the CMake test driver program. CMake-generated build trees created for projects that use the ENABLE_TESTING and ADD_TEST commands have testing support. This program will run the tests and report results.

How does CTest work?

The CTest command supports a group of command line options that allow it to be used as the test executable to run. When used as the test executable, CTest can run CMake, run the compile step, and finally run a compiled test. We will now look at the command line options to CTest that support building and running tests.

Does CTest run tests in parallel?

If no interruption occurred, the -F option will have no effect. Run the tests in parallel using the given number of jobs. This option tells CTest to run the tests in parallel using given number of jobs. This option can also be set by setting the CTEST_PARALLEL_LEVEL environment variable.

What does CMake add_test do?

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.


1 Answers

Instead of manual tests target declaration you can use CTest tool. Use add_test command to create test targets, then CMake will automatically create tests target that will run all tests:

enable_testing()
add_test(NAME TestsForA COMMAND <command>)
add_test(NAME TestsForB COMMAND <command>)
set_tests_properties(TestsForA TestsForB  PROPERTIES RUN_SERIAL TRUE)

After that you can run make tests or ctest -j8 . in your build tree. The tests will be serialized.

More information can be found at:

  • http://www.cmake.org/cmake/help/v3.2/command/add_test.html
  • http://www.cmake.org/cmake/help/v3.2/command/enable_testing.html
  • http://www.cmake.org/cmake/help/v3.2/command/set_tests_properties.html
  • http://www.cmake.org/cmake/help/v3.2/manual/cmake-properties.7.html#properties-on-tests
  • http://www.cmake.org/cmake/help/v3.2/manual/ctest.1.html
like image 106
jet47 Avatar answered Oct 04 '22 21:10

jet47