AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
AnCH unit tests library documentation

Introduction

AnCH unit tests library is unit tests library which provides utility class and functions to generate unit tests.

Prerequisites

AnCH unit tests depends on anch-cli library to parse unit tests executable.

Installation

CMake target anch::ut:

find_package(Anch REQUIRED)
target_link_libraries(my-app PRIVATE anch::ut)

See installation.

Conception

AnCH unit tests library provides main function which will execute the following calls:

These functions can be registered with the following signature std::function<void()> and can throw exceptions which will be caught by the library.
When initialization fails, tests and uninitialization are not run. A failed uninitialization fails the unit tests.
Tests are run in registration order (or in command line order with -t ).
To register tests, function anch::ut::setup(anch::ut::UnitTests&) has to be declared.

Unit tests name and description can be registered in this function with name and description anch::ut::UnitTests methods.

It will make executable with help argument (-h or --help for help): -l or --list-tests to list registered unit test, -t or --test followed by the test name to run a specific test and nothing to run all tests.

anch::ut::assertThat function is based on std::format function with {} paceholders: the message has to be a string literal, and it is checked at compile time against the arguments (literal braces are written {{ and }} ). Failed assertions (assertThat , assertTrue , assertFalse and fail ) report their file and line.

A failed assertion in a thread other than the test one terminates the process: wrap thread functions with anch::ut::ThreadErrors::wrap (ut/threadErrors.hpp ) and call anch::ut::ThreadErrors::rethrow once threads have been joined.

Colors are disabled when output is not a terminal or when NO_COLOR environment variable is set.

Examples

Unit tests suite (tests exception , assert and assert-format fail on purpose):

#include "ut/unit.hpp"
#include <iostream>
#include "ut/assert.hpp"
void
startup() {
std::cout << "Initialize tests suite" << std::endl;
}
void
before() {
std::cout << "Before test" << std::endl;
}
void
after() {
std::cout << "After test" << std::endl;
}
void
shutdown() {
std::cout << "Uninitialize tests suite" << std::endl;
}
void
testNothing() {
std::cout << "Nothing" << std::endl;
}
void
testException() {
std::cout << "Exception" << std::endl;
throw std::exception();
}
void
testAssert() {
std::cout << "Assert" << std::endl;
anch::ut::assertFalse(true, "ASSERT !!!");
}
void
testAssertFormat() {
std::cout << "Assert" << std::endl;
anch::ut::assertThat(false, "format message: int={}, float={}, str={}", 2, 3.14, "toto");
}
void
tests
.name("AnCH unit tests")
.description("Test AnCH unit tests library")
.initialize(startup)
.beforeTest(before)
.afterTest(after)
.uninitialize(shutdown)
.add("nothing", testNothing)
.add("exception", testException)
.add("assert", testAssert)
.add("assert-format", testAssertFormat)
;
}