datafile-test

Crates.iodatafile-test
lib.rsdatafile-test
version0.1.1
created_at2025-02-01 15:07:14.448589+00
updated_at2025-05-24 04:54:49.839841+00
descriptionGenerate test codes for data-file (JSON, YAML)
homepage
repositoryhttps://github.com/lambdalisue/rs-datafile-test
max_upload_size
id1538498
size21,334
Λlisue (lambdalisue)

documentation

README

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.

Commit count: 15

cargo fmt