# ctest and MPI parallel googletests

**URL:** https://discourse.cmake.org/t/ctest-and-mpi-parallel-googletests/5557
**Category:** Code
**Tags:** tool:ctest
**Created:** [April 27, 2022, 1:02pm UTC](https://discourse.cmake.org/t/ctest-and-mpi-parallel-googletests/5557 "2022-04-27T13:02:59Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![FG-TUM](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fg-tum/32/2377_2.png) [@FG-TUM](https://discourse.cmake.org/u/FG-TUM)
#### Post date: [April 27, 2022, 1:02pm UTC](https://discourse.cmake.org/t/ctest-and-mpi-parallel-googletests/5557/1 "2022-04-27T13:02:59Z")

</div>

## The Goal

My goal is to create tests with `googleTest` that make use of MPI, add them via `gtest_discover_tests()`, and execute them via `ctest`.

I have seen posts where people use `add_test` to add the MPI executer manually to their tests (e.g. [here](https://discourse.cmake.org/t/quotations-in-ctest-scripts-and-proper-usage-of-mpiexec-preflags/338)) but `gtest_discover_tests()` (which is way handier) seems to not expose such an option. There exists the `CROSSCOMPILING_EMULATOR` property which can apparently be abused for this but this seems hacky (see [here](https://gitlab.kitware.com/cmake/cmake/-/issues/23455)).

## Minimal Example:

CMakeLists.txt:

```cmake
cmake_minimum_required(VERSION 3.23)
project(TheProject)
set(CMAKE_CXX_STANDARD 17)
# get dependencies
include(FetchContent)
FetchContent_Declare(
        googletest
        URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)
FetchContent_MakeAvailable(googletest)
find_package(MPI REQUIRED)
# define a exe
add_executable(TheExe main.cpp theTest.cpp)
target_link_libraries(TheExe PUBLIC gtest MPI::MPI_CXX)
# set up tests
enable_testing()
include(GoogleTest)
set_property(TARGET TheExe PROPERTY CROSSCOMPILING_EMULATOR '${MPIEXEC_EXECUTABLE} ${MPIEXEC_NUMPROC_FLAG} 3')
gtest_discover_tests(TheExe)

```

theTest.cpp: (some dummy tests)

```cpp
#include <gtest/gtest.h>
#include <mpi.h>
TEST(HelloTest, TestA) {
  int rank;
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  std::cout << rank << std::endl;
  EXPECT_LT(rank, 1337);
}
TEST(HelloTest, TestB) {
  MPI_Barrier(MPI_COMM_WORLD); // sync all ranks
  EXPECT_EQ(42, 7*6);
}

```

main.cpp

```cpp
#include <gtest/gtest.h>
#include <mpi.h>

int main(int argc, char **argv) {
  testing::InitGoogleTest(&argc, argv);
  MPI_Init(&argc, &argv);
  auto result = RUN_ALL_TESTS();
  MPI_Finalize();
  return result;
}

```

Build via CMake and then execute with `ctest`. This appears to give the desired behavior, alas very verbose because we get the output of all ranks. There is also no way to make use of `MPIEXEC_POSTFLAGS`. However, at least for me, both of these points are secondary.

## The Question

Is this is recommended / proper /best way to achieve the goal stated in the beginning??

---

<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 27, 2022, 1:11pm UTC](https://discourse.cmake.org/t/ctest-and-mpi-parallel-googletests/5557/2 "2022-04-27T13:11:27Z")

</div>

I suspect this is something that `gtest_discover_tests()` just doesn’t support right now.

Cc: @Matthew_Woehlke

---

<div class="post-metadata">

### Author: ![Matthew\_Woehlke](https://discourse.cmake.org/user_avatar/discourse.cmake.org/matthew_woehlke/32/2345_2.png) [@Matthew\_Woehlke](https://discourse.cmake.org/u/Matthew_Woehlke)
#### Post date: [April 27, 2022, 2:18pm UTC](https://discourse.cmake.org/t/ctest-and-mpi-parallel-googletests/5557/3 "2022-04-27T14:18:06Z")

</div>

What Ben said. Wrapping tests with another executable (which IIUC is what needs to happen here) is a fairly exotic use case, outside, perhaps, of emulators for cross-compiles (i.e. why `CROSSCOMPILING_EMULATOR` exists).

Contributions welcomed? 🙂

---

<div class="post-metadata">

### Author: ![FG-TUM](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fg-tum/32/2377_2.png) [@FG-TUM](https://discourse.cmake.org/u/FG-TUM)
#### Post date: [April 28, 2022, 9:18am UTC](https://discourse.cmake.org/t/ctest-and-mpi-parallel-googletests/5557/4 "2022-04-28T09:18:17Z")

</div>

@Matthew_Woehlke while I agree that this wrapping seems weird, I would argue that MPI is hardly an exotic tool. That’s why I was wondering whether I’m just looking at this from the wrong angle and whether all of this should be done totally differently?

---

<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 28, 2022, 10:57am UTC](https://discourse.cmake.org/t/ctest-and-mpi-parallel-googletests/5557/5 "2022-04-28T10:57:37Z")

</div>

MPI support could probably be added, but it’d need someone to do so.
