CTest --build-and-test with --test-command option
Asked Answered
B

1

11

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?

Betweentimes answered 6/7, 2016 at 21:13 Comment(2)
Is there a good example for the usage of the --test-command option? This link has usage but it doesn't work: public.kitware.com/pipermail/cmake/2009-March/028066.htmlBetweentimes
I'm trying to achieve to exact same thing. Did you find a solution?Ruffin
H
3

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")
Harmonia answered 13/2, 2020 at 0:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.