KaMPIng 0.1.1
Flexible and (near) zero-overhead C++ bindings for MPI
|
In this document, we describe how to write tests for the KaMPIng library. We use GoogleTest for writing tests. Write many unit tests for your code. Check for nonsensical input and edge cases.
Our general philosophy is:
If there is no unit test for it, it does not exist.
Consider the following class:
The corresponding test file should be named tests/foo_test.cpp
. Inside this file, add tests using the following naming scheme: TEST(FooTest, <member function of Foo>_<description>)
, where <description>
could for example be: basics
, invalid_parameters
, or many others.
The tests for Foo
may look like this:
Do not forget to document what your tests are checking.
If you have written your unit tests, you need to build the tests and register them with ctest
so they may be executed automatically by our CI or by running ctest
or make test
in the build directory.
We provide several CMake helper functions to ease test registration. These helpers are implemented in the CMake modules KampingTestHelper
and KaTestrophe
, but usually you should only have to use the former.
To simplify building a specific test by relying on command line completion (e.g. by using make test_<TAB>
), the test target names should start with the prefix test_*
.
To register a test tests/foo_test.cpp
which should not be executed using mpiexec
use kamping_register_test(...)
and add the following line to tests/CMakeLists.txt
This links the test with KaMPIng (which transitively links MPI), adds the test target and registers the test with ctest
.
If your test test/foo_mpi_test.cpp
should be executed in parallel using MPI use kamping_register_mpi_test(...)
like so:
The test will be executed using 1, 2, 4 and 8 MPI ranks separately.
For a detailed description of the functions available for registering tests, see the reference below.
If you want to test that a certain piece of code does not compile, you can create a compilation failure test. This is one of the only possibilities to test that a templated class cannot be instantiated with a specific template parameter or that a static_assert
fails.
KaMPIng provides a CMake helper for creating compilation failure tests. Let's look at the example of mpi_datatype<Datatype>()
which should not compile for types for which there is no equivalent MPI datatype, e.g. void
or a pointer.
First, we have to create a simple cpp
file containing the code to be tested. To reduce the amount of duplicated code, the CMake macro for compilation failures supports sections. In each compilation, only one of sections (POINTER
, VOID
) is enabled. To test that there is no bug in the remainder of the file, we also define a else
section for which compilation should succeed. Our CMake helper will automatically compile the file without any sections macros defined to check this.
This is the content of our compilation failure test for mpi_datatype<Datatype>()
.
And the CMake code to register our tests:
If you want to link libraries other than KaMPIng, use katestrophe_add_compilation_failure_test
.
The CMake module KaTestrophe
provides convenience wrappers for registering tests for KaMPIng.
The CMake module KaTestrophe
provides general functions for registering unit tests with ctest
, which should be executed using mpiexec
. They are agnostic to the library under test and do not depend on KaMPIng. You should only have to use them if you want fine control over what is linked to your test and how it is executed.