# Executing a complex command in CTest

**URL:** https://discourse.cmake.org/t/executing-a-complex-command-in-ctest/541
**Category:** Usage
**Created:** [January 27, 2020, 2:20pm UTC](https://discourse.cmake.org/t/executing-a-complex-command-in-ctest/541 "2020-01-27T14:20:57Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Aurelien\_Coussat](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@Aurelien\_Coussat](https://discourse.cmake.org/u/Aurelien_Coussat)
#### Post date: [January 27, 2020, 2:20pm UTC](https://discourse.cmake.org/t/executing-a-complex-command-in-ctest/541/1 "2020-01-27T14:20:57Z")

</div>

Hi!

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

```auto
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  
provided.

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,

Aurélien.

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [January 27, 2020, 2:29pm UTC](https://discourse.cmake.org/t/executing-a-complex-command-in-ctest/541/2 "2020-01-27T14:29:58Z")

</div>

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

The script: `myscript.cmake`:

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

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

```

and the test:

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

```

---

<div class="post-metadata">

### Author: ![Aurelien\_Coussat](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@Aurelien\_Coussat](https://discourse.cmake.org/u/Aurelien_Coussat)
#### Post date: [January 31, 2020, 1:30pm UTC](https://discourse.cmake.org/t/executing-a-complex-command-in-ctest/541/3 "2020-01-31T13:30:05Z")

</div>

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