Unit-testing MPI Programs with gtest
Asked Answered
A

2

10

I'm parallelizing an already existent application that uses gTest with MPI. In MPI programs, the first thing to do is to initialize the environment with a call to

MPI_Init( int *argc, char ***argv )

At the end of an MPI program the root process should also call MPI_Finalize. How can I write unit-tests for such an application using Google Test?

In particular, how do I access argc, and argv from the tests before gTest modifies them.

Right now I'm doing:

int argc = 0;
char** argv = NULL;
boost::mpi::environment env(argc,argv);

TEST(component_test, test_name) {
  // stuff using mpi
}

and feels wrong.

Anecdotal answered 13/5, 2013 at 16:16 Comment(0)
H
16

Are you sure you want to access the argc and argv values before googletest? They are modified to remove googletest specific arguments such as --gtest_filter so that the application does not see them.

I think what you want to do is simply using the following snippet as your main:

int main(int argc, char* argv[]) {
    int result = 0;

    ::testing::InitGoogleTest(&argc, argv);
    MPI_Init(&argc, &argv);
    result = RUN_ALL_TESTS();
    MPI_Finalize();

    return result;
}
Haunted answered 13/5, 2013 at 18:54 Comment(2)
Thanks! Does InitGoogleTest remove arguments that are not relevant to gTest?Anecdotal
No, it doesn't. Only the gtest arguments.Haunted
V
5

Just add to @rmhartog's answer.

You probably also want to add below to leave only one listener for printing before RUN_ALL_TESTS(), otherwise, the stdout messages mingle.

::testing::TestEventListeners& listeners =
    ::testing::UnitTest::GetInstance()->listeners();
if (world.rank() != 0) {
    delete listeners.Release(listeners.default_result_printer());
}
Virilism answered 14/9, 2017 at 23:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.