Set CTest Arguments from CMake (for `make test`)

Published by Philipp Schuster on

Set CTest arguments from CMake (Code Snippet).

Just a quick follow-up to my previous post. It was somehow trickier than I thought to set arguments for CTest from CMake. In the end, the solution is quite simple, but existing resources weren’t as helpful as they should.

For reference: If you type $ ninja test or $ make test in the generated build directory from CMake, CTest is invoked under the hood. By setting CMAKE_CTEST_ARGUMENTS at the right place in your CMakeLists.txt file, you can set additional arguments for CMake. Otherwise, you have to invoke $ ctest --my--additional args manually.

TL;DR: The solution is the following:

# Must be set before including CTest so that these arguments are applied when
# running `$ ninja test` or `$ make test`.
set(CMAKE_CTEST_ARGUMENTS "--output-on-failure" "--output-junit" "junit.xml")
# This entry must be placed in the top-level CMakeLists.txt file to ensure 
# proper test discovery

TL;DR 2: This typically applies for all CMake modules that are added via include(): first specify variables, then include the module.

You can find an example in my example project on GitHub.

Philipp Schuster

Hi, I'm Philipp and interested in Computer Science. I especially like low level development, making ugly things nice, and de-mystify "low level magic".


Leave a Reply

Your email address will not be published. Required fields are marked *