Generate tests at compile-time based on files and directories.
This crate contains three attributes that all generate tests based on a file glob pattern. Each attribute generates tests with different argument types. The generated tests will be named after sanitized versions of the file names.
Receive file contents as &'static str
with test_each::file
. This ignores any matched directories.
```rust
fn test_file(content: &str) { // check contents } ```
If data
contains the files foo.txt
and bar.txt
, the following code will be generated:
```rust
fn testfilefoo() { testfile(includestr("data/foo.txt")) }
fn testfilebar() { testfile(includestr("data/bar.txt")) } ```
Receive file contents as &'static [u8]
with test_each::blob
. This ignores any matched directories.
```rust
fn test_bytes(content: &[u8]) { // check contents } ```
Declare a second parameter in order to additionally receive the path of file.
```rust
fn test_bytes(content: &[u8], path: PathBuf) { // check contents and path } ```
Receive file path as PathBuf
with test_each::path
. This includes any matched directories.
```rust
fn test_bytes(path: PathBuf) { // check path } ```
By default the name of the generated test will consist of the escaped file name without extension. Use the name
attribute to change how the function names are formatted.
Use name(segments = <n>)
to add n
amount of path segments (from right to left) to the name.
Use name(index)
to add a unique index to the end of the test name. This will prevent name collisions.
Use name(extension)
to include the file extension the end of the test name.
``rust
/// The generated function name will be
testfilebarbazdatatxt0`
fn testfile(: &str) {} ```
Any change to an already included file will correctly trigger a recompilation, but creating a new file that matches the glob might not cause a recompilation.
To fix this issue add a build file that emits cargo-rerun-if-changed={<glob directories>}
.