Skip to main content

µTest++ Testing Framework

A macro-free C++ testing framework for embedded projects, with TAP output

xpm install @micro-os-plus/micro-test-plus@6.0.1 --verbose

Macro-free, expressive checks

Checks are plain C++20 expressions, such as t.expect (mt::eq (a, 42)), written without C macros. A failed check reports the file, the line, and the actual values of the operands. Test suites are static objects that register themselves, so adding a new suite requires no changes to existing code.

Built for bare-metal

The framework does not use iostream, so, on Cortex-M, the flash it adds to a program is less than half of that added by the other C++ testing frameworks measured (see the figures next to the example below). Results are written to stdout, so semihosting or a serial port is sufficient. The same tests run on Cortex-M, Cortex-A, and RISC-V devices, on QEMU, and on native macOS, Windows, and GNU/Linux hosts.

Standard TAP output

Results use the TAP 14 format by default, so standard TAP tools and CI pipelines can process them. Use --reporter=human for readable console output, and --output-file to also save the results to a file. The overall outcome is returned as the process exit code.


A first test

The outputs below were produced by running the program with --verbose; the header lines, the blank lines, the timings, and the totals were omitted.

minimal-test.cpp
#include "micro-os-plus/micro-test-plus.h"

// Define a shortcut for the namespace.
namespace mt = micro_os_plus::micro_test_plus;

// Create the test runner.
mt::runner tr;

int
main (int argc, char* argv[])
{
// Initialise the runner with the options
// passed as command-line arguments.
tr.initialise (argc, argv);

// Add the top suite and get a reference to it.
auto& ts = tr.add_top_suite ();

// Add a test case to the top suite;
// its body runs immediately.
ts.test ("Check truth", [] (auto& t)
{
// Check that 6 times 7 equals 42.
t.expect (mt::eq (6 * 7, 42)) << "the answer is 42";
});

// Run the registered suites, if any, and return
// the test result as process exit code.
return tr.run_suites ();
}
TAP output (default)
TAP version 14
# Subtest: minimal-test
# Subtest: Check truth
ok 1 - the answer is 42
1..1
ok 1 - Check truth # { passed, 1 check }
1..1
ok 1 - minimal-test # { passed, 1 check in 1 test case }
1..1
Human-readable output (--reporter=human)
µTest++ human-readable report
• minimal-test
• Check truth
✓ the answer is 42
✓ Check truth - passed (1 check)
✓ minimal-test - passed (1 check in 1 test case)

Footprint. On a Cortex-M4F, a similar program, with one test case and two checks, adds 46 KiB of flash to an empty program (81 KiB) with µTest++, against 102 KiB with CppUTest, and 354 to 507 KiB with Boost UT, doctest, GoogleTest, snitch, Boost.Test, and Catch2 (measurements).

Where to run. Natively while developing, then on QEMU, which emulates Cortex-M, Cortex-A, and RISC-V cores with no hardware, also in CI. On hardware, prefer the Raspberry Pi Pico 2 and Pico, then the STM32 Nucleo boards with 256 KiB of flash (preferably 512 KiB) or more (details).