I use Ctest to run a bunch of google tests that I have registered using add_test()
. Currently, these tests do not take any arguments. However, I want to give them all arguments (common to all, specifically --gtest_output=xml
) while running ctest
.
I heard that this is possible using that --test-command
option, however, I see that we need to use --test-command
along with --build-and-test
. Is there an example for this usage?
Adding tests to your project There are two steps to perform to integrate your CMake build system with the CTest tool: Call the enable_testing command. This takes no arguments. Add tests with the add_test command.
CTest provides a command-line signature to configure (i.e. run cmake on), build, and/or execute a test: ctest --build-and-test <path-to-source> <path-to-build> --build-generator <generator> [<options>...]
CTest is an executable that comes with CMake; it handles running the tests for the project. While CTest works well with CMake, you do not have to use CMake in order to use CTest.
enable_testing() Enables testing for this directory and below. This command should be in the source directory root because ctest expects to find a test file in the build directory root. This command is automatically invoked when the CTest module is included, except if the BUILD_TESTING option is turned off.
The format for ctest
arguments are specified in the CTest documentation as such:
ctest --build-and-test <path-to-source> <path-to-build> --build-generator <generator> [<options>...] [--build-options <opts>...] [--test-command <command> [<args>...]]
So, on Visual Studio for example, you could run test Test1
from your build folder (using ..
for the source directory, and .
for the binary directory):
ctest --build-and-test .. . --build-generator "Visual Studio 16 2019" --test-command Test1 --gtest_output=xml
As of CMake 3.17, you can now specify arguments to pass to CTest (and arguments for individual tests) in the CMake file where you define the test itself. The CMAKE_CTEST_ARGUMENTS
variable takes a semicolon-delimited list of arguments to pass to CTest. So, sticking with the above example, you could do something like this:
set(CMAKE_CTEST_ARGUMENTS "--build-and-test;${CMAKE_SOURCE_DIR};${CMAKE_BINARY_DIR};--build-generator;${CMAKE_GENERATOR};--test-command;Test1;--gtest_output=xml")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With