Classes

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

class UnitTest

The base class for all unit test suites.

Please read the documentation on how to write your unit tests.

Warning

Thread Safety: The unit tests are meant to run in a single thread. If tests use multithreaded 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.

Usage: class MyTest final : public el::UnitTest { public: TestedType testedType{}; auto additionalErrorMessages() -> std::string override { try { std::string text; text += std::format("testedType.foo = {}\n", testedType.foo); // ... return text; } catch(...) { return {"Unexpected Exception"}; } // ... };

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 tests are failing.

Usage: int x = 5; runWithContext(SOURCE_LOCATION(), [&]() { x = 9; REQUIRE(x == 10); }, [&]() { return std::format("x = {}", x); });

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.