Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass command-line arguments in CTest at runtime

Tags:

I'm using CTest and want to pass command-line arguments to the underlying tests at runtime. I know there are ways to hard code command-line arguments into the CMake/CTest script, but I want to specify the command-line arguments at runtime and have those arguments passed through CTest to the underlying test.

Is this even possible?

like image 613
jlconlin Avatar asked Mar 02 '15 14:03

jlconlin


People also ask

How do you pass command line arguments?

If you want to pass command line arguments then you will have to define the main() function with two arguments. The first argument defines the number of command line arguments and the second argument is the list of command line arguments.

Is it possible to pass command line arguments to a test?

It is possible to pass custom command line arguments to the test module.

What is CTest command?

Build and Test Mode. 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>...]

Is CTest part of CMake?

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.


1 Answers

I've figured out a way to do it (using the Fundamental theorem of software engineering). It's not as simple as I'd like, but here it is.

First, create a file ${CMAKE_SOURCE_DIR}/cmake/RunTests.cmake with the content

if(NOT DEFINED ENV{TESTS_ARGUMENTS})     set(ENV{TESTS_ARGUMENTS} "--default-arguments") endif() execute_process(COMMAND ${TEST_EXECUTABLE} $ENV{TESTS_ARGUMENTS} RESULT_VARIABLE result) if(NOT "${result}" STREQUAL "0")     message(FATAL_ERROR "Test failed with return value '${result}'") endif() 

Then, when you add the test, use

add_test(     NAME MyTest     COMMAND ${CMAKE_COMMAND} -DTEST_EXECUTABLE=$<TARGET_FILE:MyTest> -P ${CMAKE_SOURCE_DIR}/cmake/RunTests.cmake ) 

Finally, you can run the test with custom arguments using

cmake -E env TESTS_ARGUMENTS="--custom-arguments" ctest 

Note that if you use bash, you can simplify this to

TESTS_ARGUMENTS="--custom-arguments" ctest 

There are some problems with this approach, e.g. it ignores the WILL_FAIL property of the tests. Of course I wish it could be as simple as calling ctest -- --custom-arguments, but, as the Stones said, You can't always get what you want.

like image 117
Kevin Avatar answered Oct 14 '22 17:10

Kevin