Search guides and the APICtrl K
  1. API reference
  2. Applications

unit_test.h

A small unit-test framework for plugin authors' own tests.

#include <sonore/unit_test.h>
On this page

Design notes

Why the SDK ships one

The SDK's own tests do not use this: they are standalone programs run by ctest and by the sanitizer gate, and a framework inside the thing being tested would be a dependency those tests share with the code under test.

A plugin author is in a different position. They have a DSP class, a preset parser, a tuning table, and want to check them without choosing, fetching and wiring up a test library first -- and code moving from the reference arrives with tests written against exactly this shape: a UnitTest subclass per area, beginTest / expect / expectEquals inside runTest(), and a static instance that registers itself.

struct FilterTests : sonore::UnitTest {
  FilterTests() : UnitTest("Filter", "dsp") {}
  void runTest() override {
    beginTest("DC passes a lowpass");
    expectWithinAbsoluteError(runDc(), 1.0f, 1e-4f);
  }
};
static FilterTests filterTests;

int main() {
  sonore::UnitTestRunner runner;
  runner.runAllTests();
  return runner.getTotalFailures() == 0 ? 0 : 1;
}

Each test is given a reproducible random generator; the runner logs its seed, so a failure that depends on random data can be replayed exactly by passing that seed back. An exception escaping a test is a failure, caught and reported, not the end of the run.

Types 2

A group of tests. Subclass it, override runTest(), and create one static instance: it registers itself, and a UnitTestRunner finds it.

Runs UnitTests and keeps their results. Override logMessage to send the log somewhere other than stdout, resultsUpdated to watch progress, and shouldAbortTests to stop a long run early.

↑↓ to move↵ to openEvery public name in the headers, and every guide