#directory #testing #test-cases #test-macro #macro #cargo-toml

dir-test

Provides a macro to generate tests from files in a directory

7 unstable releases (3 breaking)

0.4.1 Dec 4, 2024
0.4.0 Nov 22, 2024
0.3.0 May 16, 2024
0.2.1 Mar 2, 2024
0.1.1 Feb 4, 2023

#158 in Filesystem

Download history 690/week @ 2024-08-21 560/week @ 2024-08-28 680/week @ 2024-09-04 679/week @ 2024-09-11 461/week @ 2024-09-18 560/week @ 2024-09-25 622/week @ 2024-10-02 449/week @ 2024-10-09 964/week @ 2024-10-16 1441/week @ 2024-10-23 2021/week @ 2024-10-30 2048/week @ 2024-11-06 1792/week @ 2024-11-13 2185/week @ 2024-11-20 2062/week @ 2024-11-27 2494/week @ 2024-12-04

8,793 downloads per month
Used in 2 crates

Apache-2.0

12KB
51 lines

CI Crates.io License

dir-test provides a macro to generate single test cases from files in a directory.

Usage

Add the following dependency to your Cargo.toml.

[dev-dependencies]
dir-test = "0.4"

Basic Usage

use dir_test::{dir_test, Fixture};

#[dir_test(
    dir: "$CARGO_MANIFEST_DIR/fixtures",
    glob: "**/*.txt",
)]
fn mytest(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 mytest__foo() and mytest__fixtures_a_bar().

my-crate/
├─ fixtures/
│  ├─ foo.txt
│  ├─ fixtures_a/
│  │  ├─ bar.txt
├─ src/
│  ├─ ...
│  ├─ lib.rs
├─ Cargo.toml
├─ README.md

🔽

#[test]
fn mytest__foo() {
    mytest(fixture);
}

#[test]
fn mytest__fixtures_a_bar() {
    mytest(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.

Custom Loader

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.

use dir_test::{dir_test, Fixture};

#[dir_test(
    dir: "$CARGO_MANIFEST_DIR/fixtures",
    glob: "**/*.txt",
    loader: std::fs::read_to_string,
)]
fn test(fixture: Fixture<std::io::Result<String>>) {
    let content = fixture.content().unwrap();

    // ...
}

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.

use dir_test::{dir_test, Fixture};

#[dir_test(
    dir: "$CARGO_MANIFEST_DIR/fixtures",
    glob: "**/*.txt",
    postfix: "custom", // `_custom` is appended to the test name.
)]
fn test(fixture: Fixture<std::io::Result<String>>) {
    // ...
}

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.

use dir_test::{dir_test, Fixture};

#[dir_test(
    dir: "$CARGO_MANIFEST_DIR/fixtures",
    glob: "**/*.txt",
)]
#[dir_test_attr(
    #[wasm_bindgen_test]
    #[cfg(target_family = "wasm")]
)]
fn wasm_test(fixture: Fixture<std::io::Result<String>>) {
    // ...
}

NOTE: The dir_test_attr attribute must be specified after the dir_test.

Return Types

Tests may have a return type, allowing for the [Result<T, E>] type to be used in the test. See the relevant book link here.

use dir_test::{dir_test, Fixture};

#[dir_test(
    dir: "$CARGO_MANIFEST_DIR/fixtures",
    glob: "**/*.txt",
)]
fn test(fixture: Fixture<&str>) -> std::io::Result<()> {
    // ...
}

Dependencies

~255–690KB
~16K SLoC