dir-test
provides a macro to generate single test cases from files in a directory.
Add the following dependency to your Cargo.toml
.
toml
[dev-dependencies]
dir-test = "0.1"
```rust, norun use dirtest::{dir_test, Fixture};
dir: "$CARGO_MANIFEST_DIR/fixtures",
glob: "**/*.txt",
)] fn test(fixture: Fixture<&str>) { // The file content and the absolute path of the file are available as follows. let content = fixture.content(); let path = fixture.path();
// Write your test code here.
// ...
} ```
Assuming your crate is as follows, then the above code generates two test
cases foo()
and fixtures_a_bar()
.
text
my-crate/
├─ fixtures/
│ ├─ foo.txt
│ ├─ fixtures_a/
│ │ ├─ bar.txt
├─ src/
│ ├─ ...
│ ├─ lib.rs
├─ Cargo.toml
├─ README.md
🔽
```rust, no_run
fn foo() { test(fixture); }
fn fixturesabar() { test(fixture); } ```
NOTE: The dir
argument must be specified in an absolute path because
of the limitation of the current procedural macro system. Consider using
environment variables, dir-test
crate resolves environment variables
internally.
You can specify a custom loader function to load the file content from the
file path. The loader will be passed &'static str
file path and can return
an arbitrary type.
```rust, norun
use dirtest::{dir_test, Fixture};
dir: "$CARGO_MANIFEST_DIR/fixtures",
glob: "**/*.txt",
loader: std::fs::read_to_string,
)]
fn test(fixture: Fixture
// ...
} ```
If the loader function is not specified, the default content type is
&'static str
.
### Custom Test Name
Test names can be customized by specifying the postfix
argument.
postfix
is appended to the test name.
```rust, norun use dirtest::{dir_test, Fixture};
dir: "$CARGO_MANIFEST_DIR/fixtures",
glob: "**/*.txt",
postfix: "custom", // `_custom` is appended to the test name.
)]
fn test(fixture: Fixture
### Test Attributes
Test attributes can specified by the dir_test_attr
attribute. The
attributes inside dir_test_atrr
are applied to the all generated test.
```rust, norun use dirtest::{dir_test, Fixture};
dir: "$CARGO_MANIFEST_DIR/fixtures",
glob: "**/*.txt",
)]
#[wasm_bindgen_test]
#[cfg(target_family = "wasm")]
)]
fn wasm_test(fixture: Fixture
NOTE: The dir_test_attr
attribute must be specified after the
dir_test
.