Skip to content

Commit

Permalink
add example for rstest (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanganto authored Jun 7, 2024
1 parent fa76718 commit 0b87e9f
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
76 changes: 76 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ executable = ["which"]
tokio = { version = "1.15.0", features = ["rt", "macros"] }
serial_test = "2.0.0"
libtest-with = { version = "0.7.0-0", features = ["net", "resource", "user", "executable"] }
rstest = "0.21.0"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Rust version `1.61` of stable channel or `2022-03-30` of nightly channel will sh
If the ignore message does not show in the previous Rust version you used, the feature `ign-msg` can be used to work around.
and the name of ignored test case will be rewritten, such that you can easier to know why the test is ignored.

The order of test macros(`#[test]`, `#[tokio::test]`, `#[serial_test::serial]`, ...) is important, please check out examples.
The order of test macros(`#[test]`, `#[tokio::test]`, `#[serial_test::serial]`, `#[rstest]`...) is important, please check out examples.

## Environment Variable
Run test case when the environment variable is set.
Expand Down
22 changes: 22 additions & 0 deletions examples/rstest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use rstest::rstest;

fn main() {}

#[test_with::env(NOTHING)]
#[rstest]
#[case(0, 0)]
#[case(1, 1)]
#[case(2, 1)]
#[case(3, 2)]
#[case(4, 3)]
fn fibonacci_test(#[case] input: u32, #[case] expected: u32) {
assert_eq!(expected, fibonacci(input))
}

fn fibonacci(input: u32) -> u32 {
match input {
0 => 0,
1 => 1,
n => fibonacci(n - 2) + fibonacci(n - 1),
}
}

0 comments on commit 0b87e9f

Please sign in to comment.