Skip to main content

How to use the µTest++ Testing Framework

Rationale​

At its most basic level, the simplest testing approach requires no framework at all. A test for a C or C++ project can be written without any framework, perhaps using the standard assert() macro or a similar user-defined mechanism.

However, with this mechanism, failed assertions typically abort the test, and when dealing with multiple test cases containing numerous checks, it is preferable to obtain a complete report.

Testing frameworks address this need by providing convenient mechanisms for writing various checks and generating clear, structured reports.

The xPack Framework already includes ready-to-use support for several testing frameworks (GoogleTest, Catch2, Boost UT). Nevertheless, these frameworks tend to be quite demanding in terms of memory resources, and the learning curve required to master them can be rather steep.

Therefore, for embedded projects, a simpler solution with a smaller memory footprint was considered a valuable addition.

Intended use​

µTest++ is intended for unit tests: tests that exercise the functions, the classes, and the modules of a project through their interfaces, in isolation from the hardware.

Portable tests​

Such tests are best written as portable programs: the same test sources build both natively, for the development machine, and for the embedded target, while the access to the hardware stays behind interfaces that the tests can replace. The tests of µTest++ itself are written this way, and the same sources run on the native platforms, on nine QEMU platforms, and on physical boards.

The two kinds of runs complement each other:

  • the native runs are fast, and allow the use of a debugger and of the compiler sanitisers, so they are the place for the day-to-day iterations;
  • the runs on the target, on QEMU or on a board, reveal what only the target shows: the sizes and the alignment of the types, the behaviour of the cross toolchain and of its C library, the builds without exceptions, and the limits of the stack and of the memory (see Embedded targets).

Running on QEMU​

For the runs on the target, the recommended platform is QEMU: it emulates the Cortex-M, Cortex-A, and RISC-V cores, with the same cross toolchains as the boards; it needs no hardware and no debugger, forwards the output and the exit code to the host through semihosting, and runs unattended, so the same tests can run in the continuous integration workflows. The xPack builds of QEMU (@xpack-dev-tools/qemu-arm and @xpack-dev-tools/qemu-riscv) are installed with xpm, as dependencies of the project, like the toolchains.

Running on physical boards​

The physical boards remain useful for the final checks: the peripherals, the real timings, and the exact memory limits of a device. The recommended boards are, in order of preference:

  • the Raspberry Pi Pico 2 and the Raspberry Pi Pico: inexpensive, with plenty of flash (4 MiB and 2 MiB), and, on the Pico 2, a choice of Arm Cortex-M33 or RISC-V cores; they are connected to the host through the Raspberry Pi Debug Probe, which runs the tests with semihosting, through OpenOCD (version 0.12 or later); the models with headers connect to the probe with no custom wiring;
  • the STM32 Nucleo boards, with at least 256 KiB of flash, and preferably 512 KiB or more; their on-board ST-LINK debug probe runs the tests with semihosting, through OpenOCD, with no other hardware.

The flash size matters, since even a minimal test program takes over 120 KiB with the full newlib (see Memory footprint). The test platforms of µTest++ include all these boards: the Raspberry Pi Pico, the Pico 2 (Arm and RISC-V), the NUCLEO-F411RE, the NUCLEO-F767ZI, and the NUCLEO-H533RE.

Tests that depend on the hardware​

The tests that depend on the peripherals, or on the timing, can also use the checks and the reports of µTest++; for them, the mocks and the simulation of the hardware, when needed, must be provided by the application, or by a dedicated library, and so must the timing measurements on bare metal, where the framework does not measure the time (see the limits and trade-offs of µTest++).

Overview​

The initial version of the µTest++ Testing Framework drew its primary inspiration from node-tap, with a strong emphasis on simplicity. Version 3.x was heavily influenced by Boost UT, from which it derived its function comparators, while version 4.x represented a complete redesign, drawing even more inspiration from node-tap by passing explicit references to parent objects into test and suite callbacks. Version 6.0 is the result of a systematic review of the API and of the implementation, which simplified the API, reworked the runner configuration, the test hierarchy, the checks, and the reports, and deprecated the overloaded operators, the literals, and the type wrappers in favour of the function comparators. For more details, see the History page.

Concepts and features​

  • The runner drives a test session: it holds the test suites, runs them, counts the passed and failed checks, reports the results, and returns the primary result of the test to the system as the process exit code.
  • A test suite is a named collection of test cases; for more complex applications, test cases are organised into test suites.
  • Test suites can reside in separate compilation units and automatically register themselves with the runner.
  • A test case comprises a sequence of checks (the test conditions of the Glossary), which are expectations or requirements — conditions anticipated to be true.
  • Checks are based on logical expressions, typically evaluating a result and comparing it to an expected value; each check either passes or fails.
  • For C++ projects, it is also possible to verify whether evaluating an expression results in an exception being thrown.
  • A failed expectation is reported and counted, and the test case continues.
  • Requirements are critical conditions that must be satisfied for the test case to proceed; a failed requirement stops the current test case, and the run continues with the next one.
  • The reporter writes the results on stdout, in TAP format by default: one line for each passed test case and, for the failed ones, the failed checks, with their location and, where possible, the values of their operands.
  • Optionally, the complete report is also written to a file (see Output file).

A test suite is deemed successful if there are no failed checks.

If all the test suites are successful, the test is successful, and this is indicated by an exit code of 0 (see Test result for the other codes).

Contents of this guide​

The guide is organised by the task of the reader:

  • Tutorial: the first tests, step by step, with the code and the output of each;
  • Writing checks: expectations, requirements, the function comparators, and the checks for strings, containers, and exceptions;
  • Organising tests: the runner, the top suite, test cases, and test suites;
  • Running tests: the command-line options, the reporters, and the colours;
  • Extending the framework: custom types, custom comparators, and the utility functions;
  • Embedded targets: memory footprint, stack usage, timings, the console output, and the builds without exceptions;
  • Migrating from earlier versions: the replacement of the deprecated operators, literals, and wrappers.

The exact declarations are in the API reference.

C API​

While µTest++ can be used to test C code, its implementation relies extensively on modern C++ features that cannot be replicated in C.

Consequently, there are no C equivalents for the C++ definitions provided by µTest++.