#test #fixture

macro rstest_reuse

Reuse rstest attributes: create a set of tests and apply it to every scenario you want to test

3 releases

0.1.2 Mar 14, 2021
0.1.1 Feb 14, 2021
0.1.0 Jun 21, 2020

#112 in Testing

Download history 29/week @ 2020-12-23 106/week @ 2020-12-30 97/week @ 2021-01-06 38/week @ 2021-01-13 74/week @ 2021-01-20 140/week @ 2021-01-27 117/week @ 2021-02-03 235/week @ 2021-02-10 246/week @ 2021-02-17 201/week @ 2021-02-24 135/week @ 2021-03-03 159/week @ 2021-03-10 264/week @ 2021-03-17 159/week @ 2021-03-24 230/week @ 2021-03-31 572/week @ 2021-04-07

507 downloads per month
Used in 2 crates


86 lines

Crate Status Apache 2.0 Licensed MIT Licensed

Reuse rstest's parametrized cases

This crate give a way to define a tests set and apply them to every case you need to test. With rstest crate you can define a tests list but if you want to apply the same tests to another test function you must rewrite all cases or write some macros that do the job.

Both solutions have some drawbreak:

  • rewrite create duplication
  • macros makes code harder to read and shift out the focus from tests core

The aim of this crate is solve this problem. rstest_resuse expose two attributes:

  • #[template]: to define a template
  • #[apply]: to apply a defined template to create tests

Here is a simple example:

use rstest::rstest;
use rstest_reuse::{self, *};
// Here we define the template. This define
// * The test list name to `two_simple_cases`
// * cases: here two cases that feed the `a`, `b` values
#[rstest(a,  b,
    case(2, 2),
    case(4/2, 2),
fn two_simple_cases(a: u32, b: u32) {}
// Here we apply the `two_simple_cases` template: That is expanded in
// #[template]
// #[rstest(a,  b,
//     case(2, 2),
//     case(4/2, 2),
//     )
// ]
// fn it_works(a: u32, b: u32) {
//     assert!(a == b);
// }
fn it_works(a: u32, b: u32) {
    assert!(a == b);
// Here we reuse the `two_simple_cases` template to create two 
// other tests
fn it_fail(a: u32, b: u32) {
    assert!(a != b);

If we run cargo test we have:

    Finished test [unoptimized + debuginfo] target(s) in 0.05s
     Running target/debug/deps/playground-8a1212f8b5eb00ce
running 4 tests
test it_fail::case_1 ... FAILED
test it_works::case_1 ... ok
test it_works::case_2 ... ok
test it_fail::case_2 ... FAILED
---- it_fail::case_1 stdout ----
-------------- TEST START --------------
thread 'it_fail::case_1' panicked at 'assertion failed: a != b', src/main.rs:34:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
---- it_fail::case_2 stdout ----
-------------- TEST START --------------
thread 'it_fail::case_2' panicked at 'assertion failed: a != b', src/main.rs:34:5
test result: FAILED. 2 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out
error: test failed, to rerun pass '--bin playground'

Simple and neat!


use rstest_resuse at the top of your crate

You should add use rstest_resuse at the top of your crate:

use rstest_reuse;

This is due rstest_reuse::template define a macro that need to call a rstest_resuse's macro. I hope to remove this in the future but for now we should live with it.

Note that

use rstest_reuse::*;

is not enougth: this statment doesn't include rstest_reuse but just its public items.

Define template before apply it

template attribute define a macro that apply will use. Macro in rust are expanded in a single depth-first, lexical-order traversal of a crate’s source, that means the template definition should be allways before the apply.

Tag modules with #[macro_use]

If you define a template in a module and you want to use it outside the module you should lift it by mark the module with the #[macro_use] attribute. This attribute make your template visibe outside this module but not at the upper level. When a template is defined you can use it in all submodules that follow the definition.

If you plan to spread your templates in some modules and you use a dirrerent name for each template consider to add the global attribute !#[macro_use] at crate level: this put all your templates available everywhere: you should just take care that a template should be defined before the apply call.


This crate is in developer stage. I don't know if I'll include it in rstest or changing some syntax in the future.

I did't test it in a lot of cases: if you have some cases where it doesn't works file a ticket on rstest


Licensed under either of


~22K SLoC