North Denver Metro C++ Meetup, Dec 2019
[Boost].UT
Unit Testing Framework
Overview
(*) Limitations may apply
[Boost].UT is not an official Boost library
int main() {
"hello world"_test = [] {
expect(true);
};
}
$CXX $CXXFLAGS hello_world.cpp -o hello_world
./hello_world
-> All tests passed (1 asserts in 1 tests)
expect(1_i == 2);
-> assertions.cpp:1:FAILED [1 == 2]
expect(that % 1 == 2);
-> assertions.cpp:1:FAILED [1 == 2]
expect(2 == 1_i) << "should equal?";
-> assertions.cpp:1:FAILED [2 == 1] should equal?
std::vector v{1l, 2l, 3l};
!expect(4_ul == std::size(v));
expect(v[3] == 4_l);
-> assertions.cpp:2:FAILED [4 == 3]
expect(41.10_d == 42.101 and "a" == "b"sv);
-> assertions.cpp:1:FAILED [42.1 == 42.101 and a == b]
"[vector]"_test = [] {
std::vector<int> v(5);
!expect(5_ul == std::size(v));
should("resize bigger") = [=] {
v.resize(10);
expect(10_ul == std::size(v));
};
!expect(5_ul == std::size(v));
should("resize smaller") = [=] {
v.resize(0);
expect(0_ul == std::size(v));
};
};
"[vector]"_test = [] {
given("I have a vector") = [] {
std::vector<int> v(5);
!expect(5_ul == std::size(v));
when("I resize bigger") = [=] {
v.resize(10);
then("The size should increase") = [=] {
expect(10_ul == std::size(v));
};
};
};
};
"args"_test = [](const auto& arg) {
expect(arg > 0_i) << "all values greater than 0";
}
| std::vector{1, 2, 3};
-> All tests passed (3 asserts in 3 tests)
"args and types"_test =
[]<class TArg>(const TArg& arg) {
expect(std::is_integral_v<TArg>);
expect(type<TArg> == type<int> or type<TArg> == type<bool>);
}
| std::tuple{true, 42};
-> All tests passed (4 asserts in 2 tests)
suite errors = [] {
"exception"_test = [] {
expect(throws([] { throw 0; })) << "throws any exception";
};
"failure"_test = [] {
expect(aborts([] { assert(false); }));
};
};
int main() { }
-> All tests passed (2 asserts in 2 tests)
"If you liked it then you "should have put a"_test on it", Beyonce rule