Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit-testing MPI Programs with gtest

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.

like image 338
gnzlbg Avatar asked May 13 '13 16:05

gnzlbg


People also ask

What is Gtest used for?

It is a test framework i.e., a software tool for writing and running unit tests. It is a library for writing C++ tests. It is based on xUnit architecture which is a set of “Frameworks” for programming and automated execution of test cases.

Does Gtest run tests in parallel?

gtest-parallel is a script that executes Google Test binaries in parallel, providing good speedup for single-threaded tests (on multi-core machines) and tests that do not run at 100% CPU (on single- or multi-core machines).

Is Gtest header only?

Google Test is not header-only: there are libraries to build. So, as a Visual Studio user, you have essentially two options.

Can I use Gtest for C?

Google test, or gtest is an open source framework for unit testing C\C++ projects. It easily integrates with CMake, has a great assertion engine, and produces XML reports to be for display so that it can be integrated with common CI\CD frameworks.


2 Answers

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());
}
like image 126
Ken H Avatar answered Sep 20 '22 10:09

Ken H


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;
}
like image 27
rmhartog Avatar answered Sep 21 '22 10:09

rmhartog