Attribute Macro test_generator::test_resources

source ·
#[test_resources]
Expand description

Macro generating test-functions, invoking the fn for each item matching the resource-pattern.

The resource-pattern must not expand to empty list, otherwise an error is raised. The generated test-functions is aregular tests, being compiled by the rust-compiler; and being executed in parallel by the test-framework.

#[cfg(test)]
extern crate test_generator;

#[cfg(test)]
mod tests {
  use test_generator::test_resources;

  #[test_resources("res/*/input.txt")]
  fn verify_resource(resource: &str) {
     assert!(std::path::Path::new(resource).exists());
  }
}

Assuming the following package layout with test file mytests.rs and resource folder res/, the output below will be printed on console. The functionality of build.rs is explained at crate build-deps and demonstrated with example

├── build.rs
├── Cargo.toml
├── res
│   ├── set1
│   │   ├── expect.txt
│   │   └── input.txt
│   ├── set2
│   │   ├── expect.txt
│   │   └── input.txt
│   └── set3
│       ├── expect.txt
│       └── input.txt
├── src
│   └── main.rs
├── benches
│   └── mybenches.rs
└── tests
    └── mytests.rs

Producing the following test output

$ cargo test

running 3 tests
test tests::verify_resource_res_set1_input_txt ... ok
test tests::verify_resource_res_set2_input_txt ... ok
test tests::verify_resource_res_set3_input_txt ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out