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
make install
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.
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
<the test to run> to run a specific test and nothing to run all tests.
anch::ut::assert
function is based on std::format
function with {}
paceholders.
Examples
Serialize structure into JSON format:
#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::assert(false, "format message: int={}, float={}, str={}", 2, 3.14, "toto");
}
void
tests
.
add(
"nothing", testNothing)
.
add(
"exception", testException)
.
add(
"assert", testAssert)
.
add(
"assert-format", testAssertFormat)
;
}
Unit tests collection.
Definition unit.hpp:37
UnitTests & afterTest(std::function< void(void)> afterFunc)
UnitTests & uninitialize(std::function< void(void)> uninitFunc)
UnitTests & name(const std::string &name)
UnitTests & add(const std::string &name, std::function< void(void)> testFunc)
UnitTests & initialize(std::function< void(void)> initFunc)
UnitTests & description(const std::string &description)
UnitTests & beforeTest(std::function< void(void)> beforeFunc)