Macro credibility::test_block [−][src]
macro_rules! test_block { ($block:ident, $tracker:ident, $name:expr, $code:block) => { ... }; ($block:ident, $name:expr, $code:block) => { ... }; }
Create a TestBlock
valid for a block of code.
test_block
is a convenience macro (that's very convenient!) for
running tests in bulk and causing a test abort based on their
results once the block terminates. The block of code runs in a
closure defined to return a Result
, so within the block, ?
and
try!
work correctly.
Handling Result
Since test_block!
executes code inside a closure that returns a
Result, test code can use ?
within that block, to short-circuit
error handling without unsightly .unwrap()
calls. The
unfortunate consequence of this is that code blocks within
test_block!
macros must return Ok(())
at the end.
Teardown behavior
The behavior at the end of the block depends on the
TestReporter
used; the default form
of this macro creates a
DefaultTestReporter
, which
panics at the end of the block if any errors occur, or if the code
block returns a non-Ok
result.
Use the form of this macro that takes an additional
TestReporter
argument to customize
this behavior; see the module selftest
for an example.
Examples
A functioning example of a table test:
test_block!(tb, "An example test block", { let cases = vec![ (2, 3, 5), (1, 1, 2), (1, 1, 2), ]; for (in1, in2, output) in cases { aver_eq!(tb, output, in1+in2); } Ok(()) });
An example of how to handle error results in tests:
test_block!(tb, "An example test block that should fail", { fail() });