Search code examples
cmakempigoogletestctest

ctest and MPI parallel tests


I'm trying to build some tests with googleTest which should test some MPI parallel code. Ideally I want our CI server to execute them through ctest.

My naive approach was to simply call ctest with MPI:

mpirun -n 3 ctest

However, this results in even trivial tests failing as long as at least two are executed.

Example of a trivial tests:

TEST(TestSuite, DummyTest1) {
  EXPECT_TRUE(true);
}
TEST(TestSuite, DummyTest2) {
  EXPECT_TRUE(true);
}

Am I supposed to launch ctest in some other way? Or do I have to approach this completely differently?

Additional info:

  • The tests are added via gtest_discover_tests().
  • Launching the test executable directly with MPI (mpirun -n 3 testExe) yields successful tests but I would prefer using ctest.
  • Version info:
    • googletest: 1.11.0
    • MPI: OpenMPI 4.0.3 AND MPICH 3.3.2

Solution

  • According to this CMake Forum post, which links to a Merge Requests and an issue in the CMake GitLab repository, gtest_discover_tests() currently simply doesn't support MPI.

    A possible workaround is to abuse the CROSSCOMPILING_EMULATOR property to inject the wrapper into the test command. However, this changes the whole target and not only the tests so your mileage may vary.

    set_property(TARGET TheExe PROPERTY CROSSCOMPILING_EMULATOR '${MPIEXEC_EXECUTABLE} ${MPIEXEC_NUMPROC_FLAG} 3')
    gtest_discover_tests(TheExe)
    

    See the forum post for a full minimal example.