| 1 |
[![Crate][crate-image]][crate-link] |
| 2 |
[![Docs][docs-image]][docs-link] |
| 3 |
[![Status][test-action-image]][test-action-link] |
| 4 |
[![Apache 2.0 Licensed][license-apache-image]][license-apache-link] |
| 5 |
[![MIT Licensed][license-mit-image]][license-mit-link] |
| 6 |
|
| 7 |
# Fixture-based test framework for Rust |
| 8 |
|
| 9 |
## Introduction |
| 10 |
|
| 11 |
`rstest` uses procedural macros to help you on writing |
| 12 |
fixtures and table-based tests. To use it, add the |
| 13 |
following lines to your `Cargo.toml` file: |
| 14 |
|
| 15 |
```toml |
| 16 |
[dev-dependencies] |
| 17 |
rstest = "0.27.0" |
| 18 |
``` |
| 19 |
|
| 20 |
### Features |
| 21 |
|
| 22 |
- `async-timeout`: `timeout` for `async` tests (Default enabled) |
| 23 |
- `crate-name`: Import `rstest` package with different name (Default enabled) |
| 24 |
|
| 25 |
### Fixture |
| 26 |
|
| 27 |
The core idea is that you can inject your test dependencies |
| 28 |
by passing them as test arguments. In the following example, |
| 29 |
a `fixture` is defined and then used in two tests, |
| 30 |
simply providing it as an argument: |
| 31 |
|
| 32 |
```rust |
| 33 |
use rstest::*; |
| 34 |
|
| 35 |
#[fixture] |
| 36 |
pub fn fixture() -> u32 { 42 } |
| 37 |
|
| 38 |
#[rstest] |
| 39 |
fn should_success(fixture: u32) { |
| 40 |
assert_eq!(fixture, 42); |
| 41 |
} |
| 42 |
|
| 43 |
#[rstest] |
| 44 |
fn should_fail(fixture: u32) { |
| 45 |
assert_ne!(fixture, 42); |
| 46 |
} |
| 47 |
``` |
| 48 |
|
| 49 |
### Parametrize |
| 50 |
|
| 51 |
You can also inject values in some other ways. For instance, you can |
| 52 |
create a set of tests by simply providing the injected values for each |
| 53 |
case: `rstest` will generate an independent test for each case. |
| 54 |
|
| 55 |
```rust |
| 56 |
use rstest::rstest; |
| 57 |
|
| 58 |
#[rstest] |
| 59 |
#[case(0, 0)] |
| 60 |
#[case(1, 1)] |
| 61 |
#[case(2, 1)] |
| 62 |
#[case(3, 2)] |
| 63 |
#[case(4, 3)] |
| 64 |
fn fibonacci_test(#[case] input: u32, #[case] expected: u32) { |
| 65 |
assert_eq!(expected, fibonacci(input)) |
| 66 |
} |
| 67 |
``` |
| 68 |
|
| 69 |
Running `cargo test` in this case executes five tests: |
| 70 |
|
| 71 |
```bash |
| 72 |
running 5 tests |
| 73 |
test fibonacci_test::case_1 ... ok |
| 74 |
test fibonacci_test::case_2 ... ok |
| 75 |
test fibonacci_test::case_3 ... ok |
| 76 |
test fibonacci_test::case_4 ... ok |
| 77 |
test fibonacci_test::case_5 ... ok |
| 78 |
|
| 79 |
test result: ok. 5 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out |
| 80 |
``` |
| 81 |
|
| 82 |
If you need to just providing a bunch of values for which you |
| 83 |
need to run your test, you can use `#[values(list, of, values)]` |
| 84 |
argument attribute: |
| 85 |
|
| 86 |
```rust |
| 87 |
use rstest::rstest; |
| 88 |
|
| 89 |
#[rstest] |
| 90 |
fn should_be_invalid( |
| 91 |
#[values(None, Some(""), Some(" "))] |
| 92 |
value: Option<&str> |
| 93 |
) { |
| 94 |
assert!(!valid(value)) |
| 95 |
} |
| 96 |
``` |
| 97 |
|
| 98 |
Or create a _matrix_ test by using _list of values_ for some |
| 99 |
variables that will generate the cartesian product of all the |
| 100 |
values. |
| 101 |
|
| 102 |
#### Use Parametrize definition in more tests |
| 103 |
|
| 104 |
If you need to use a test list for more than one test you can use [`rstest_reuse`][reuse-crate-link] |
| 105 |
crate. With this helper crate you can define a template and use it everywhere. |
| 106 |
|
| 107 |
```rust |
| 108 |
use rstest::rstest; |
| 109 |
use rstest_reuse::{self, *}; |
| 110 |
|
| 111 |
#[template] |
| 112 |
#[rstest] |
| 113 |
#[case(2, 2)] |
| 114 |
#[case(4/2, 2)] |
| 115 |
fn two_simple_cases(#[case] a: u32, #[case] b: u32) {} |
| 116 |
|
| 117 |
#[apply(two_simple_cases)] |
| 118 |
fn it_works(#[case] a: u32, #[case] b: u32) { |
| 119 |
assert!(a == b); |
| 120 |
} |
| 121 |
``` |
| 122 |
|
| 123 |
See [`rstest_reuse`][reuse-crate-link] for more details. |
| 124 |
|
| 125 |
#### Feature flagged cases |
| 126 |
|
| 127 |
In case you want certain test cases to only be present if a certain feature is |
| 128 |
enabled, use `#[cfg_attr(feature = …, case(…))]`: |
| 129 |
|
| 130 |
```rust |
| 131 |
use rstest::rstest; |
| 132 |
|
| 133 |
#[rstest] |
| 134 |
#[case(2, 2)] |
| 135 |
#[cfg_attr(feature = "frac", case(4/2, 2))] |
| 136 |
fn it_works(#[case] a: u32, #[case] b: u32) { |
| 137 |
assert!(a == b); |
| 138 |
} |
| 139 |
``` |
| 140 |
|
| 141 |
This also works with [`rstest_reuse`][reuse-crate-link]. |
| 142 |
|
| 143 |
### Magic Conversion |
| 144 |
|
| 145 |
If you need a value where its type implement `FromStr()` trait you can use a literal |
| 146 |
string to build it: |
| 147 |
|
| 148 |
```rust |
| 149 |
# use rstest::rstest; |
| 150 |
# use std::net::SocketAddr; |
| 151 |
#[rstest] |
| 152 |
#[case("1.2.3.4:8080", 8080)] |
| 153 |
#[case("127.0.0.1:9000", 9000)] |
| 154 |
fn check_port(#[case] addr: SocketAddr, #[case] expected: u16) { |
| 155 |
assert_eq!(expected, addr.port()); |
| 156 |
} |
| 157 |
``` |
| 158 |
|
| 159 |
You can use this feature also in value list and in fixture default value. |
| 160 |
|
| 161 |
### Async |
| 162 |
|
| 163 |
`rstest` supports async tests, but makes no assumptions about what |
| 164 |
async runtime you are using. Async tests require either an |
| 165 |
[explicit test attribute](#explicit-test-attribute) or an |
| 166 |
[implicit test attribute](#implicit-test-attribute). |
| 167 |
|
| 168 |
You are responsible for providing any appropriate peer dependencies to run your async tests. |
| 169 |
|
| 170 |
If your test input is an async value (fixture or test parameter) you can use `#[future]` |
| 171 |
attribute to remove `impl Future<Output = T>` boilerplate and just use `T`: |
| 172 |
|
| 173 |
```rust |
| 174 |
use rstest::*; |
| 175 |
#[fixture] |
| 176 |
async fn base() -> u32 { 42 } |
| 177 |
|
| 178 |
#[rstest] |
| 179 |
#[tokio::test] |
| 180 |
#[case(21, async { 2 })] |
| 181 |
#[case(6, async { 7 })] |
| 182 |
async fn my_async_test( |
| 183 |
#[future] base: u32, |
| 184 |
#[case] expected: u32, |
| 185 |
#[future] #[case] div: u32 |
| 186 |
) { |
| 187 |
assert_eq!(expected, base.await / div.await); |
| 188 |
} |
| 189 |
``` |
| 190 |
|
| 191 |
As you noted you should `.await` all _future_ values and this sometimes can be really boring. |
| 192 |
In this case you can use `#[future(awt)]` to _awaiting_ an input or annotating your function |
| 193 |
with `#[awt]` attributes to globally `.await` all your _future_ inputs. Previous code can be |
| 194 |
simplified as follows: |
| 195 |
|
| 196 |
```rust |
| 197 |
use rstest::*; |
| 198 |
# #[fixture] |
| 199 |
# async fn base() -> u32 { 42 } |
| 200 |
#[rstest] |
| 201 |
#[tokio::test] |
| 202 |
#[case(21, async { 2 })] |
| 203 |
#[case(6, async { 7 })] |
| 204 |
#[awt] |
| 205 |
async fn global( |
| 206 |
#[future] base: u32, |
| 207 |
#[case] expected: u32, |
| 208 |
#[future] #[case] div: u32 |
| 209 |
) { |
| 210 |
assert_eq!(expected, base / div); |
| 211 |
} |
| 212 |
|
| 213 |
#[rstest] |
| 214 |
#[tokio::test] |
| 215 |
#[case(21, async { 2 })] |
| 216 |
#[case(6, async { 7 })] |
| 217 |
async fn single( |
| 218 |
#[future] base: u32, |
| 219 |
#[case] expected: u32, |
| 220 |
#[future(awt)] #[case] div: u32 |
| 221 |
) { |
| 222 |
assert_eq!(expected, base.await / div); |
| 223 |
} |
| 224 |
``` |
| 225 |
|
| 226 |
### Files path as input arguments |
| 227 |
|
| 228 |
If you need to create a test for each file in a given location you can use |
| 229 |
`#[files("glob path syntax")]` attribute to generate a test for each file that |
| 230 |
satisfy the given glob path. |
| 231 |
|
| 232 |
```rust |
| 233 |
#[rstest] |
| 234 |
fn for_each_file( |
| 235 |
#[files("src/**/*.rs")] |
| 236 |
#[exclude("test")] path: PathBuf |
| 237 |
) { |
| 238 |
assert!(check_file(&path)) |
| 239 |
} |
| 240 |
``` |
| 241 |
|
| 242 |
The default behavior is to ignore the files that start with `"."`, but you can |
| 243 |
modify this by use `#[include_dot_files]` attribute. The `files` attribute can be |
| 244 |
used more than once on the same variable, and you can also create some custom |
| 245 |
exclusion rules with the `#[exclude("regex")]` attributes that filter out all |
| 246 |
paths that verify the regular expression. |
| 247 |
|
| 248 |
You can pass in environment variables by using `${ENV_VAR_NAME}` in the glob |
| 249 |
path, e.g. `#[files("${SOME_ENV}/hello")]`. To set a default value for the |
| 250 |
environment variable, use `${ENV_VAR_NAME:-default_value}`. |
| 251 |
|
| 252 |
Files are resolved at compile time against your Cargo project root |
| 253 |
(the `CARGO_MANIFEST_DIR` environment variable). If you need to change this |
| 254 |
behavior, you can use the `#[base_dir = "..."]` attribute to specify a different |
| 255 |
base directory. That directory MUST exist, and will be used as the root for |
| 256 |
the files, as well as to resolve the relative path when creating the test name. |
| 257 |
Similar to the `files` attribute, you can use `${ENV_VAR_NAME}` in the `base_dir`. |
| 258 |
|
| 259 |
The `#[files(...)]` attribute ignores matched directory paths by default, |
| 260 |
returning only file paths. If you need the test cases to include directories |
| 261 |
found by the glob pattern, use the `#[dirs]` attribute in conjunction with `#[files(...)]`. |
| 262 |
|
| 263 |
### Use `#[once]` Fixture |
| 264 |
|
| 265 |
If you need to a fixture that should be initialized just once for all tests |
| 266 |
you can use `#[once]` attribute. `rstest` call your fixture function just once and |
| 267 |
return a reference to your function result to all your tests: |
| 268 |
|
| 269 |
```rust |
| 270 |
#[fixture] |
| 271 |
#[once] |
| 272 |
fn once_fixture() -> i32 { 42 } |
| 273 |
|
| 274 |
#[rstest] |
| 275 |
fn single(once_fixture: &i32) { |
| 276 |
// All tests that use once_fixture will share the same reference to once_fixture() |
| 277 |
// function result. |
| 278 |
assert_eq!(&42, once_fixture) |
| 279 |
} |
| 280 |
``` |
| 281 |
|
| 282 |
### Test `#[timeout()]` |
| 283 |
|
| 284 |
You can define an execution timeout for your tests with `#[timeout(<duration>)]` attribute. Timeout |
| 285 |
works both for sync and async tests and is runtime agnostic. `#[timeout(<duration>)]` take an |
| 286 |
expression that should return a `std::time::Duration`. Follow a simple async example: |
| 287 |
|
| 288 |
```rust |
| 289 |
use rstest::*; |
| 290 |
use std::time::Duration; |
| 291 |
|
| 292 |
async fn delayed_sum(a: u32, b: u32, delay: Duration) -> u32 { |
| 293 |
async_std::task::sleep(delay).await; |
| 294 |
a + b |
| 295 |
} |
| 296 |
|
| 297 |
#[rstest] |
| 298 |
#[async_std::test] |
| 299 |
#[timeout(Duration::from_millis(80))] |
| 300 |
async fn single_pass() { |
| 301 |
assert_eq!(4, delayed_sum(2, 2, ms(10)).await); |
| 302 |
} |
| 303 |
``` |
| 304 |
|
| 305 |
In this case test pass because the delay is just 10 milliseconds and timeout is |
| 306 |
80 milliseconds. |
| 307 |
|
| 308 |
You can use `timeout` attribute like any other attribute in your tests, and you can |
| 309 |
override a group timeout with a case specific one. In the follow example we have |
| 310 |
3 tests where first and third use 100 milliseconds but the second one use 10 milliseconds. |
| 311 |
Another valuable point in this example is to use an expression to compute the |
| 312 |
duration. |
| 313 |
|
| 314 |
```rust |
| 315 |
fn ms(ms: u32) -> Duration { |
| 316 |
Duration::from_millis(ms.into()) |
| 317 |
} |
| 318 |
|
| 319 |
#[rstest] |
| 320 |
#[case::pass(ms(1), 4)] |
| 321 |
#[timeout(ms(10))] |
| 322 |
#[case::fail_timeout(ms(60), 4)] |
| 323 |
#[case::fail_value(ms(1), 5)] |
| 324 |
#[timeout(ms(100))] |
| 325 |
async fn group_one_timeout_override(#[case] delay: Duration, #[case] expected: u32) { |
| 326 |
assert_eq!(expected, delayed_sum(2, 2, delay).await); |
| 327 |
} |
| 328 |
``` |
| 329 |
|
| 330 |
If you want to use `timeout` for `async` test you need to use `async-timeout` |
| 331 |
feature (enabled by default). |
| 332 |
|
| 333 |
### Default timeout |
| 334 |
|
| 335 |
You can set a default timeout for test using the `RSTEST_TIMEOUT` environment variable. |
| 336 |
The value is in seconds and is evaluated on test compile time. |
| 337 |
|
| 338 |
### Explicit Test Attribute |
| 339 |
|
| 340 |
If your test contains an attribute `#[test_attr(...)]`, this is an explicit test attribute declaration. |
| 341 |
The contents of the parentheses will be inserted as an attribute of your test. This allows for arbitrary |
| 342 |
test attribute syntax. |
| 343 |
|
| 344 |
For instance, if you want to test an async function with [`smol_macros::test`](https://docs.rs/smol-macros/0.1.1/smol_macros/macro.test.html), |
| 345 |
you can write |
| 346 |
|
| 347 |
```rust |
| 348 |
use rstest::*; |
| 349 |
use macro_rules_attribute::apply; |
| 350 |
use smol_macros::test; |
| 351 |
use std::future::Future; |
| 352 |
|
| 353 |
#[rstest] |
| 354 |
#[case(2, async { 4 })] |
| 355 |
#[case(21, async { 42 })] |
| 356 |
#[test_attr(apply(test))] |
| 357 |
async fn my_async_test(#[case] a: u32, #[case] #[future] result: u32) { |
| 358 |
assert_eq!(2 * a, result.await); |
| 359 |
} |
| 360 |
``` |
| 361 |
|
| 362 |
When using an explicit test attribute, ensure it appears after any `#[case()]` attributes so that the attribute binds |
| 363 |
to the test function and not the case. |
| 364 |
|
| 365 |
### Implicit Test Attribute |
| 366 |
|
| 367 |
If your test contains an attribute whose path ends in `test`, this is treated |
| 368 |
as an implicit test attribute, and replaces the default `#[test]` attribute. |
| 369 |
Simply including the attribute in your test's attribute list is all that is required. |
| 370 |
|
| 371 |
For instance, if you want to test an async function with the `actix_rt::test` attribute, you can just write: |
| 372 |
|
| 373 |
```rust |
| 374 |
use rstest::*; |
| 375 |
use actix_rt; |
| 376 |
use std::future::Future; |
| 377 |
|
| 378 |
#[rstest] |
| 379 |
#[case(2, async { 4 })] |
| 380 |
#[case(21, async { 42 })] |
| 381 |
#[actix_rt::test] |
| 382 |
async fn my_async_test(#[case] a: u32, #[case] #[future] result: u32) { |
| 383 |
assert_eq!(2 * a, result.await); |
| 384 |
} |
| 385 |
``` |
| 386 |
|
| 387 |
Implicit test attributes have a lower priority than [`explicit test attributes`](#explicit-test-attribute). |
| 388 |
If you have an unrelated attribute whose path ends in `test`, you should declare an explicit test attribute |
| 389 |
for deconfliction. |
| 390 |
|
| 391 |
When using an implicit test attribute, ensure it appears after any `#[case()]` attributes so that the attribute binds to the |
| 392 |
test function and not the case. |
| 393 |
|
| 394 |
### Default Test Attribute |
| 395 |
|
| 396 |
Synchronous tests use `#[test]` as the default test attribute if nothing else is supplied explicitly or implicitly. |
| 397 |
|
| 398 |
Asynchronous tests will fail to compile if no test attribute is specified explicitly or implicitly. |
| 399 |
|
| 400 |
## Test `Context` object |
| 401 |
|
| 402 |
You can have a [`Context`] object for your test just by annotate an argument by `#[context]` attribute. |
| 403 |
This object contains some useful information both to implement simple logics and debugging stuff. |
| 404 |
|
| 405 |
```rust |
| 406 |
use rstest::{rstest, Context}; |
| 407 |
|
| 408 |
#[rstest] |
| 409 |
#[case::a_description(42)] |
| 410 |
fn my_test(#[context] ctx: Context, #[case] _c: u32) { |
| 411 |
assert_eq!("my_test", ctx.name); |
| 412 |
assert_eq!(Some("a_description"), ctx.description); |
| 413 |
assert_eq!(Some(0), ctx.case); |
| 414 |
|
| 415 |
std::thread::sleep(std::time::Duration::from_millis(100)); |
| 416 |
assert!(ctx.start.elapsed() >= std::time::Duration::from_millis(100)); |
| 417 |
} |
| 418 |
``` |
| 419 |
|
| 420 |
## Local lifetime and `#[by_ref]` attribute |
| 421 |
|
| 422 |
In some cases you may want to use a local lifetime for some arguments of your test. |
| 423 |
In these cases you can use the `#[by_ref]` attribute then use the reference instead |
| 424 |
the value. |
| 425 |
|
| 426 |
```rust |
| 427 |
enum E<'a> { |
| 428 |
A(bool), |
| 429 |
B(&'a Cell<E<'a>>), |
| 430 |
} |
| 431 |
|
| 432 |
fn make_e_from_bool<'a>(_bump: &'a (), b: bool) -> E<'a> { |
| 433 |
E::A(b) |
| 434 |
} |
| 435 |
|
| 436 |
#[fixture] |
| 437 |
fn bump() -> () {} |
| 438 |
|
| 439 |
#[rstest] |
| 440 |
#[case(true, E::A(true))] |
| 441 |
fn it_works<'a>(#[by_ref] bump: &'a (), #[case] b: bool, #[case] expected: E<'a>) { |
| 442 |
let actual = make_e_from_bool(&bump, b); |
| 443 |
assert_eq!(actual, expected); |
| 444 |
} |
| 445 |
``` |
| 446 |
|
| 447 |
You can use `#[by_ref]` attribute for all arguments of your test and not just for fixture |
| 448 |
but also for cases, values and files. |
| 449 |
|
| 450 |
## Complete Example |
| 451 |
|
| 452 |
All these features can be used together with a mixture of fixture variables, |
| 453 |
fixed cases and a bunch of values. For instance, you might need two |
| 454 |
test cases which test for panics, one for a logged-in user and one for a guest user. |
| 455 |
|
| 456 |
```rust |
| 457 |
use rstest::*; |
| 458 |
|
| 459 |
#[fixture] |
| 460 |
fn repository() -> InMemoryRepository { |
| 461 |
let mut r = InMemoryRepository::default(); |
| 462 |
// fill repository with some data |
| 463 |
r |
| 464 |
} |
| 465 |
|
| 466 |
#[fixture] |
| 467 |
fn alice() -> User { |
| 468 |
User::logged("Alice", "2001-10-04", "London", "UK") |
| 469 |
} |
| 470 |
|
| 471 |
#[rstest] |
| 472 |
#[case::authorized_user(alice())] // We can use `fixture` also as standard function |
| 473 |
#[case::guest(User::Guest)] // We can give a name to every case : `guest` in this case |
| 474 |
// and `authorized_user` |
| 475 |
#[should_panic(expected = "Invalid query error")] // We would test a panic |
| 476 |
fn should_be_invalid_query_error( |
| 477 |
repository: impl Repository, |
| 478 |
#[case] user: User, |
| 479 |
#[values(" ", "^%$some#@invalid!chars", ".n.o.d.o.t.s.")] query: &str, |
| 480 |
) { |
| 481 |
repository.find_items(&user, query).unwrap(); |
| 482 |
} |
| 483 |
``` |
| 484 |
|
| 485 |
This example will generate exactly 6 tests grouped by 2 different cases: |
| 486 |
|
| 487 |
```text |
| 488 |
running 6 tests |
| 489 |
test should_be_invalid_query_error::case_1_authorized_user::query_1_____ - should panic ... ok |
| 490 |
test should_be_invalid_query_error::case_2_guest::query_2_____someinvalid_chars__ - should panic ... ok |
| 491 |
test should_be_invalid_query_error::case_1_authorized_user::query_2_____someinvalid_chars__ - should panic ... ok |
| 492 |
test should_be_invalid_query_error::case_2_guest::query_3____n_o_d_o_t_s___ - should panic ... ok |
| 493 |
test should_be_invalid_query_error::case_1_authorized_user::query_3____n_o_d_o_t_s___ - should panic ... ok |
| 494 |
test should_be_invalid_query_error::case_2_guest::query_1_____ - should panic ... ok |
| 495 |
|
| 496 |
test result: ok. 6 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s |
| 497 |
``` |
| 498 |
|
| 499 |
Note that the names of the values _try_ to convert the input expression in a |
| 500 |
Rust valid identifier name to help you find which tests fail. |
| 501 |
|
| 502 |
## More |
| 503 |
|
| 504 |
Is that all? Not quite yet! |
| 505 |
|
| 506 |
A fixture can be injected by another fixture, and they can be called |
| 507 |
using just some of its arguments. |
| 508 |
|
| 509 |
```rust |
| 510 |
#[fixture] |
| 511 |
fn user(#[default("Alice")] name: &str, #[default(22)] age: u8) -> User { |
| 512 |
User::new(name, age) |
| 513 |
} |
| 514 |
|
| 515 |
#[rstest] |
| 516 |
fn is_alice(user: User) { |
| 517 |
assert_eq!(user.name(), "Alice") |
| 518 |
} |
| 519 |
|
| 520 |
#[rstest] |
| 521 |
fn is_22(user: User) { |
| 522 |
assert_eq!(user.age(), 22) |
| 523 |
} |
| 524 |
|
| 525 |
#[rstest] |
| 526 |
fn is_bob(#[with("Bob")] user: User) { |
| 527 |
assert_eq!(user.name(), "Bob") |
| 528 |
} |
| 529 |
|
| 530 |
#[rstest] |
| 531 |
fn is_42(#[with("", 42)] user: User) { |
| 532 |
assert_eq!(user.age(), 42) |
| 533 |
} |
| 534 |
``` |
| 535 |
|
| 536 |
As you noted you can provide default values without the need of a fixture |
| 537 |
to define it. |
| 538 |
|
| 539 |
Finally, if you need tracing the input values you can just |
| 540 |
add the `trace` attribute to your test to enable the dump of all input |
| 541 |
variables. |
| 542 |
|
| 543 |
```rust |
| 544 |
#[rstest] |
| 545 |
#[case(42, "FortyTwo", ("minus twelve", -12))] |
| 546 |
#[case(24, "TwentyFour", ("minus twentyfour", -24))] |
| 547 |
#[trace] //This attribute enable tracing |
| 548 |
fn should_fail(#[case] number: u32, #[case] name: &str, #[case] tuple: (&str, i32)) { |
| 549 |
assert!(false); // <- stdout come out just for failed tests |
| 550 |
} |
| 551 |
``` |
| 552 |
|
| 553 |
```text |
| 554 |
running 2 tests |
| 555 |
test should_fail::case_1 ... FAILED |
| 556 |
test should_fail::case_2 ... FAILED |
| 557 |
|
| 558 |
failures: |
| 559 |
|
| 560 |
---- should_fail::case_1 stdout ---- |
| 561 |
------------ TEST ARGUMENTS ------------ |
| 562 |
number = 42 |
| 563 |
name = "FortyTwo" |
| 564 |
tuple = ("minus twelve", -12) |
| 565 |
-------------- TEST START -------------- |
| 566 |
thread 'should_fail::case_1' panicked at 'assertion failed: false', src/main.rs:64:5 |
| 567 |
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace. |
| 568 |
|
| 569 |
---- should_fail::case_2 stdout ---- |
| 570 |
------------ TEST ARGUMENTS ------------ |
| 571 |
number = 24 |
| 572 |
name = "TwentyFour" |
| 573 |
tuple = ("minus twentyfour", -24) |
| 574 |
-------------- TEST START -------------- |
| 575 |
thread 'should_fail::case_2' panicked at 'assertion failed: false', src/main.rs:64:5 |
| 576 |
|
| 577 |
|
| 578 |
failures: |
| 579 |
should_fail::case_1 |
| 580 |
should_fail::case_2 |
| 581 |
|
| 582 |
test result: FAILED. 0 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out |
| 583 |
``` |
| 584 |
|
| 585 |
In case one or more variables don't implement the `Debug` trait, an error |
| 586 |
is raised, but it's also possible to exclude a variable using the |
| 587 |
`#[notrace]` argument attribute. |
| 588 |
|
| 589 |
You can learn more on [Docs][docs-link] and find more examples in |
| 590 |
[`tests/resources`](/rstest/tests/resources) directory. |
| 591 |
|
| 592 |
## Changelog |
| 593 |
|
| 594 |
See [CHANGELOG.md](/CHANGELOG.md) |
| 595 |
|
| 596 |
## License |
| 597 |
|
| 598 |
Licensed under either of |
| 599 |
|
| 600 |
* Apache License, Version 2.0, ([LICENSE-APACHE](/LICENSE-APACHE) or |
| 601 |
[license-apache-link]) |
| 602 |
|
| 603 |
* MIT license [LICENSE-MIT](/LICENSE-MIT) or [license-MIT-link] |
| 604 |
at your option. |
| 605 |
|
| 606 |
[//]: # (links) |
| 607 |
|
| 608 |
[crate-image]: https://img.shields.io/crates/v/rstest.svg |
| 609 |
|
| 610 |
[crate-link]: https://crates.io/crates/rstest |
| 611 |
|
| 612 |
[docs-image]: https://docs.rs/rstest/badge.svg |
| 613 |
|
| 614 |
[docs-link]: https://docs.rs/rstest/ |
| 615 |
|
| 616 |
[test-action-image]: https://github.com/la10736/rstest/workflows/Test/badge.svg |
| 617 |
|
| 618 |
[test-action-link]: https://github.com/la10736/rstest/actions?query=workflow:Test |
| 619 |
|
| 620 |
[license-apache-image]: https://img.shields.io/badge/license-Apache2.0-blue.svg |
| 621 |
|
| 622 |
[license-mit-image]: https://img.shields.io/badge/license-MIT-blue.svg |
| 623 |
|
| 624 |
[license-apache-link]: http://www.apache.org/licenses/LICENSE-2.0 |
| 625 |
|
| 626 |
[license-MIT-link]: http://opensource.org/licenses/MIT |
| 627 |
|
| 628 |
[reuse-crate-link]: https://crates.io/crates/rstest_reuse |