Crates.io | datafile-test |
lib.rs | datafile-test |
version | 0.1.1 |
created_at | 2025-02-01 15:07:14.448589+00 |
updated_at | 2025-05-24 04:54:49.839841+00 |
description | Generate test codes for data-file (JSON, YAML) |
homepage | |
repository | https://github.com/lambdalisue/rs-datafile-test |
max_upload_size | |
id | 1538498 |
size | 21,334 |
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.
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
}
]
The code follows the MIT license written in LICENSE. Contributors need to agree that any modifications sent to this repository follow the license.