Test helpers should be simple.
You don't want to have to worry about bugs in your test suite or unexpected behaviour leading
to failing tests silently passing. With that in mind, simple_test_case aims to do the bare
minimum to eliminate the boilerplate of writing parameterised tests and no more.
The test_case attribute macro handles generating multiple test functions for you which are
parameterised by the inputs you provide. You still need to provide the #[test] attribute (or
an alternative such as #[tokio::test]) and all test cases must be provided before any
additional attribute macros you wish to apply.
And that's it.
There is no additional support for custom assertions, fixtures etc. That said, if you want or
need a more complicated testing set up, additional attribute macros should play nice with
simple_test_case provided you follow the advice below.
Usage
Valid
Here the #[test] attribute is provided after all instances of test_case. This will work.
use test_case;
Invalid
Here the #[test] attribute is provided before all instances of test_case. This will cause
the compiler to complain about functions used as tests not being allowed to have any arguments.
use test_case;
Additional attributes
test_case preserves all attributes beneath it, forwarding them on to the individual generated
test functions. As an example, the standard library should_panic attribute works just fine as
shown below (just make sure to provide your test cases first as described above):
use test_case;
Async tests
Async tests are supported in the same way that all other attributes are supported: add your tests cases first and then apply the async testing macro of your choice beneath.
use test_case;
async
async
How does it work?
You are encouraged to read the source of the macro itself (the macro plus associated helper functions are under 150 lines of code) but the general idea is as follows:
- Collect all
test_case(orsimple_test_case::test_case) attributes, each of which maps a set of function arguments to a test case name. - For each test case create a copy of the original test function with the function arguments replaced with explicit variable bindings at the top of the function body.
- Write out each of the cases as their own test inside of a new module that is named using the original test function name.
You can use cargo expand to see what the generated
tests look like using the example provided in the examples directory like so:
)
)
use *;
extern crate std;
use test_case;