Debian Codegraph @

debian/rust-vendor/rstest_reuse/README.md

raw
1 [![Crate][crate-image]][crate-link]
2 [![Status][test-action-image]][test-action-link]
3 [![Apache 2.0 Licensed][license-apache-image]][license-apache-link]
4 [![MIT Licensed][license-mit-image]][license-mit-link]
5
6 # Reuse `rstest`'s parametrized cases
7
8 This crate gives a way to define tests set and apply them to every case you need to
9 test. With `rstest` crate you can define a tests list but if you want to apply the same tests
10 to another test function you must rewrite all cases or write some macros that do the job.
11
12 Both solutions have some drawbreak:
13
14 - introduce duplication
15 - macros makes code harder to read and shift out the focus from tests core
16
17 The aim of this crate is solve this problem. `rstest_reuse` expose two attributes:
18
19 - `#[template]`: to define a template
20 - `#[apply]`: to apply a defined template to create tests
21
22 Here is a simple example:
23
24 ```rust
25 use rstest::rstest;
26 use rstest_reuse::{self, *};
27 // Here we define the template. This define
28 // * The test list name to `two_simple_cases`
29 // * cases: here two cases
30 #[template]
31 #[rstest]
32 #[case(2, 2)]
33 #[case(4/2, 2)]
34 // Define a and b as cases arguments
35 fn two_simple_cases(#[case] a: u32, #[case] b: u32) {}
36 // Here we apply the `two_simple_cases` template: That is expanded in
37 // #[template]
38 // #[rstest]
39 // #[case(2, 2)]
40 // #[case(4/2, 2)]
41 // fn it_works(#[case] a: u32,#[case] b: u32) {
42 // assert!(a == b);
43 // }
44 #[apply(two_simple_cases)]
45 fn it_works(a: u32, b: u32) {
46 assert!(a == b);
47 }
48 // Here we reuse the `two_simple_cases` template to create two
49 // other tests
50 #[apply(two_simple_cases)]
51 fn it_fail(a: u32, b: u32) {
52 assert!(a != b);
53 }
54 ```
55
56 If we run `cargo test` we have:
57
58 ```text
59 Finished test [unoptimized + debuginfo] target(s) in 0.05s
60 Running target/debug/deps/playground-8a1212f8b5eb00ce
61 running 4 tests
62 test it_fail::case_1 ... FAILED
63 test it_works::case_1 ... ok
64 test it_works::case_2 ... ok
65 test it_fail::case_2 ... FAILED
66 failures:
67 ---- it_fail::case_1 stdout ----
68 thread 'it_fail::case_1' panicked at 'assertion failed: a != b', src/main.rs:34:5
69 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
70 ---- it_fail::case_2 stdout ----
71 thread 'it_fail::case_2' panicked at 'assertion failed: a != b', src/main.rs:34:5
72 failures:
73 it_fail::case_1
74 it_fail::case_2
75 test result: FAILED. 2 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out
76 error: test failed, to rerun pass '--bin playground'
77 ```
78
79 Simple and neat!
80
81 Note that if the test arguments names match the template's ones you can don't
82 repeat the arguments attributes.
83
84 ## Composition and Values
85
86 If you need to add some cases or values when apply a template you can leverage on
87 composition. Here a simple example:
88
89 ```rust
90 #[template]
91 #[rstest]
92 #[case(2, 2)]
93 #[case(4/2, 2)]
94 fn base(#[case] a: u32, #[case] b: u32) {}
95
96 // Here we add a new case and an argument in a value list:
97 #[apply(base)]
98 #[case(9/3, 3)]
99 fn it_works(a: u32, b: u32, #[values("a", "b")] t: &str) {
100 assert!(a == b);
101 assert!("abcd".contains(t))
102 }
103 ```
104
105 run 6 tests:
106
107 ```text
108 running 6 tests
109 test it_works::case_1::t_2 ... ok
110 test it_works::case_2::t_2 ... ok
111 test it_works::case_2::t_1 ... ok
112 test it_works::case_3::t_2 ... ok
113 test it_works::case_3::t_1 ... ok
114 test it_works::case_1::t_1 ... ok
115 ```
116
117 Template can also be used for values and with arguments if you need:
118
119 ```rust
120 #[template]
121 #[rstest]
122 fn base(#[with(42)] fix: u32, #[values(1,2,3)] v: u32) {}
123
124 #[fixture]
125 fn fix(#[default(0)] inner: u32) -> u32 {
126 inner
127 }
128
129 #[apply(base)]
130 fn use_it_with_fixture(fix: u32, v: u32) {
131 assert!(fix%v == 0);
132 }
133
134 #[apply(base)]
135 fn use_it_without_fixture(v: u32) {
136 assert!(24 % v == 0);
137 }
138 ```
139
140 Run also 6 tests:
141
142 ```text
143 running 6 tests
144 test use_it_with_fixture::v_1 ... ok
145 test use_it_without_fixture::v_1 ... ok
146 test use_it_with_fixture::v_3 ... ok
147 test use_it_without_fixture::v_2 ... ok
148 test use_it_without_fixture::v_3 ... ok
149 test use_it_with_fixture::v_2 ... ok
150 ```
151
152 ## `#[export]` Attribute
153
154 Now `#[export]` attribute give you the possibility to export your template across crates
155 but don't lift the macro definition at the top of your crate (that was the default behavior
156 prior the 0.5.0 version).
157
158 Now if you want to put your template at the root of your crate you can define it in the root
159 module or reexport it at the top with something like the following line at the top of
160 your crate:
161
162 ```rust
163 pub use my::modules::path::of::my::template::my_template;
164 ```
165
166 When you want to export your template you should also take care to declare `rstest_reuse` as `pub`
167 at the top of your crate to enable to use it from the modules that would import the template.
168
169 So in this case in the crate that would export template you should put at the root of your
170 crate
171
172 ```rust
173 #[cfg(test)]
174 pub use rstest_reuse;
175 ```
176
177 And not just `use rstest_reuse` like in the standard cases.
178
179 ## Disclamer
180
181 This crate is in a development stage. I don't know if I'll include it in `rstest` or change some syntax in the future.
182
183 I didn't test it in a lot of cases: if you have some cases where it doesn't work file a ticket on [`rstest`][rstest-link]
184
185 ## License
186
187 Licensed under either of
188
189 - Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
190 [license-apache-link])
191
192 - MIT license [LICENSE-MIT](LICENSE-MIT) or [license-MIT-link]
193 at your option.
194
195 [//]: # (links)
196
197 [crate-image]: https://img.shields.io/crates/v/rstest_reuse.svg
198 [crate-link]: https://crates.io/crates/rstest_reuse
199 [test-action-image]: https://github.com/la10736/rstest/workflows/Test/badge.svg
200 [test-action-link]: https://github.com/la10736/rstest/actions?query=workflow:Test
201 [license-apache-image]: https://img.shields.io/badge/license-Apache2.0-blue.svg
202 [license-mit-image]: https://img.shields.io/badge/license-MIT-blue.svg
203 [license-apache-link]: http://www.apache.org/licenses/LICENSE-2.0
204 [license-MIT-link]: http://opensource.org/licenses/MIT
205 [rstest-link]: https://github.com/la10736/rstest