Testing¶
Operator developer Framework developer
Two kinds of tests are available to plugins and applications built on Onika:
unit tests compiled into the plugins and run by onika-exec, and
CMake tests (ctest), including regression tests that run input files.
Unit tests¶
onika/test/unit_test.h lets plugins embed unit tests, run with
--run_unit_tests true:
#include <onika/test/unit_test.h>
#include <onika/string_utils.h>
ONIKA_UNIT_TEST(my_string_tests)
{
ONIKA_TEST_ASSERT( onika::format_string("%03d", 7) == "007" );
ONIKA_TEST_ASSERT( onika::split_string("a b c").size() == 3 );
}
Unit test 1/6 : my_failing_test ... Assertion '1 + 1 == 3' failed
FAILED
Unit test 2/6 : my_string_tests ... Ok
Unit test 3/6 : onika_physics_units_conversion ... ...
Unit test 4/6 : xsdatamove_process_id_range_size ... Ok
...
Executed 6 unit tests : 4 passed, 2 failed
The tests of Onika itself and of the loaded plugins are run, then the program exits. The exit code is the number of failed tests, so it can be used in CI.
Load the plugins
Plugins are loaded on demand (when an input file uses one of their
operators). With an empty input file, only the tests of Onika itself run,
unless the plugin database is being regenerated: add
--generate_plugins_db true to load all plugins and run all tests.
For tests driven by CMake (ctest), see
Tests with ctest.
Tests with CMake and ctest¶
find_package(onika) also provides CMake functions to declare tests in an
application or a plugin project:
| Function | Meaning |
|---|---|
onika_add_test(<name> <command> [args...]) |
Adds the test onika_test_<name> running <command>. |
onika_add_test_par(<name> <np> <nc> <command> [args...]) |
Same, with <np> MPI processes and <nc> threads each. |
AddRegressionTestDir(<dir>) |
Adds one test per *.msp file of each sub-directory of <dir>, run with onikarun, in sequential (_seq), multi-threaded (_mt) and MPI (4 processes, _par) variants. |
The first line of each .msp file of AddRegressionTestDir can contain
keywords that control its variants:
| Keyword | Effect |
|---|---|
no-seq, no-mt, no-mpi |
Do not create this variant. |
nthreads=<n> or nthreads=max |
Threads for the _mt and _par variants (default 4). |
enable-if-<VAR> / enable-if-<VAR>=<value> |
Create the test only if the CMake variable <VAR> is true (or equal to <value>). |
The variants are switched globally by ONIKA_REGRESSION_TEST_ENABLE_SEQ,
_MT and _MPI (CMake options). Run the tests with ctest in the build directory.
Unit tests embedded in plugins are run differently, with
--run_unit_tests (see Unit tests).