|
halcheck 1.0
|
Property-Based Testing checks whether a system behaves correctly for wide range of automatically generated inputs. Property-based testing provides the following benefits over standard testing techniques:
To illustrate the advantages of property-based testing, consider this implementation of binary search:
Is binary_search implemented correctly? We can write a few simple example-based tests:
Code coverage tools will confirm that the above test executes every line of code in binary_search. Nonetheless, binary_search does have a bug. This bug is revealed by running by a simple property-based test:
The above test reveals that binary_search({0, 1}, 0) returns false instead of true. Fixing this bug is left as an exercise to the reader.
Property-based tests require three things:
In the above example, the system under test is the function binary_search. There are two generators, marked by (a) and (b). Finally, the oracle is marked by (c) [1].
The system under test is usually given, so the main challenge of property-based testing lies in devising generators and properties. Property-based testing frameworks (such as halcheck) primarily support test execution and writing generators (see the generator reference.)
[1] Properties need not be explicitly asserted. Sometimes you just want to test that a piece of code does not crash.