8 releases (breaking)

0.8.0 Jul 20, 2023
0.7.0 May 9, 2023
0.6.0 Jun 29, 2021
0.5.0 Jan 19, 2021
0.1.0 May 29, 2020

#118 in Testing

Download history 12842/week @ 2024-08-15 13189/week @ 2024-08-22 12118/week @ 2024-08-29 13556/week @ 2024-09-05 9693/week @ 2024-09-12 11167/week @ 2024-09-19 12711/week @ 2024-09-26 13691/week @ 2024-10-03 12473/week @ 2024-10-10 16181/week @ 2024-10-17 13867/week @ 2024-10-24 13047/week @ 2024-10-31 12475/week @ 2024-11-07 13977/week @ 2024-11-14 16231/week @ 2024-11-21 10690/week @ 2024-11-28

55,664 downloads per month
Used in 6 crates

Apache-2.0

29KB
670 lines

datadriven

datadriven is a port of the Go datadriven library originally written by Andy Kimball.

It's a tool for writing table-driven tests in Rust, with rewrite support.

Usage

A test file looks like this:

eval
1 + 1
----
2
  • eval here is the directive, which describes what kind of test is being run.
  • 1 + 1 is the input.
  • ---- is the separator between input and output.
  • 2 is the expected output.

If this file is in tests/testdata relative to the crate root, it can be processed by having a test like this:

use datadriven::walk;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn run() {
        walk("tests/testdata", |f| {
            f.run(|test_case| -> String {
                // Do something with `s` and return it.
                test_case.input.to_string()

                // Can access the directive via `test_case.directive`.
            })
        });
    }
}

The closure passed to walk will be evaluated for each file in the given directory (or just a single file), and the closure passed to f.run will be evaluated for each test case in that file. Test cases can share state by closing over values in the walk closure.

Rewriting

If the env var REWRITE is set, the results will all be rewritten to match the expectation.

Running specific tests

If the env var RUN is set, its value will be appended to the directory passed to walk.

Multiline output

If the output for a test case has blank lines, that can be expressed by enclosing the entire output in a double-up of ----:

render
foo\n\nbar
----
----
foo

bar
----
----

Arguments

Strings can be passed as arguments to tests in a directive line.

render a=world
hello $a
----
hello world

Arguments can be accessed from the args field on TestCase.

They are actually Vecs of strings:

render a=(one,two)

will have the vector

vec!["one".to_string(), "two".to_string()]

Dependencies

~220–800KB
~18K SLoC