1 unstable release

0.1.0 Feb 1, 2025

#352 in Procedural macros

Download history 54/week @ 2025-01-26 74/week @ 2025-02-02

128 downloads per month

MIT license

10KB
123 lines

crates.io docs.rs MIT License Build Test Audit

📃 datafile-test

This crate provides a macro for data file driven test. The macro generates a test function for each entry in the data file and runs the test function for each entry.

Usage

Use datafile_test attribute macro to define a test function. Note that this macro requires the serde and serde_json crate to be available.

use datafile_test::datafile_test;

#[derive(Debug, serde::Deserialize)]
struct TestCaseInput {
    a: i32,
    b: i32,
}

#[derive(Debug, serde::Deserialize)]
struct TestCase {
    input: TestCaseInput,
    output: i32,
}

#[datafile_test("tests/datafile_test.json")]
fn test_addition(test_case: TestCase) {
    assert_eq!(test_case.input.a + test_case.input.b, test_case.output);
}

This code will generate a test function for each entry in the data file tests/datafile_test.json. The data file should be a JSON or YAML file containing an array of objects like this:

[
    {
        "input": {
            "a": 1,
            "b": 2
        },
        "output": 3
    },
    {
        "input": {
            "a": 2,
            "b": 3
        },
        "output": 5
    }
]

License

The code follows the MIT license written in LICENSE. Contributors need to agree that any modifications sent to this repository follow the license.

Dependencies

~2.3–3.5MB
~71K SLoC