Executing a complex command in CTest


I am trying to add a test to my project using add_test. It works well
when I provide simple commands. I am now trying to add a more complex
test which should succeed when different commands succeed, i.e. I would
like to execute a command that contains bash’s &&.

I have tried writing something like

add_test(NAME myexecutabletest COMMAND myexecutable --param1 &&
myexecutable --param2 && myexecutable --param3)

but it keeps being interpreted as myexecutable to which the arguments
--param1 && myexecutable --param2 && myexecutable --param3 are

I have also tried to translate my command to a “simpler” command using
bash’s test but without any success so far.

I am fairly new to CMake and CTest, so I might be missing something
obvious, or I might simply not do things the right way. Anyway, any
help would be greatly appreciated!

Best regards,


The most simple solution is to encapsulate your commands in a CMake script and launch this script through add_test.

The script: myscript.cmake:

execute_process (COMMAND myexecutable --param1 RESULT_VARIABLE status)
if (NOT status)
execute_process (COMMAND myexecutable --param2 RESULT_VARIABLE status) 
# etc...

if (status)
message (FATAL_ERROR "test failed")

and the test:

add_test (NAME myexecutabletest COMMAND ${CMAKE_COMMAND} -P "my_script.cmake")

Thank you very much for your answer! Good to know that this solution
exists. I guess it solves my problem.