micro-test-plus 4.1.0
µTest++ Testing Framework
Loading...
Searching...
No Matches
Test Suites

Test suites are named groups of related test cases. More...

Classes

class  micro_os_plus::micro_test_plus::static_suite
 A test suite designed for static (namespace-scope) registration with a static_runner. More...
class  micro_os_plus::micro_test_plus::suite
 A named, runnable test suite registered with the test runner. More...

Detailed Description

By default, all test cases defined in main() are included in the top-level test suite, which is created by runner::initialise() and executed immediately. For more complex projects, additional test suites can be defined, typically in separate source files.

Stand-alone test suites are implemented as mt::static_suite objects, constructed with a name, a reference to the runner, and a callable (usually a lambda that chains execution of test cases), along with any optional arguments. This design enables self-registration of test suites, ensuring they are automatically included in the test run.

Important
It is strongly recommended to instantiate additional test suites as static objects so that their constructors are invoked before main() begins.

Self-registration is performed within the constructor, ensuring that each test suite is automatically included in the test run. When test suites are defined in separate compilation units, they can be executed in any order, as the sequence in which static constructors are invoked is not specified. Consequently, test suites should be designed to be independent and must not rely on the execution order of other suites.

All registered test suites are executed when runner::exit_code() is called, guaranteeing comprehensive test coverage across the project.

Examples
// In main.cpp: create the runner (the static_runner variant also works).
static mt::static_runner tr{ "Sample" };
int
main (int argc, char* argv[])
{
auto& ts = tr.initialise (argc, argv);
ts.test ("Check one", [] (auto& t) {
t.expect (true) << "Passed";
});
return tr.exit_code ();
}
// In a separate source file: a self-registering suite.
static mt::static_suite ts_extra
= { "Extra suite", tr, [] (auto& t) {
t.test ("Check two", [] (auto& t) { t.expect (true) << "Passed"; });
} };
// Parametrised test suite accepting additional arguments.
static void
test_suite_with_args (mt::static_suite& t, int ic, int iv,
int& ir, int* ip1, int* ip2)
{
t.test ("args", [&] (auto& t) {
t.expect (mt::eq (ic, 42)) << "ic is 42";
t.expect (mt::eq (iv, 43)) << "iv is 43";
t.expect (mt::eq (ir, 44)) << "ir is 44";
t.expect (mt::eq (*ip1, 45)) << "*ip1 is 45";
t.expect (mt::eq (*ip2, 46)) << "*ip2 is 46";
});
}
static int in = 43;
static int in44 = 44;
static int& ir = in44;
static int in45 = 45;
static int in46 = 46;
static int* ip2 = &in46;
static mt::static_suite ts_args
= { "Args suite", tr, test_suite_with_args,
42, in, ir, &in45, ip2 };
Primary namespace for the µTest++ testing framework.