Skip to content

boost.ut

1
2
FetchContent_Declare(boost.ut GIT_REPOSITORY https://github.com/boost-ext/ut GIT_TAG v1.1.8)
FetchContent_MakeAvailable(boost.ut)

1
2
add_executable(unit_tests unit_tests_ut.cpp)
target_link_libraries(unit_tests PRIVATE boost.ut)

1
#include <boost/ut.hpp>

1
2
3
4
5
6
7
8
9
expect(true);
expect(1 == 2);
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
expect((1 == 1_i) >> fatal); // fatal assertion
expect(1_i == 2);            // not executed
expect(42l == 42_l and 1 == 2_i) << "additional info\n";

1
2
3
4
"hello world"_test = [] {
    int i = 42;
    expect(42_i == i);
};

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
"[vector]"_test = [] {
    std::vector<int> v(5);

    expect((5_ul == std::size(v)) >> fatal);

    should("resize bigger") = [v] { // or "resize bigger"_test
        mut(v).resize(10);
        expect(10_ul == std::size(v));
    };

    expect((5_ul == std::size(v)) >> fatal);

    should("resize smaller") = [=]() mutable { // or "resize smaller"_test
        v.resize(0);
        expect(0_ul == std::size(v));
    };
};

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
using namespace boost::ut::bdd;
"vector"_test = [] {
    given("I have a vector") = [] {
        std::vector<int> v(5);
        expect((5_ul == std::size(v)) >> fatal);

        when("I resize bigger") = [=] {
            mut(v).resize(10);

            then("The size should increase") = [=] {
                expect(10_ul == std::size(v));
            };
        };
    };
};

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
feature("vector") = [] {
    scenario("size") = [] {
        given("I have a vector") = [] {
            std::vector<int> v(5);
            expect((5_ul == std::size(v)) >> fatal);

            when("I resize bigger") = [=] {
                mut(v).resize(10);

                then("The size should increase") = [=] {
                    expect(10_ul == std::size(v));
                };
            };
        };
    };
};

1
2
3
4
5
for (auto i : std::vector{1, 2, 3}) {
    test("parameterized " + std::to_string(i)) = [i] { // 3 tests
        expect(that % i > 0);                          // 3 asserts
    };
}

1
2
3
"args"_test = [](const auto &arg) {
    expect(arg > 0_i) << "all values greater than 0";
} | std::vector{1, 2, 3};

1
2
3
4
5
6
7
8
"vector"_test = [] {
    std::vector<int> v(5);
    expect((5_ul == std::size(v)) >> fatal);
    "samesize"_test = [&] {
        std::vector<int> v2(5);
        expect(eq(v.size(), v2.size()));
    };
};

1
2
3
4
5
"logging"_test = [] {
    log << "pre";
    expect(42_i == 43) << "message on failure";
    log << "post";
};

Share Snippets