Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CTest with multiple commands

I'm building some tests using CTest. Usually, I can set up the test by simply the line:

ADD_TEST(Test_Name executable args)

However, I've run into a problem, I have some tests that require two commands to be run in order for it to work, is there any way I can run two programs within a single ctest, or am I required to create a new test for each?

Thank you.

like image 779
Leif Andersen Avatar asked Jun 17 '10 20:06

Leif Andersen


People also ask

Does CTest run tests in parallel?

CTest is the test runner that is shipped with CMake. This runner can run tests in parallel using the -j X option (X is the numbers of threads). However, it can only run the tests that are declared in the CMakeLists. txt file.

How CTest works?

The CTest command supports a group of command line options that allow it to be used as the test executable to run. When used as the test executable, CTest can run CMake, run the compile step, and finally run a compiled test. We will now look at the command line options to CTest that support building and running tests.

What is CTest command?

DESCRIPTION. The "ctest" executable is the CMake test driver program. CMake-generated build trees created for projects that use the ENABLE_TESTING and ADD_TEST commands have testing support. This program will run the tests and report results.

What is ENABLE_TESTING in CMake?

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.


1 Answers

The add_test command only accepts one executable, but you can run any executable that is really a script. To do this in a cross platform way, write the script in CMake itself. CMake has the -P option for running arbitrary chunks of CMake scripting language when you run make or make test, rather than at Makefile generation time.

Sadly you can't pass arguments to such a script. But you can set variables to values, which is just as good.

This script you can call runtests.cmake, it runs the commands CMD1 and CMD2 and checks each for a non-zero return code, returning out of CMake itself with an error if that happens:

macro(EXEC_CHECK CMD)
    execute_process(COMMAND ${CMD} RESULT_VARIABLE CMD_RESULT)
    if(CMD_RESULT)
        message(FATAL_ERROR "Error running ${CMD}")
    endif()
endmacro()
exec_check(${CMD1})
exec_check(${CMD2})

... and then add your test cases like so:

add_executable(test1 test1.c)
add_executable(test2 test2.c)
add_test(NAME test
    COMMAND ${CMAKE_COMMAND}
            -DCMD1=$<TARGET_FILE:test1>
            -DCMD2=$<TARGET_FILE:test2>
    -P ${CMAKE_CURRENT_SOURCE_DIR}/runtests.cmake)

$<TARGET_FILE:test1> gets expanded to the full path to the executable at build-file generation time. When you run make test or equivalent this will run "cmake -P runtests.cmake" setting the CMD1 and CMD2 variables to the appropriate test programs. The script will then execute your 2 programs in sequence. If either of the test programs fail, the whole test fails. If you need to see the output of the test, you can run make test ARGS=-V

like image 189
richq Avatar answered Sep 20 '22 12:09

richq