fixtures
fixtures is a Rust crate which allows developers to run tests against fixture files. It provides a procedural macro
to generate tests from the filesystem, using glob patterns.
Example
To ensure tests re-run when the fixtures change, add the following line to build.rs.
watch_dir;
Or, if you prefer to avoid adding build dependencies to your project:
println!;
Comparison to datatest and datatest-stable
| fixtures | datatest | datatest-stable | |
|---|---|---|---|
| Supports stable rust | 🏅 yes | no | 🏅 yes |
Requires setting harness = false in Cargo.toml |
🏅 no | no | yes |
Supports non-test configurations e.g. criterion |
🏅 yes | no | no |
| Supports embedding directories at compile time | no | no | 🏅 yes |
Works with cargo-nextest |
🏅 yes | no | 🏅 yes |
| Supports arbitrary function signatures | 🏅 yes | no | no |
| Supports automatically injecting file contents | no | 🏅 yes | 🏅 yes |
Allows #[ignore]ing tests by glob patterns |
🏅 yes | 🏅 yes | no |
Usage
Installation
[]
= "2"
[]
= "2"
Setup
Add the following code to build.rs to watch your fixtures directories for changes.
Basic Usage
use fixtures;
The above example expands to:
use fixtures;
Multiple Globs
Extended Glob Syntax
fixtures supports gitignore's extended glob syntax.
Advanced Usage
Ignoring Files
Sometimes, you might want to ignore tests for one or more fixture files. If you want to skip generating the test
entirely, you can simply use a negative glob, as discussed above. However, if you instead want to #[ignore] the test,
you can do so as follows:
In some cases you may wish to provide a reason for ignoring the test case.
This feature can be used in combination with the cfg_attr macro to conditionally exclude tests only for certain
configurations:
This feature is only available for test functions; those with a #[test] attribute.
Note that the ignore glob will not be used to include files. This means that, for example, the ignore glob shown below
would have no effect, since none of the files matched by the include glob, are matched by the ignore glob.
// This won't work!
This can be fixed as shown in the following example.
// This works as expected 🥳
Criterion
fixtures can be used with criterion as shown in the following example:
// Equivalent to criterion_group!(benches, bench::expansion_1, bench::expansion_2, ...);
criterion_main!;