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 trickier than I thought to set arguments for CTest from CMake then I thought. In the end it is quite simple, but existing resources weren’t as easy as they could be.

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 would 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
include(CTest)

You can find this also 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".

0 Comments

Leave a Reply

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