4 releases

Uses old Rust 2015

0.2.1 Apr 9, 2018
0.2.0 Mar 6, 2018
0.1.1 Mar 1, 2018
0.1.0 Mar 1, 2018

#691 in Testing

Download history 75/week @ 2024-03-24 63/week @ 2024-03-31 30/week @ 2024-04-07 22/week @ 2024-04-14 28/week @ 2024-04-21 14/week @ 2024-04-28 27/week @ 2024-05-05 22/week @ 2024-05-12 23/week @ 2024-05-19 56/week @ 2024-05-26 42/week @ 2024-06-02 117/week @ 2024-06-09 49/week @ 2024-06-16 26/week @ 2024-06-23 20/week @ 2024-06-30 93/week @ 2024-07-07

193 downloads per month
Used in 8 crates (5 directly)

MIT license

230KB
413 lines

Table Test

Build Status codecov.io Current Crates.io Version

This library aims to make table testing reliable in Rust. The main problem of table testing with basic Rust is assert_eq! calling panic!. It means that when an assertion fails, then the rest of the test function is not executed. In the case of a table test, it will result with potentially multiple use cases untested, making the output of that test unreliable.

Usage

Specify this crate as [dev-dependencies].

[dev-dependencies]
table-test = "0.2.1"
#[cfg(test)] // <-- not needed in integration tests
#[macro_use]
extern crate table_test;

The table iterator returns a tuple (test_case, input, expected). If you have more than one input, just use a tuple of inputs. The test_case allows you to add comments like given when and then, but also description and custom giving you the freedom to log your tests the best way possible.

Examples

If we make a simple test for an add function that takes two values as input:

#[test]
fn test_add() {
    let table = vec![
        ((1, 2), 3),
        ((2, 5), 7),
        ((0, 0), 0),
        ((0, 1), 1),
        ((2, 2), 4),
    ];

    for (validator, (input_1, input_2), expected) in table_test!(table) {
        let actual = add(input_1, input_2);

        validator
            .given(&format!("{}, {}", input_1, input_2))
            .when("add")
            .then(&format!("it should be {}", expected))
            .assert_eq(expected, actual);
    }
}

If we make a mistake in the implementation of the add function and multiplying instead, then the output will look like this:

multiple inputs

As we can see, it would be easier to debug than with a normal assert_eq! output. But the gain is when we work with something more complex. The example here test the changing name method and the result looks like this:

mutable struct

More examples can be found in the examples folder.

Dependencies

~245KB