Crates.io | fixtures |
lib.rs | fixtures |
version | 2.1.0 |
created_at | 2023-09-17 16:26:32.178834+00 |
updated_at | 2025-09-18 17:30:39.322734+00 |
description | Run tests against fixtures |
homepage | https://github.com/bcheidemann/fixtures-rs |
repository | https://github.com/bcheidemann/fixtures-rs |
max_upload_size | |
id | 975213 |
size | 28,013 |
fixtures
is a Rust crate which allows developers to run tests against fixture files.
[dependencies]
fixtures = "1"
[build-dependencies]
fixtures = "1"
Add the following code to build.rs
to watch your fixtures directories for changes.
// build.rs
use fixtures::build::watch_fixture_dir;
fn main() {
watch_fixture_dir("path/to/fixtures");
// or...
watch_fixture_dir(&[
"path/to/fixtures",
// ...
]);
}
#[cfg(test)]
mod tests {
use fixtures::fixtures;
#[fixtures(["fixtures/*.txt"])]
#[test]
fn test(path: &std::path::Path) {
// This test will be run once for each file matching the glob pattern
}
}
The above example expands to:
#[cfg(test)]
mod tests {
use fixtures::fixtures;
fn test(path: &std::path::Path) {
// This test will be run once for each file matching the glob pattern
}
#[test]
fn test_one_dot_txt_1() {
test(::std::path::Path::new("fixtures/one.txt"));
}
#[test]
fn test_two_dot_txt_1() {
test(::std::path::Path::new("fixtures/two.txt"));
}
// ...
}
#[cfg(test)]
mod tests {
use fixtures::fixtures;
#[fixtures(["fixtures/*.txt", "fixtures/*.data"])]
#[test]
fn test(path: &std::path::Path) {
// This test will be run once for each file matching either "fixtures/*.txt" or "fixtures/*.data"
}
}
fixtures
supports gitignore
's extended glob syntax.
#[cfg(test)]
mod tests {
use fixtures::fixtures;
#[fixtures(["fixtures/*.{txt,data}", "!fixtures/skip.*.{txt,data}"])]
#[test]
fn test(path: &std::path::Path) {
// This test will be run once for each fixture with the extension `txt` or `data`, unless it is prefixed with `skip.`
}
}
fixtures
can be used with criterion
as shown in the following example:
#[fixtures(["fixtures/bench/*"])]
fn bench(path: &std::path::Path, c: &mut Criterion) {
let test_name = fixture_path.file_name().unwrap().to_str().unwrap();
c.bench_function(test_name, |b| b.iter(|| { /* ... */ }));
}
// Equivalent to criterion_group!(benches, bench::expansion_1, bench::expansion_2, ...);
fn benches() {
let mut c = Criterion::default().configure_from_args();
for bench in bench::EXPANSIONS {
bench(&mut criterion);
}
}
criterion_main!(benches);