Attribute Macro datafile_test

Source
#[datafile_test]
Expand description

Define data-file-driven tests using JSON/YAML files.

This attribute macro reads a JSON/YAML file at compile time and generates a test function for each test case in the file. The test function must take a single argument, which is a structured type that implements serde::Deserialize. The file is read from the file system relative to the current working directory of the compiler.

Note that serde and serde_json crate is required in caller’s Cargo.toml.

§Example

use datafile_test::datafile_test;

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

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

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

The yaml file should look like this:

- input:
    a: 1
    b: 2
  expect: 3
- input:
    a: 2
    b: 3
  expect: 5