|
Hegel 0.18.1
Property-based testing for C++
|
Hegel is a property-based testing library for C++. Hegel is based on Hypothesis, using the libhegel engine.
This guide walks you through the basics of installing Hegel and writing your first tests.
Using CMake:
Hegel requires CMake 3.14 and, by default, a C++20 compiler. The library lives in the hegeldev/hegel repository next to libhegel, Hegel's native engine, which the build compiles with cargo (a Rust toolchain from https://rustup.rs). To link a libhegel built elsewhere instead, so that no Rust toolchain is needed, configure with -DHEGEL_LIBHEGEL_LIBRARY=/path/to/libhegel_c.<ext>.
To consume Hegel from C++17, configure with -DHEGEL_REFLECTION=OFF. This drops the reflect-cpp dependency: you lose default_generator (type-directed derivation for structs), but every other generator and combinator still works. (The designated-initializer parameter API, e.g. integers<int>({.min_value = 0}), then relies on a GCC/Clang C++17 extension.)
You're now ready to write your first test. In a new file:
Now build and run the test. You should see that this test passes.
Let's look at what's happening in more detail. HEGEL_TEST defines a property test as an ordinary function, self_equality. Running the test executes your test body 100 times by default. While you may call the function from main(), if you use a test framework, write the property inside one of its tests instead. We officially support gtest (see Hegel and test frameworks). Please make an issue on Github if Hegel does not integrate well with your framework.
The body receives a TestCase, which provides a TestCase::draw() method for drawing different values. This test draws a random integer and checks that it should be equal to itself. The macro also names the test in Hegel's example database, so a failure found in one run is replayed first in the next.
The name you pass to TestCase::draw() labels the value in the failure report, so a failing run replays each drawn value under the variable it was assigned to (e.g. auto n = 42). Give each draw the name of its variable. TestCase::draw(gen) without a name prints numbered placeholders (auto draw_1 = ...;) instead.
Next, try a test that fails:
This test asserts that any integer is less than 50, which is obviously incorrect. Hegel will find a test case that makes this assertion fail, and then shrink it to find the smallest counterexample. It reports:
The report shows the falsifying value(s), then the line that replays that exact failure (see HEGEL_REPRODUCE_FAILURE). The test's own exception, n should be below 50, then propagates out of below_50().
To fix this test, you can constrain the integers you generate with the min_value and max_value parameters:
Run the test again. It should now pass.
Hegel provides a rich library of generators in the hegel::generators namespace that you can use out of the box. There are primitive generators, such as integers, floats, and text, and combinators that allow you to make generators out of other generators, such as vectors and tuples.
For example, you can use vectors to generate a vector of integers:
This test checks that appending an element to a random vector of integers should always increase its length.
You can also define custom generators. For example, say you have a Person struct that we want to generate:
Note that you can feed the results of a draw to subsequent calls. For example, say that you extend the Person struct to include a driving_license boolean field:
Hegel can also derive generators automatically for reflectable structs via default_generator. This uses reflect-cpp to inspect the struct's fields and pick an appropriate generator for each:
Call .override(...) on the returned generator to customize individual fields (see override).
A test body states what must hold by throwing when it does not. Any exception fails the test case, and Hegel then shrinks the values that caused it:
The assertion macros of a test framework work too. See Hegel and test frameworks for GoogleTest.
Hegel groups counterexamples by origin to tell one bug from another. The origin of a thrown exception is its type and the site it was thrown from. Derive the exception from FailureOrigin to group them some other way, but the origin must be stable.
Drawn values print automatically in the failure report (auto x = ...;). Use TestCase::note to add whatever context the values alone do not show:
Notes and drawn values print on the failing replay only. Raise Settings::verbosity to Verbosity::Verbose to see them for every case.
The generator prints each value as it draws it, so a container prints each element as the element's generator prints it. TestCase::draw takes a printable hegel::generators::Generator. A generator of a type with no C++ representation (no print_to(hegel::Printer&) const member, no operator<<, and not a reflectable aggregate) is a hegel::generators::AnyGenerator. Give it a printer, or draw it with TestCase::draw_silent:
A generator of your own implements hegel::generators::IPrintableGenerator and writes its value to a hegel::Printer.
By default Hegel runs 100 test cases. To override this, write a Settings initializer after the test name:
These settings are the test function's default argument; passing a Settings when invoking the test (self_equality({.test_cases = 5})) replaces them for that run.
To run a test with values you choose, before Hegel generates any, add an explicit test case with HEGEL_EXPLICIT_TEST_CASE. Its values go to the test's draws: positional values in draw order, and hegel::named() values to the draw of that name.
With hegel::test(), pass the cases as a list of hegel::ExplicitTestCase.
With a test framework, write the property inside one of its tests and call hegel::test() with the body. You cannot use HEGEL_TEST there.
GoogleTest then reports the test failed with the hegel::GTestFailure that holds the failed assertion (arithmetic_test.cpp:11: Expected: (x + y) == (y + x), actual: 51 vs 0).
Outside a test framework, use HEGEL_TEST, since it names the test for you. hegel::test() can still be used: it derives the database key from the function that calls it, and you can pass a TestLocation to name the test.
HEGEL_MAIN defines a Hegel test as a standalone binary that runs one test case each time it is invoked, for example as an Antithesis workload:
Inside Antithesis, Hegel uses its workload settings profile.