Classes

See The UnitTest Class for a detailed description of the unittest class, and how to use it.

class UnitTest

A minimal unit test system to write tests for the library.

You can use it for your own unittests, but be aware of the potential naming conflicts because of all the macros used for the tests. Make sure you add #include <erbsland/UnitTest.h> as the last include statement in your unittest file!

See the existing unit tests for the core library how to use this unit test class.

Thread Safety: The unit tests are meant to run in a single thread. If tests use multi threaded testing, macros like REQUIRE() must only be called from the main thread.

Public Functions

UnitTest() = default

Create a new unittest instance.

virtual ~UnitTest() = default

dtor

virtual auto additionalErrorMessages() -> std::string

Add additional output to the error message.

Overwrite this method to add additional text after the error message.

Returns:

The additional text that is added to the error message. May contain newlines.

virtual void setUp()

Execute code before every test in this class.

virtual void tearDown()

Execute code after every test in this class.

void runWithContext(const SourceLocation &sourceLocation, const std::function<void()> &testFn, const std::function<std::string()> &diagnoseFn = nullptr)

Run a code in a separate context and optionally collect additional information if a tests is failing.

Use this function like this:

int x = 5;
runWithContext(SOURCE_LOCATION(), [&]() {
    x = 9;
    REQUIRE(x == 10);
}, [&]() {
    std::stringstream text;
    text << "x = " << x;
    return text.str();
});

Parameters:
  • sourceLocation – Use the SOURCE_LOCATION() macro for this parameter.

  • testFn – The lambda function with the tests.

  • diagnoseFn – Optional function to collect diagnose information.

void consoleWriteLine(const std::string &text)

Method to write debug messages to the console.

A newline will be added automatically.

Parameters:

text – The text to write to the console.

auto unitTestExecutablePath() -> std::filesystem::path

Access the executable path for the unittest executable.

Returns:

The absolute path to the currently executed unittest executable.