C++20 μ(micro)/Unit Testing framework
"If you liked it then you
"should have put a"_teston it", Beyonce rule
| Motivation | Quick Start | Overview | Tutorial | Examples | User Guide | FAQ | Benchmarks |
C++ single header/single module, macro-free μ(micro)/Unit Testing Framework#include <boost/ut.hpp> // import boost.ut;
constexpr auto sum(auto... values) { return (values + ...); }
int main() {
using namespace boost::ut;
"sum"_test = [] {
expect(sum(0) == 0_i);
expect(sum(1, 2) == 3_i);
expect(sum(1, 2) > 0_i and 41_i == sum(40, 2)); // row 11
};
}UT starts =====================================================================
Running test "sum"...
FAILED in: ...\example.cpp:11 - test condition: [(3 > 0 and 41 == 42)]
===============================================================================
Suite global
tests: 1 | 1 failed
asserts: 3 | 2 passed | 1 failed
Completed =====================================================================Motivation
Testing is a very important part of the Software Development, however, C++ doesn't provide any good testing facilities out of the box, which often leads into a poor testing experience for develops and/or lack of tests/coverage in general.
One should treat testing code as production code!
Additionally, well established testing practises such as Test Driven Development (TDD)/Behaviour Driven Development (BDD) are often not followed due to the same reasons.
The following snippet is a common example of testing with projects in C++.
int main() {
// should sum numbers
{
assert(3 == sum(1, 2));
}
}There are quite a few problems with the approach above
TDD/BDD (Lack of support for sections and declarative expressions)UT is trying to address these issues by simplifying testing experience with a few simple steps:
And you good to go!
Okay, great, but why I would use UT over other/similar testing frameworks already available in C++?
Great question! There are a few unique features which makes UT worth trying
Sounds intriguing/interesting? Learn more at
Quick StartOverviewhttps://bit.ly/ut-quick-start (slides)
test, suite, operators, literals, [expect])Get the latest latest header/module from here!
Include/Import
// #include <boost/ut.hpp> // single header
// import boost.ut; // single module (C++20)
int main() { }Compile & Run
$CXX main.cpp && ./a.outAll tests passed (0 assert in 0 test)[Optional] Install it
cmake -Bbuild -H.
cd build && make # run tests
cd build && make install # install[Optional] CMake integration
This project provides a CMake config and target.
Just load ut with find_package to import the Boost::ut target.
Linking against this target will add the necessary include directory for the single header file.
This is demonstrated in the following example.
find_package(ut REQUIRED)
add_library(my_test my_test.cpp)
target_link_libraries(my_test PRIVATE Boost::ut)[Optional] Conan integration
The boost-ext-ut package is available from Conan Center.
Just include it in your project's Conanfile with boost-ext-ut/2.3.1.
Let's write our first assertion, shall we?
#include <boost/ut.hpp>
int main() {
boost::ut::expect(true);
}UT starts =====================================================================
Suite 'global': all tests passed (1 asserts in 0 tests)
Completed =====================================================================Okay, let's make it fail now?
#include <boost/ut.hpp>
int main() {
boost::ut::expect(1 == 2); // row 4
}.... v vvvvv
FAILED in: ...\example.cpp:4 - test condition: [false]
===============================================================================
Suite global
tests: 0 | 0 failed
asserts: 1 | 0 passed | 1 failed
Completed =====================================================================Notice that expression
1 == 2hasn't been printed. Instead we gotfalse?
Let's print it then?
#include <boost/ut.hpp>
int main() {
using namespace boost::ut;
expect(1_i == 2); // row 5
}.... v vvvvvv
FAILED in: ...\example.cpp:5 - test condition: [1 == 2]
===============================================================================
Suite global
tests: 0 | 0 failed
asserts: 1 | 0 passed | 1 failed
Completed =====================================================================Okay, now we have it!
1 == 2has been printed as expected. Notice the User Defined Literal (UDL)1_iwas used._iis a compile-time constant integer value
See the User-guide for more details.
Alternatively, a
tersenotation (no expect required) can be used.
#include <boost/ut.hpp>
int main() {
using namespace boost::ut::literals;
using namespace boost::ut::operators::terse;
1_i == 2; // terse notation
}UT starts =====================================================================
FAILED in: ...\example.cpp:7 - test condition: [1 == 2]
===============================================================================
Suite global
tests: 0 | 0 failed
asserts: 1 | 0 passed | 1 failed
Completed =====================================================================Other expression syntaxes are also available.
#include <boost/ut.hpp>
int main() {
using namespace boost::ut;
expect(1_i == 2); // UDL syntax
expect(1 == 2_i); // UDL syntax
expect(that % 1 == 2); // Matcher syntax
expect(eq(1, 2)); // eq/neq/gt/ge/lt/le
}UT starts =====================================================================
FAILED in: ...\example.cpp:6 - test condition: [1 == 2]
FAILED in: ...\example.cpp:7 - test condition: [1 == 2]
FAILED in: ...\example.cpp:8 - test condition: [1 == 2]
FAILED in: ...\example.cpp:9 - test condition: [1 == 2]
===============================================================================
Suite global
tests: 0 | 0 failed
asserts: 4 | 0 passed | 4 failed
Completed =====================================================================Okay, but what about the case if my assertion is fatal. Meaning that the program will crash unless the processing will be terminated. Nothing easier, let's just add
fatalcall to make the test fail immediately.
#include <boost/ut.hpp>No open issues yet, or sync has not completed.