https://github.com/nathanielsimard/table-test
Improve table testing in Rust.
https://github.com/nathanielsimard/table-test
rust rust-lang testing
Last synced: about 1 year ago
JSON representation
Improve table testing in Rust.
- Host: GitHub
- URL: https://github.com/nathanielsimard/table-test
- Owner: nathanielsimard
- License: mit
- Created: 2018-02-23T01:38:58.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-04-09T18:19:10.000Z (about 8 years ago)
- Last Synced: 2025-04-14T21:08:31.467Z (about 1 year ago)
- Topics: rust, rust-lang, testing
- Language: Rust
- Homepage:
- Size: 927 KB
- Stars: 10
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Table Test
[](https://travis-ci.org/nathanielsimard/table-test)
[](https://codecov.io/gh/nathanielsimard/table-test)
[](https://crates.io/crates/table-test)
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]`.
```toml
[dev-dependencies]
table-test = "0.2.1"
```
```rust
#[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:
```rust
#[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:

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](examples/mutable_struct.rs) test the changing name method and the result looks like this:

More examples can be found in the examples folder.