Test cases
Test cases are named sequences of checks. More...
Classes Index
| class | test_case |
|
A named, runnable test case that lives inside a suite. More... | |
Description
Test cases are named sequences of checks.
A test case is created with test(), called on a suite, with a name and a callable, usually a lambda that receives the test case ([] (auto& t) { ... }), followed by any arguments for the callable. The test case runs immediately, within the call, and its checks are made on the test case received by the lambda (t.expect (...)).
Write one test case for each behaviour of the code under test; the report shows the result of each test case, so a failure points to the behaviour that broke.
A test case that makes no check passes, and is reported as passed (0 checks); a body that forgets its checks is not detected, so make at least one in each test case.
An exception that escapes the body of a test case is caught, and reported as a failed check of that test case, with the text of its what() (unexpected exception: ..., or unexpected exception of unknown type); the test case ends, and the run continues with the next one. Nested test cases catch their own exceptions, so the exception is reported by the innermost test case whose body it escapes. An exception that escapes the body of a suite fails that suite in the same way. Without exceptions, nothing can be thrown, and this does not apply.
A test case can contain other test cases, created with test() on the test case itself (t.test (...)); they are reported nested.
- Example
- Test cases for several values
test_each(), called on a suite or on a test case, creates and runs a test case for each value of a range (a container, an array, a span, or a view), of a list written in place, or of a tuple, whose values may have different types. The callable receives the test case and the value, and a generic lambda is needed for a tuple. Each test case is named after the common name, followed by the value in parentheses, such as Sizes (16); a value that cannot be written, such as an application type without an operator<<, is replaced by its position, starting at 1, such as Records (#2).
With the TAP reporter, the first call is reported as:
Generated via doxygen2docusaurus 2.2.8 by Doxygen 1.18.0.