# add\_test function is prepending "/usr/bin/" to my command

**URL:** https://discourse.cmake.org/t/add-test-function-is-prepending-usr-bin-to-my-command/10691
**Category:** Usage
**Created:** [April 18, 2024, 2:40pm UTC](https://discourse.cmake.org/t/add-test-function-is-prepending-usr-bin-to-my-command/10691 "2024-04-18T14:40:38Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![P11](https://discourse.cmake.org/user_avatar/discourse.cmake.org/p11/32/4506_2.png) [@P11](https://discourse.cmake.org/u/P11)
#### Post date: [April 18, 2024, 2:40pm UTC](https://discourse.cmake.org/t/add-test-function-is-prepending-usr-bin-to-my-command/10691/1 "2024-04-18T14:40:38Z")

</div>

I’m trying to run a test via `mpirun -n 2 python3 ...` and the python3 on my path is not in `/usr/bin` but because, for some reason, `/usr/bin` is being used in the final command via ctest that the command looks like

`/usr/bin/mpirun -n 2 python3 ...` it uses the python3 in /usr/bin, which is not at all what I want.

Previous similar discussions:

> [@add\_custom\_command converting my absolute executable path to relative?](https://discourse.cmake.org/t/add-custom-command-converting-my-absolute-executable-path-to-relative/1326):
>
> As part of the configuration stage of my project, I create a small executable program somewhere in the CMAKE\_BINARY\_DIR tree. I’m then trying to use that program as part of an ‘add\_custom\_command’ call to generate some source files at build time. I’m running into an issue where add\_custom\_command seems to be switching out my absolute path (obtained via find\_program) with a relative path. Then the command fails to execute correctly as part of the custom command. In my case, the generated progra…

suggests that it’s caused by using `WORKING_DIRECTORY=...` which I am using, but I don’t see how that matters, I don’t expect my command to be modified.

Thanks

---

<div class="post-metadata">

### Author: ![P11](https://discourse.cmake.org/user_avatar/discourse.cmake.org/p11/32/4506_2.png) [@P11](https://discourse.cmake.org/u/P11)
#### Post date: [April 18, 2024, 3:19pm UTC](https://discourse.cmake.org/t/add-test-function-is-prepending-usr-bin-to-my-command/10691/2 "2024-04-18T15:19:33Z")

</div>

I can see via `ctest --show-only=json-v1` that `/usr/bin/` is being prepended to the command, but there is no instance of `/usr/bin/mpirun` in the geneated CMake test files

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [April 18, 2024, 4:53pm UTC](https://discourse.cmake.org/t/add-test-function-is-prepending-usr-bin-to-my-command/10691/3 "2024-04-18T16:53:38Z")

</div>

It looks like `cmCTestTestHandler::FindExecutable` does this. But it only does it for the main binary. If you want a specific `python3`, I suggest using an absolute path at configure time; `mpirun` is going to rely on test-time `PATH` (I don’t think CTest has any magic insight into the arguments to `mpirun` here).

---

<div class="post-metadata">

### Author: ![P11](https://discourse.cmake.org/user_avatar/discourse.cmake.org/p11/32/4506_2.png) [@P11](https://discourse.cmake.org/u/P11)
#### Post date: [April 18, 2024, 5:04pm UTC](https://discourse.cmake.org/t/add-test-function-is-prepending-usr-bin-to-my-command/10691/4 "2024-04-18T17:04:12Z")

</div>

> [@ben.boeckel](#):
>
> I suggest using an absolute path at configure time

I guess this is how it will be, thanks

---

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [April 18, 2024, 5:22pm UTC](https://discourse.cmake.org/t/add-test-function-is-prepending-usr-bin-to-my-command/10691/5 "2024-04-18T17:22:03Z")

</div>

technically, `mpirun` or other launcher programs could choose any search path they want for priority–it could be that mpirun is picking `cwd` before `PATH` here as a guess.  
Since I use such launchers a lot, I always use the fully resolved path as an argument to the launcher. Here I would before `add_test()` do

```cmake
find_package(Python COMPONENTS Interpreter REQUIRED)
# add parameters to this if desired to seek the specific Python interpreter desired and/or use `Python_ROOT` to hint location

find_package(MPI REQUIRED)
...

add_test(... COMMAND ${MPIEXEC_EXECUTABLE} ... ${Python_EXECUTABLE} ...)

```

---

<div class="post-metadata">

### Author: ![P11](https://discourse.cmake.org/user_avatar/discourse.cmake.org/p11/32/4506_2.png) [@P11](https://discourse.cmake.org/u/P11)
#### Post date: [April 18, 2024, 6:26pm UTC](https://discourse.cmake.org/t/add-test-function-is-prepending-usr-bin-to-my-command/10691/6 "2024-04-18T18:26:15Z")

</div>

I’m not sure why you are “finding” anything here, either the path is setup properly by the user, or it’s not, so finding it on the path shouldn’t be required?

This seems to be more con with very little pro that I can see

---

<div class="post-metadata">

### Author: ![P11](https://discourse.cmake.org/user_avatar/discourse.cmake.org/p11/32/4506_2.png) [@P11](https://discourse.cmake.org/u/P11)
#### Post date: [April 18, 2024, 6:28pm UTC](https://discourse.cmake.org/t/add-test-function-is-prepending-usr-bin-to-my-command/10691/7 "2024-04-18T18:28:25Z")

</div>

This is nice, but it’s not working for all of our cases

for instance, we have a native binary which launches a python3 interpreter internally, and when it’s started via `/usr/bin/mpirun` rather than just `mpirun` it again, is finding the wrong python3 installation
