> From for Matrix { fn from(_: P) -> Self { Default::default() } }
10 |
11 | fn create_empty_file() -> PathBuf { "".into() }
12 |
13 | #[test]
14 | fn should_parse_empty_file() {
15 | let empty = create_empty_file();
16 |
17 | let matrix: Matrix = Matrix::from(empty);
18 |
19 | assert_eq!(matrix, Default::default())
20 | }
21 | ```
22 |
23 | Ok, `create_empty_file()` should do a lot of job and can be reused in
24 | other tests and fixtures: it's a good candidate to be a fixture.
25 | Let's rewrite previous example by use `rstest`: we'll use `empty` for
26 | fixture instead of `create_empty_file()` because when we use as fixture
27 | will be an object instead an action.
28 |
29 | ```rust
30 | use std::path::{PathBuf, Path};
31 |
32 | use rstest::*;
33 |
34 | #[derive(Default, Debug, PartialEq)]
35 | struct Matrix { rows: usize, cols: usize, data: Vec }
36 | impl> From for Matrix { fn from(_: P) -> Self { Default::default() } }
37 |
38 | #[fixture]
39 | fn empty() -> PathBuf {
40 | let path = "empty_file".into();
41 | std::fs::File::create(&path).expect("Cannot open");
42 | path
43 | }
44 |
45 | #[rstest]
46 | fn should_parse_empty_file(empty: PathBuf) {
47 | let matrix: Matrix = Matrix::from(empty);
48 |
49 | assert_eq!(matrix, Default::default())
50 | }
51 | ```
52 |
53 | Now our `Matrix`'s `From` trait is a little bit more generic and can
54 | take every struct that implement `AsRef` like `PathBuf` do. So we
55 | can generalize our test too:
56 |
57 | ```rust
58 | use std::path::{PathBuf, Path};
59 |
60 | use rstest::*;
61 |
62 | #[derive(Default, Debug, PartialEq)]
63 | struct Matrix { rows: usize, cols: usize, data: Vec }
64 |
65 | impl> From for Matrix { fn from(_: P) -> Self { Default::default() } }
66 |
67 | #[rstest]
68 | fn should_parse_empty_file
(empty: P)
69 | where P: AsRef
70 | {
71 | let matrix: Matrix = Matrix::from(empty);
72 |
73 | assert_eq!(matrix, Default::default())
74 | }
75 | ```
76 |
77 | Our test is neat and clear but we have a big issue to fix: we cannot
78 | leave `"empty_file"` on our disk, we must remove it we are done!
79 | That is a job for Rust ownership!!
80 |
81 | We should just wrap `PathBuf` in out `TempFile` struct and implement
82 | both `Drop` and `AsRef` traits. We'll use `Drop` to delete it.
83 |
84 | ```rust
85 | use std::path::{PathBuf, Path};
86 |
87 | use rstest::*;
88 |
89 | struct TempFile(PathBuf);
90 |
91 | impl Drop for TempFile {fn drop(&mut self) {
92 | std::fs::remove_file(&self.0).unwrap();
93 | }
94 | }
95 |
96 | impl AsRef for TempFile {
97 | fn as_ref(&self) -> &Path {
98 | &self.0
99 | }
100 | }
101 |
102 | #[fixture]
103 | fn empty() -> TempFile {
104 | let path = "empty_file".into();
105 | std::fs::File::create(&path).expect("Cannot open");
106 | TempFile(path)
107 | }
108 |
--------------------------------------------------------------------------------
/playground/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | authors = ["michele "]
3 | edition = "2018"
4 | name = "playground"
5 | version = "0.1.0"
6 |
7 | [features]
8 | default = []
9 | # Depends on nightly
10 | trace_all = []
11 |
12 | [dependencies]
13 | async-std = { version = "1.13.0", features = ["attributes"] }
14 | lazy_static = "1.4.0"
15 |
16 | [dependencies.rstest]
17 | path = "../rstest"
18 | version = "0.25.0-dev"
19 |
20 | [dependencies.rstest_reuse]
21 | path = "../rstest_reuse"
22 | version = "*"
23 |
--------------------------------------------------------------------------------
/playground/LICENSE-APACHE:
--------------------------------------------------------------------------------
1 | ../LICENSE-APACHE
--------------------------------------------------------------------------------
/playground/LICENSE-MIT:
--------------------------------------------------------------------------------
1 | ../LICENSE-MIT
--------------------------------------------------------------------------------
/playground/files/myname.txt:
--------------------------------------------------------------------------------
1 | myname.txt
2 |
3 |
4 | something else
--------------------------------------------------------------------------------
/playground/files/other:
--------------------------------------------------------------------------------
1 | no test
--------------------------------------------------------------------------------
/playground/files/should_fail.txt:
--------------------------------------------------------------------------------
1 | something else
2 |
--------------------------------------------------------------------------------
/playground/src/main.rs:
--------------------------------------------------------------------------------
1 | use rstest::*;
2 | use std::fs::File;
3 | use std::path::PathBuf;
4 | use std::io::Read;
5 |
6 | #[rstest]
7 | fn start_with_name(#[files("files/*.txt")] path: PathBuf) {
8 | let name = path.file_name().unwrap();
9 | let mut f = File::open(&path).unwrap();
10 | let mut contents = String::new();
11 | f.read_to_string(&mut contents).unwrap();
12 |
13 | assert!(contents.starts_with(name.to_str().unwrap()))
14 | }
--------------------------------------------------------------------------------
/rstest/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | authors = ["Michele d'Amico "]
3 | categories = ["development-tools::testing"]
4 | description = """
5 | Rust fixture based test framework. It use procedural macro
6 | to implement fixtures and table based tests.
7 | """
8 | edition = "2021"
9 | homepage = "https://github.com/la10736/rstest"
10 | keywords = ["test", "fixture"]
11 | license = "MIT OR Apache-2.0"
12 | name = "rstest"
13 | readme = "README.md"
14 | repository = "https://github.com/la10736/rstest"
15 | rust-version = "1.70.0"
16 | version = "0.25.0-dev"
17 |
18 | [features]
19 | async-timeout = [
20 | "dep:futures-timer",
21 | "dep:futures-util",
22 | "rstest_macros/async-timeout",
23 | ]
24 | crate-name = ["rstest_macros/crate-name"]
25 | default = ["async-timeout", "crate-name"]
26 |
27 | [lib]
28 |
29 | [dependencies]
30 | futures-timer = { version = "3.0.3", optional = true }
31 | futures-util = { version = "0.3.30", optional = true }
32 | rstest_macros = { version = "0.25.0-dev", path = "../rstest_macros", default-features = false }
33 |
34 | [dev-dependencies]
35 | actix-rt = "2.9.0"
36 | async-std = { version = "1.13.0", features = ["attributes"] }
37 | lazy_static = "1.5.0"
38 | macro_rules_attribute = "0.2.0"
39 | mytest = { package = "rstest", version = "0.25.0", default-features = false }
40 | pretty_assertions = "1.4.1"
41 | rstest_reuse = { path = "../rstest_reuse" }
42 | rstest_test = { path = "../rstest_test" }
43 | smol-macros = "0.1.1"
44 | temp_testdir = "0.2.3"
45 | tokio = { version = "1.38.1", features = ["rt", "macros"] }
46 | unindent = "0.2.3"
47 |
48 | [build-dependencies]
49 | rustc_version = "0.4.1"
50 |
--------------------------------------------------------------------------------
/rstest/LICENSE-APACHE:
--------------------------------------------------------------------------------
1 | ../LICENSE-APACHE
--------------------------------------------------------------------------------
/rstest/LICENSE-MIT:
--------------------------------------------------------------------------------
1 | ../LICENSE-MIT
--------------------------------------------------------------------------------
/rstest/README.md:
--------------------------------------------------------------------------------
1 | ../README.md
--------------------------------------------------------------------------------
/rstest/src/context.rs:
--------------------------------------------------------------------------------
1 | /// A test context.
2 | #[non_exhaustive]
3 | pub struct Context {
4 | /// The complete module test path
5 | pub module: &'static str,
6 | /// The test function name
7 | pub name: &'static str,
8 | /// The test description if present
9 | pub description: Option<&'static str>,
10 | /// The cardinal case number if it's a test case
11 | pub case: Option,
12 | /// Start time
13 | pub start: std::time::Instant,
14 | }
15 |
16 | impl Context {
17 | /// Create a new test context. This function set also the start time to the current time.
18 | pub fn new(
19 | module: &'static str,
20 | name: &'static str,
21 | description: Option<&'static str>,
22 | case: Option,
23 | ) -> Self {
24 | Self {
25 | module,
26 | name,
27 | description,
28 | case,
29 | start: std::time::Instant::now(),
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/rstest/src/magic_conversion.rs:
--------------------------------------------------------------------------------
1 | pub struct Magic(pub std::marker::PhantomData);
2 |
3 | pub trait ViaParseDebug<'a, T> {
4 | fn magic_conversion(&self, input: &'a str) -> T;
5 | }
6 |
7 | impl<'a, T> ViaParseDebug<'a, T> for &&Magic
8 | where
9 | T: std::str::FromStr,
10 | T::Err: std::fmt::Debug,
11 | {
12 | fn magic_conversion(&self, input: &'a str) -> T {
13 | T::from_str(input).unwrap()
14 | }
15 | }
16 |
17 | pub trait ViaParse<'a, T> {
18 | fn magic_conversion(&self, input: &'a str) -> T;
19 | }
20 |
21 | impl<'a, T> ViaParse<'a, T> for &Magic
22 | where
23 | T: std::str::FromStr,
24 | {
25 | fn magic_conversion(&self, input: &'a str) -> T {
26 | match T::from_str(input) {
27 | Ok(v) => v,
28 | Err(_) => {
29 | panic!(
30 | "Cannot parse '{}' to get {}",
31 | input,
32 | std::any::type_name::()
33 | );
34 | }
35 | }
36 | }
37 | }
38 |
39 | pub trait ViaIdent<'a, T> {
40 | fn magic_conversion(&self, input: &'a str) -> T;
41 | }
42 |
43 | impl<'a> ViaIdent<'a, &'a str> for &&Magic<&'a str> {
44 | fn magic_conversion(&self, input: &'a str) -> &'a str {
45 | input
46 | }
47 | }
48 |
49 | #[cfg(test)]
50 | mod test {
51 | use super::*;
52 | use std::str::FromStr;
53 |
54 | #[test]
55 | fn should_return_the_same_slice_string() {
56 | assert_eq!(
57 | "something",
58 | (&&&Magic::<&str>(std::marker::PhantomData)).magic_conversion("something")
59 | );
60 | }
61 |
62 | #[test]
63 | fn should_parse_via_parse_debug() {
64 | assert_eq!(
65 | 42u32,
66 | (&&&Magic::(std::marker::PhantomData)).magic_conversion("42")
67 | );
68 | }
69 |
70 | #[test]
71 | fn should_parse_via_parse_no_error_debug() {
72 | struct S(String);
73 | struct E;
74 | impl FromStr for S {
75 | type Err = E;
76 |
77 | fn from_str(s: &str) -> Result {
78 | Ok(S(s.to_owned()))
79 | }
80 | }
81 |
82 | assert_eq!(
83 | "some",
84 | (&&&Magic::(std::marker::PhantomData))
85 | .magic_conversion("some")
86 | .0
87 | );
88 | }
89 |
90 | #[test]
91 | #[should_panic(expected = "MyTypeName")]
92 | fn should_show_error() {
93 | struct MyTypeName;
94 | struct E;
95 | impl FromStr for MyTypeName {
96 | type Err = E;
97 |
98 | fn from_str(_s: &str) -> Result {
99 | Err(E)
100 | }
101 | }
102 | (&&&Magic::(std::marker::PhantomData)).magic_conversion("");
103 | }
104 | }
105 |
--------------------------------------------------------------------------------
/rstest/tests/integration.rs:
--------------------------------------------------------------------------------
1 | use rstest_test::{sanitize_name, testname, Project};
2 |
3 | /// Rstest integration tests
4 | mod rstest;
5 |
6 | /// Fixture's integration tests
7 | mod fixture;
8 |
9 | use lazy_static::lazy_static;
10 |
11 | use std::path::{Path, PathBuf};
12 | use temp_testdir::TempDir;
13 |
14 | lazy_static! {
15 | static ref ROOT_DIR: TempDir = TempDir::default().permanent();
16 | static ref ROOT_PROJECT: Project = Project::new(ROOT_DIR.as_ref());
17 | }
18 |
19 | pub fn base_prj() -> Project {
20 | let prj_name = sanitize_name(testname());
21 |
22 | ROOT_PROJECT.subproject(&prj_name)
23 | }
24 |
25 | pub fn prj() -> Project {
26 | let prj_name = sanitize_name(testname());
27 |
28 | let prj = ROOT_PROJECT.subproject(&prj_name);
29 | prj.add_local_dependency("rstest");
30 | prj
31 | }
32 |
33 | pub fn resources>(name: O) -> PathBuf {
34 | Path::new("tests").join("resources").join(name)
35 | }
36 |
--------------------------------------------------------------------------------
/rstest/tests/resources/fixture/async_fixture.rs:
--------------------------------------------------------------------------------
1 | use std::io::prelude::*;
2 |
3 | use rstest::*;
4 |
5 | #[fixture]
6 | async fn async_u32() -> u32 {
7 | 42
8 | }
9 |
10 | #[fixture]
11 | async fn nest_fixture(#[future] async_u32: u32) -> u32 {
12 | async_u32.await
13 | }
14 |
15 | #[fixture(fortytwo = async { 42 })]
16 | async fn nest_fixture_with_default(#[future] fortytwo: u32) -> u32 {
17 | fortytwo.await
18 | }
19 |
20 | #[rstest]
21 | #[async_std::test]
22 | async fn default_is_async() {
23 | assert_eq!(42, async_u32::default().await);
24 | }
25 |
26 | #[rstest]
27 | #[async_std::test]
28 | async fn use_async_nest_fixture_default(#[future] nest_fixture: u32) {
29 | assert_eq!(42, nest_fixture.await);
30 | }
31 |
32 | #[rstest(nest_fixture(async { 24 }))]
33 | #[async_std::test]
34 | async fn use_async_nest_fixture_injected(#[future] nest_fixture: u32) {
35 | assert_eq!(24, nest_fixture.await);
36 | }
37 |
38 | #[rstest]
39 | #[async_std::test]
40 | async fn use_async_nest_fixture_with_default(#[future] nest_fixture_with_default: u32) {
41 | assert_eq!(42, nest_fixture_with_default.await);
42 | }
43 |
44 | #[rstest]
45 | #[async_std::test]
46 | async fn use_async_fixture(#[future] async_u32: u32) {
47 | assert_eq!(42, async_u32.await);
48 | }
49 |
50 | #[fixture]
51 | async fn async_impl_output() -> impl Read {
52 | std::io::Cursor::new(vec![1, 2, 3, 4, 5])
53 | }
54 |
55 | #[rstest]
56 | #[async_std::test]
57 | async fn use_async_impl_output(#[future] async_impl_output: T) {
58 | let reader = async_impl_output.await;
59 | }
60 |
61 | #[fixture(four = async { 4 }, two = 2)]
62 | async fn two_args_mix_fixture(#[future] four: u32, two: u32) -> u32 {
63 | four.await * 10 + two
64 | }
65 |
66 | #[rstest]
67 | #[async_std::test]
68 | async fn use_two_args_mix_fixture(#[future] two_args_mix_fixture: u32) {
69 | assert_eq!(42, two_args_mix_fixture.await);
70 | }
71 |
72 | #[rstest(two_args_mix_fixture(async { 5 }))]
73 | #[async_std::test]
74 | async fn use_two_args_mix_fixture_inject_first(#[future] two_args_mix_fixture: u32) {
75 | assert_eq!(52, two_args_mix_fixture.await);
76 | }
77 |
78 | #[rstest(two_args_mix_fixture(async { 3 }, 1))]
79 | #[async_std::test]
80 | async fn use_two_args_mix_fixture_inject_both(#[future] two_args_mix_fixture: u32) {
81 | assert_eq!(31, two_args_mix_fixture.await);
82 | }
83 |
--------------------------------------------------------------------------------
/rstest/tests/resources/fixture/await_complete_fixture.rs:
--------------------------------------------------------------------------------
1 | use std::io::prelude::*;
2 |
3 | use rstest::*;
4 |
5 | #[fixture]
6 | async fn async_u32() -> u32 {
7 | 42
8 | }
9 |
10 | #[fixture]
11 | #[awt]
12 | async fn nest_fixture(#[future] async_u32: u32) -> u32 {
13 | async_u32
14 | }
15 |
16 | #[fixture]
17 | #[awt]
18 | async fn nest_fixture_with_default(
19 | #[future]
20 | #[default(async { 42 })]
21 | fortytwo: u32,
22 | ) -> u32 {
23 | fortytwo
24 | }
25 |
26 | #[rstest]
27 | #[async_std::test]
28 | async fn default_is_async() {
29 | assert_eq!(42, async_u32::default().await);
30 | }
31 |
32 | #[rstest]
33 | #[async_std::test]
34 | #[awt]
35 | async fn use_async_nest_fixture_default(#[future] nest_fixture: u32) {
36 | assert_eq!(42, nest_fixture);
37 | }
38 |
39 | #[rstest]
40 | #[async_std::test]
41 | #[awt]
42 | async fn use_async_nest_fixture_injected(
43 | #[future]
44 | #[with(async { 24 })]
45 | nest_fixture: u32,
46 | ) {
47 | assert_eq!(24, nest_fixture);
48 | }
49 |
50 | #[rstest]
51 | #[async_std::test]
52 | #[awt]
53 | async fn use_async_nest_fixture_with_default(#[future] nest_fixture_with_default: u32) {
54 | assert_eq!(42, nest_fixture_with_default);
55 | }
56 |
57 | #[rstest]
58 | #[async_std::test]
59 | #[awt]
60 | async fn use_async_fixture(#[future] async_u32: u32) {
61 | assert_eq!(42, async_u32);
62 | }
63 |
64 | #[fixture]
65 | async fn async_impl_output() -> impl Read {
66 | std::io::Cursor::new(vec![1, 2, 3, 4, 5])
67 | }
68 |
69 | #[rstest]
70 | #[async_std::test]
71 | #[awt]
72 | async fn use_async_impl_output(#[future] async_impl_output: T) {
73 | let reader = async_impl_output;
74 | }
75 |
76 | #[fixture]
77 | #[awt]
78 | async fn two_args_mix_fixture(
79 | #[future]
80 | #[default(async { 4 })]
81 | four: u32,
82 | #[default(2)] two: u32,
83 | ) -> u32 {
84 | four * 10 + two
85 | }
86 |
87 | #[rstest]
88 | #[async_std::test]
89 | #[awt]
90 | async fn use_two_args_mix_fixture(#[future] two_args_mix_fixture: u32) {
91 | assert_eq!(42, two_args_mix_fixture);
92 | }
93 |
94 | #[rstest]
95 | #[async_std::test]
96 | #[awt]
97 | async fn use_two_args_mix_fixture_inject_first(
98 | #[future]
99 | #[with(async { 5 })]
100 | two_args_mix_fixture: u32,
101 | ) {
102 | assert_eq!(52, two_args_mix_fixture);
103 | }
104 |
105 | #[rstest]
106 | #[async_std::test]
107 | #[awt]
108 | async fn use_two_args_mix_fixture_inject_both(
109 | #[future]
110 | #[with(async { 3 }, 1)]
111 | two_args_mix_fixture: u32,
112 | ) {
113 | assert_eq!(31, two_args_mix_fixture);
114 | }
115 |
--------------------------------------------------------------------------------
/rstest/tests/resources/fixture/await_partial_fixture.rs:
--------------------------------------------------------------------------------
1 | use std::io::prelude::*;
2 |
3 | use rstest::*;
4 |
5 | #[fixture]
6 | async fn async_u32() -> u32 {
7 | 42
8 | }
9 |
10 | #[fixture]
11 | async fn nest_fixture(#[future(awt)] async_u32: u32) -> u32 {
12 | async_u32
13 | }
14 |
15 | #[fixture]
16 | async fn nest_fixture_with_default(
17 | #[future(awt)]
18 | #[default(async { 42 })]
19 | fortytwo: u32,
20 | ) -> u32 {
21 | fortytwo
22 | }
23 |
24 | #[rstest]
25 | #[async_std::test]
26 | async fn default_is_async() {
27 | assert_eq!(42, async_u32::default().await);
28 | }
29 |
30 | #[rstest]
31 | #[async_std::test]
32 | async fn use_async_nest_fixture_default(#[future(awt)] nest_fixture: u32) {
33 | assert_eq!(42, nest_fixture);
34 | }
35 |
36 | #[rstest]
37 | #[async_std::test]
38 | async fn use_async_nest_fixture_injected(
39 | #[future(awt)]
40 | #[with(async { 24 })]
41 | nest_fixture: u32,
42 | ) {
43 | assert_eq!(24, nest_fixture);
44 | }
45 |
46 | #[rstest]
47 | #[async_std::test]
48 | async fn use_async_nest_fixture_with_default(#[future(awt)] nest_fixture_with_default: u32) {
49 | assert_eq!(42, nest_fixture_with_default);
50 | }
51 |
52 | #[rstest]
53 | #[async_std::test]
54 | async fn use_async_fixture(#[future(awt)] async_u32: u32) {
55 | assert_eq!(42, async_u32);
56 | }
57 |
58 | #[fixture]
59 | async fn async_impl_output() -> impl Read {
60 | std::io::Cursor::new(vec![1, 2, 3, 4, 5])
61 | }
62 |
63 | #[rstest]
64 | #[async_std::test]
65 | async fn use_async_impl_output(#[future(awt)] async_impl_output: T) {
66 | let reader = async_impl_output;
67 | }
68 |
69 | #[fixture]
70 | async fn two_args_mix_fixture(
71 | #[future(awt)]
72 | #[default(async { 4 })]
73 | four: u32,
74 | #[default(2)] two: u32,
75 | ) -> u32 {
76 | four * 10 + two
77 | }
78 |
79 | #[rstest]
80 | #[async_std::test]
81 | async fn use_two_args_mix_fixture(#[future(awt)] two_args_mix_fixture: u32) {
82 | assert_eq!(42, two_args_mix_fixture);
83 | }
84 |
85 | #[rstest]
86 | #[async_std::test]
87 | async fn use_two_args_mix_fixture_inject_first(
88 | #[future(awt)]
89 | #[with(async { 5 })]
90 | two_args_mix_fixture: u32,
91 | ) {
92 | assert_eq!(52, two_args_mix_fixture);
93 | }
94 |
95 | #[rstest]
96 | #[async_std::test]
97 | async fn use_two_args_mix_fixture_inject_both(
98 | #[future(awt)]
99 | #[with(async { 3 }, 1)]
100 | two_args_mix_fixture: u32,
101 | ) {
102 | assert_eq!(31, two_args_mix_fixture);
103 | }
104 |
--------------------------------------------------------------------------------
/rstest/tests/resources/fixture/clean_up_default_generics.rs:
--------------------------------------------------------------------------------
1 | use rstest::*;
2 |
3 | #[fixture]
4 | fn s() -> &'static str {
5 | "42"
6 | }
7 |
8 | #[fixture]
9 | fn fx(s: S) -> usize {
10 | s.to_string().len()
11 | }
12 |
13 | #[fixture]
14 | fn sum() -> usize {
15 | 42
16 | }
17 |
18 | #[fixture]
19 | fn fx_double(sum: usize, s: S) -> usize {
20 | s.to_string().len() + sum
21 | }
22 |
23 | #[test]
24 | fn resolve() {
25 | assert_eq!(2, fx::default())
26 | }
27 |
28 | #[test]
29 | fn resolve_partial() {
30 | assert_eq!(12, fx_double::partial_1(10))
31 | }
32 |
--------------------------------------------------------------------------------
/rstest/tests/resources/fixture/default.rs:
--------------------------------------------------------------------------------
1 | use rstest::{fixture, rstest};
2 |
3 | #[fixture(value = 42)]
4 | pub fn simple(value: u32) -> u32 {
5 | value
6 | }
7 |
8 | #[fixture(value = 21, mult = 2)]
9 | pub fn double(value: u32, mult: u32) -> u32 {
10 | value * mult
11 | }
12 |
13 | #[fixture]
14 | pub fn middle() -> u32 {
15 | 2
16 | }
17 |
18 | #[fixture(value = 21, mult = 4)]
19 | pub fn mixed(value: u32, middle: u32, mult: u32) -> u32 {
20 | value * mult / middle
21 | }
22 |
23 | #[rstest]
24 | fn test_simple(simple: u32) {
25 | assert_eq!(simple, 42)
26 | }
27 |
28 | #[rstest(simple(21))]
29 | fn test_simple_changed(simple: u32) {
30 | assert_eq!(simple, 21)
31 | }
32 |
33 | #[rstest]
34 | fn test_double(double: u32) {
35 | assert_eq!(double, 42)
36 | }
37 |
38 | #[rstest(double(20, 3))]
39 | fn test_double_changed(double: u32) {
40 | assert_eq!(double, 60)
41 | }
42 |
43 | #[rstest]
44 | fn test_mixed(mixed: u32) {
45 | assert_eq!(mixed, 42)
46 | }
47 |
--------------------------------------------------------------------------------
/rstest/tests/resources/fixture/default_conversion.rs:
--------------------------------------------------------------------------------
1 | use rstest::{fixture, rstest};
2 | use std::net::{Ipv4Addr, SocketAddr};
3 |
4 | struct MyType(String);
5 | struct E;
6 | impl core::str::FromStr for MyType {
7 | type Err = E;
8 |
9 | fn from_str(s: &str) -> Result {
10 | match s {
11 | "error" => Err(E),
12 | inner => Ok(MyType(inner.to_owned())),
13 | }
14 | }
15 | }
16 |
17 | #[fixture]
18 | fn base(#[default("1.2.3.4")] ip: Ipv4Addr, #[default(r#"8080"#)] port: u16) -> SocketAddr {
19 | SocketAddr::new(ip.into(), port)
20 | }
21 |
22 | #[fixture]
23 | fn fail(#[default("error")] t: MyType) -> MyType {
24 | t
25 | }
26 |
27 | #[fixture]
28 | fn valid(#[default("some")] t: MyType) -> MyType {
29 | t
30 | }
31 |
32 | #[rstest]
33 | fn test_base(base: SocketAddr) {
34 | assert_eq!(base, "1.2.3.4:8080".parse().unwrap());
35 | }
36 |
37 | #[fixture]
38 | fn byte_array(#[default(b"1234")] some: &[u8]) -> usize {
39 | some.len()
40 | }
41 |
42 | #[rstest]
43 | fn test_byte_array(byte_array: usize) {
44 | assert_eq!(4, byte_array);
45 | }
46 |
47 | #[rstest]
48 | fn test_convert_custom(valid: MyType) {}
49 |
50 | #[rstest]
51 | fn test_fail_conversion(fail: MyType) {}
52 |
--------------------------------------------------------------------------------
/rstest/tests/resources/fixture/default_in_attrs.rs:
--------------------------------------------------------------------------------
1 | use rstest::{fixture, rstest};
2 |
3 | #[fixture]
4 | pub fn simple(#[default(42)] value: u32) -> u32 {
5 | value
6 | }
7 |
8 | #[fixture]
9 | pub fn double(#[default(20 + 1)] value: u32, #[default(1 + 1)] mult: u32) -> u32 {
10 | value * mult
11 | }
12 |
13 | #[fixture]
14 | pub fn middle() -> u32 {
15 | 2
16 | }
17 |
18 | #[fixture]
19 | pub fn mixed(#[default(21)] value: u32, middle: u32, #[default(2 + 2)] mult: u32) -> u32 {
20 | value * mult / middle
21 | }
22 |
23 | #[rstest]
24 | fn test_simple(simple: u32) {
25 | assert_eq!(simple, 42)
26 | }
27 |
28 | #[rstest(simple(21))]
29 | fn test_simple_changed(simple: u32) {
30 | assert_eq!(simple, 21)
31 | }
32 |
33 | #[rstest]
34 | fn test_double(double: u32) {
35 | assert_eq!(double, 42)
36 | }
37 |
38 | #[rstest(double(20, 3))]
39 | fn test_double_changed(double: u32) {
40 | assert_eq!(double, 60)
41 | }
42 |
43 | #[rstest]
44 | fn test_mixed(mixed: u32) {
45 | assert_eq!(mixed, 42)
46 | }
47 |
--------------------------------------------------------------------------------
/rstest/tests/resources/fixture/defined_return_type.rs:
--------------------------------------------------------------------------------
1 | use rstest::*;
2 |
3 | #[fixture]
4 | pub fn i() -> u32 {
5 | 42
6 | }
7 |
8 | #[fixture]
9 | pub fn j() -> i32 {
10 | -42
11 | }
12 |
13 | #[fixture(::default>::partial_1>)]
14 | pub fn fx(i: I, j: J) -> impl Iterator- {
15 | std::iter::once((i, j))
16 | }
17 |
18 | #[test]
19 | fn resolve() {
20 | assert_eq!((42, -42), fx::default().next().unwrap())
21 | }
22 |
23 | #[test]
24 | fn resolve_partial() {
25 | assert_eq!((42.0, -42), fx::partial_1(42.0).next().unwrap())
26 | }
27 |
28 | #[fixture]
29 | #[default(impl Iterator
- )]
30 | #[partial_1(impl Iterator
- )]
31 | pub fn fx_attrs(i: I, j: J) -> impl Iterator
- {
32 | std::iter::once((i, j))
33 | }
34 |
35 | #[test]
36 | fn resolve_attrs() {
37 | assert_eq!((42, -42), fx_attrs::default().next().unwrap())
38 | }
39 |
40 | #[test]
41 | fn resolve_partial_attrs() {
42 | assert_eq!((42.0, -42), fx_attrs::partial_1(42.0).next().unwrap())
43 | }
--------------------------------------------------------------------------------
/rstest/tests/resources/fixture/deny_docs.rs:
--------------------------------------------------------------------------------
1 | //! Crate with tests for missing docs.
2 |
3 | #![deny(missing_docs)]
4 |
5 | use rstest::{fixture, rstest};
6 |
7 | /// Root fixture
8 | #[fixture]
9 | pub fn root() -> u32 {
10 | 21
11 | }
12 |
13 | /// Injection fixture
14 | #[fixture]
15 | pub fn injection(root: u32) -> u32 {
16 | 2 * root
17 | }
18 |
19 | /// Success test
20 | #[rstest]
21 | pub fn success(injection: u32) {
22 | assert_eq!(42, injection);
23 | }
24 |
25 | /// Fail test
26 | #[rstest]
27 | pub fn fail(injection: u32) {
28 | assert_eq!(41, injection);
29 | }
30 |
--------------------------------------------------------------------------------
/rstest/tests/resources/fixture/dyn.rs:
--------------------------------------------------------------------------------
1 | use rstest::*;
2 |
3 | #[fixture]
4 | fn dyn_box() -> Box> {
5 | Box::new(std::iter::once(42))
6 | }
7 |
8 | #[fixture]
9 | fn dyn_ref() -> &'static dyn ToString {
10 | &42
11 | }
12 |
13 | #[fixture]
14 | fn dyn_box_resolve(mut dyn_box: Box>) -> i32 {
15 | dyn_box.next().unwrap()
16 | }
17 |
18 | #[fixture]
19 | fn dyn_ref_resolve(dyn_ref: &dyn ToString) -> String {
20 | dyn_ref.to_string()
21 | }
22 |
23 | #[rstest]
24 | fn test_dyn_box(mut dyn_box: Box>) {
25 | assert_eq!(42, dyn_box.next().unwrap())
26 | }
27 |
28 | #[rstest]
29 | fn test_dyn_ref(dyn_ref: &dyn ToString) {
30 | assert_eq!("42", dyn_ref.to_string())
31 | }
32 |
33 | #[rstest]
34 | fn test_dyn_box_resolve(dyn_box_resolve: i32) {
35 | assert_eq!(42, dyn_box_resolve)
36 | }
37 |
38 | #[rstest]
39 | fn test_dyn_ref_resolve(dyn_ref_resolve: String) {
40 | assert_eq!("42", dyn_ref_resolve)
41 | }
42 |
--------------------------------------------------------------------------------
/rstest/tests/resources/fixture/errors.rs:
--------------------------------------------------------------------------------
1 | use rstest::*;
2 |
3 | #[fixture]
4 | pub fn fixture() -> u32 {
5 | 42
6 | }
7 |
8 | #[fixture]
9 | fn error_inner(fixture: u32) {
10 | let a: u32 = "";
11 | }
12 |
13 | #[fixture]
14 | fn error_cannot_resolve_fixture(no_fixture: u32) {}
15 |
16 | #[fixture]
17 | fn error_fixture_wrong_type(fixture: String) {}
18 |
19 | #[fixture(not_a_fixture(24))]
20 | fn error_inject_an_invalid_fixture(fixture: String) {}
21 |
22 | #[fixture]
23 | fn name() -> &'static str {
24 | "name"
25 | }
26 |
27 | #[fixture]
28 | fn f(name: &str) -> String {
29 | name.to_owned()
30 | }
31 |
32 | #[fixture(f("first"), f("second"))]
33 | fn error_inject_a_fixture_more_than_once(f: String) {}
34 |
35 | struct T(u32);
36 |
37 | #[fixture]
38 | fn structed() -> T {
39 | T(42)
40 | }
41 |
42 | #[fixture]
43 | fn structed_injectd(fixture: u32) -> T {
44 | T(fixture)
45 | }
46 |
47 | #[fixture]
48 | fn error_destruct_without_resolve(T(a): T) {}
49 |
50 | #[fixture]
51 | fn error_destruct_without_resolve_also_with(#[with(21)] T(a): T) {}
52 |
--------------------------------------------------------------------------------
/rstest/tests/resources/fixture/errors_once.rs:
--------------------------------------------------------------------------------
1 | use rstest::*;
2 |
3 | #[fixture]
4 | #[once]
5 | async fn error_async_once_fixture() {
6 | }
7 |
8 | #[fixture]
9 | #[once]
10 | fn error_generics_once_fixture() -> T {
11 | 42
12 | }
13 |
14 | #[fixture]
15 | #[once]
16 | fn error_generics_once_fixture() -> impl Iterator
- {
17 | std::iter::once(42)
18 | }
19 |
20 | #[fixture]
21 | #[once]
22 | fn error_once_fixture_not_sync() -> std::cell::Cell {
23 | std::cell::Cell::new(42)
24 | }
25 |
--------------------------------------------------------------------------------
/rstest/tests/resources/fixture/fixture_struct.rs:
--------------------------------------------------------------------------------
1 | use rstest::fixture;
2 |
3 | trait Mult {
4 | fn mult(&self, n: u32) -> u32;
5 | }
6 |
7 | struct M(u32);
8 |
9 | impl Mult for M {
10 | fn mult(&self, n: u32) -> u32 {
11 | n * self.0
12 | }
13 | }
14 |
15 | #[fixture]
16 | fn my_fixture() -> u32 { 42 }
17 |
18 | #[fixture]
19 | fn multiplier() -> M {
20 | M(2)
21 | }
22 |
23 | #[fixture]
24 | fn my_fixture_injected(my_fixture: u32, multiplier: impl Mult) -> u32 { multiplier.mult(my_fixture) }
25 |
26 | #[test]
27 | fn resolve_new() {
28 | assert_eq!(42, my_fixture::get());
29 | }
30 |
31 | #[test]
32 | fn resolve_default() {
33 | assert_eq!(42, my_fixture::default());
34 | }
35 |
36 | #[test]
37 | fn injected_new() {
38 | assert_eq!(63, my_fixture_injected::get(21, M(3)));
39 | }
40 |
41 | #[test]
42 | fn injected_default() {
43 | assert_eq!(84, my_fixture_injected::default());
44 | }
45 |
--------------------------------------------------------------------------------
/rstest/tests/resources/fixture/from_other_module.rs:
--------------------------------------------------------------------------------
1 |
2 | mod my_mod {
3 | use rstest::{fixture};
4 |
5 | #[fixture]
6 | pub fn mod_fixture() -> u32 { 42 }
7 | }
8 |
9 | use my_mod::mod_fixture;
10 |
11 | #[test]
12 | fn struct_access() {
13 | assert_eq!(42, mod_fixture::default());
14 | }
15 |
--------------------------------------------------------------------------------
/rstest/tests/resources/fixture/impl.rs:
--------------------------------------------------------------------------------
1 | use rstest::*;
2 |
3 | #[fixture]
4 | fn fx_base_impl_return() -> impl Iterator
- { std::iter::once(42) }
5 |
6 | #[fixture]
7 | fn fx_base_impl_input(mut fx_base_impl_return: impl Iterator
- ) -> u32 {
8 | fx_base_impl_return.next().unwrap()
9 | }
10 |
11 | #[rstest]
12 | fn base_impl_return(mut fx_base_impl_return: impl Iterator
- ) {
13 | assert_eq!(42, fx_base_impl_return.next().unwrap());
14 | }
15 |
16 | #[rstest]
17 | fn base_impl_input(mut fx_base_impl_input: u32) {
18 | assert_eq!(42, fx_base_impl_input);
19 | }
20 |
21 | #[fixture]
22 | fn fx_nested_impl_return() -> impl Iterator
- { std::iter::once(42) }
23 |
24 | #[fixture]
25 | fn fx_nested_impl_input(mut fx_nested_impl_return: impl Iterator
- ) -> String {
26 | fx_nested_impl_return.next().unwrap().to_string()
27 | }
28 |
29 | #[rstest]
30 | fn nested_impl_return(mut fx_nested_impl_return: impl Iterator
- ) {
31 | assert_eq!("42", fx_nested_impl_return.next().unwrap().to_string());
32 | }
33 |
34 | #[rstest]
35 | fn nested_impl_input(mut fx_nested_impl_input: String) {
36 | assert_eq!("42", &fx_nested_impl_input);
37 | }
38 |
39 | #[fixture]
40 | fn fx_nested_multiple_impl_return() -> (impl Iterator
- , impl ToString) {
41 | (std::iter::once(42), 42i32)
42 | }
43 |
44 | #[fixture]
45 | fn fx_nested_multiple_impl_input(mut fx_nested_multiple_impl_return: (impl Iterator
- , impl ToString)) -> bool {
46 | fx_nested_multiple_impl_return.0.next().unwrap().to_string() == fx_nested_multiple_impl_return.1.to_string()
47 | }
48 |
49 | #[rstest]
50 | fn nested_multiple_impl_return(mut fx_nested_multiple_impl_return: (impl Iterator
- , impl ToString)) {
51 | assert_eq!(fx_nested_multiple_impl_return.0.next().unwrap().to_string(), fx_nested_multiple_impl_return.1.to_string());
52 | }
53 |
54 | #[rstest]
55 | fn nested_multiple_impl_input(fx_nested_multiple_impl_input: bool) {
56 | assert!(fx_nested_multiple_impl_input);
57 | }
58 |
--------------------------------------------------------------------------------
/rstest/tests/resources/fixture/no_warning.rs:
--------------------------------------------------------------------------------
1 | use rstest::*;
2 |
3 | #[fixture]
4 | fn val() -> i32 {
5 | 21
6 | }
7 |
8 | #[fixture]
9 | fn fortytwo(mut val: i32) -> i32 {
10 | val *= 2;
11 | val
12 | }
13 |
14 | #[rstest]
15 | fn the_test(fortytwo: i32) {
16 | assert_eq!(fortytwo, 42);
17 | }
18 |
--------------------------------------------------------------------------------
/rstest/tests/resources/fixture/once.rs:
--------------------------------------------------------------------------------
1 | use rstest::{fixture, rstest};
2 |
3 | #[fixture]
4 | #[once]
5 | fn once_fixture() -> u32 {
6 | eprintln!("Exec fixture() just once");
7 | 42
8 | }
9 |
10 | #[rstest]
11 | fn base(once_fixture: &u32) {
12 | assert_eq!(&42, once_fixture);
13 | }
14 |
15 | #[rstest]
16 | #[case(2)]
17 | #[case(3)]
18 | #[case(7)]
19 | fn cases(once_fixture: &u32, #[case] divisor: u32) {
20 | assert_eq!(0, *once_fixture % divisor);
21 | }
22 |
--------------------------------------------------------------------------------
/rstest/tests/resources/fixture/once_defined_type.rs:
--------------------------------------------------------------------------------
1 | use rstest::{fixture, rstest};
2 |
3 | #[fixture]
4 | #[default(u32)]
5 | #[partial_1(u32)]
6 | #[once]
7 | fn once_fixture(#[default(())] a: (), #[default(())] b: ()) -> u32 {
8 | eprintln!("Exec fixture() just once");
9 | 42
10 | }
11 |
12 | #[rstest]
13 | fn base(once_fixture: &u32) {
14 | assert_eq!(&42, once_fixture);
15 | }
16 |
17 | #[rstest]
18 | fn base_partial(#[with(())] once_fixture: &u32) {
19 | assert_eq!(&42, once_fixture);
20 | }
21 |
22 | #[rstest]
23 | fn base_complete(#[with((), ())] once_fixture: &u32) {
24 | assert_eq!(&42, once_fixture);
25 | }
26 |
27 | #[rstest]
28 | #[case(2)]
29 | #[case(3)]
30 | #[case(7)]
31 | fn cases(once_fixture: &u32, #[case] divisor: u32) {
32 | assert_eq!(0, *once_fixture % divisor);
33 | }
34 |
--------------------------------------------------------------------------------
/rstest/tests/resources/fixture/once_no_return.rs:
--------------------------------------------------------------------------------
1 | use rstest::*;
2 |
3 | #[fixture]
4 | #[once]
5 | fn once_fixture() {
6 | eprintln!("Exec fixture() just once");
7 | }
8 |
9 | #[rstest]
10 | fn base(_once_fixture: ()) {
11 | assert!(true);
12 | }
13 |
14 | #[rstest]
15 | #[case()]
16 | #[case()]
17 | #[case()]
18 | fn cases(_once_fixture: ()) {
19 | assert!(true);
20 | }
21 |
--------------------------------------------------------------------------------
/rstest/tests/resources/fixture/partial.rs:
--------------------------------------------------------------------------------
1 | use rstest::*;
2 |
3 | #[fixture]
4 | fn f1() -> u32 { 0 }
5 | #[fixture]
6 | fn f2() -> u32 { 0 }
7 | #[fixture]
8 | fn f3() -> u32 { 0 }
9 |
10 | #[fixture]
11 | fn fixture(f1: u32, f2: u32, f3: u32) -> u32 { f1 + 10 * f2 + 100 * f3 }
12 |
13 | #[fixture(fixture(7))]
14 | fn partial_1(fixture: u32) -> u32 { fixture }
15 |
16 | #[fixture(fixture(2, 4))]
17 | fn partial_2(fixture: u32) -> u32 { fixture }
18 |
19 | #[fixture(fixture(2, 4, 5))]
20 | fn complete(fixture: u32) -> u32 { fixture }
21 |
22 | #[rstest]
23 | fn default(fixture: u32) {
24 | assert_eq!(fixture, 0);
25 | }
26 |
27 | #[rstest]
28 | fn t_partial_1(partial_1: u32) {
29 | assert_eq!(partial_1, 7);
30 | }
31 |
32 | #[rstest]
33 | fn t_partial_2(partial_2: u32) {
34 | assert_eq!(partial_2, 42);
35 | }
36 |
37 | #[rstest]
38 | fn t_complete(complete: u32) {
39 | assert_eq!(complete, 542);
40 | }
41 |
--------------------------------------------------------------------------------
/rstest/tests/resources/fixture/partial_in_attr.rs:
--------------------------------------------------------------------------------
1 | use rstest::*;
2 |
3 | #[fixture]
4 | fn f1() -> u32 {
5 | 0
6 | }
7 | #[fixture]
8 | fn f2() -> u32 {
9 | 0
10 | }
11 | #[fixture]
12 | fn f3() -> u32 {
13 | 0
14 | }
15 |
16 | #[fixture]
17 | fn fixture(f1: u32, f2: u32, f3: u32) -> u32 {
18 | f1 + 10 * f2 + 100 * f3
19 | }
20 |
21 | #[fixture]
22 | fn partial_1(#[with(7)] fixture: u32) -> u32 {
23 | fixture
24 | }
25 |
26 | #[fixture]
27 | fn partial_2(#[with(2, 4)] fixture: u32) -> u32 {
28 | fixture
29 | }
30 |
31 | #[fixture]
32 | fn complete(#[with(2, 4, 5)] fixture: u32) -> u32 {
33 | fixture
34 | }
35 |
36 | #[rstest]
37 | fn default(fixture: u32) {
38 | assert_eq!(fixture, 0);
39 | }
40 |
41 | #[rstest]
42 | fn t_partial_1(partial_1: u32) {
43 | assert_eq!(partial_1, 7);
44 | }
45 |
46 | #[rstest]
47 | fn t_partial_2(partial_2: u32) {
48 | assert_eq!(partial_2, 42);
49 | }
50 |
51 | #[rstest]
52 | fn t_complete(complete: u32) {
53 | assert_eq!(complete, 542);
54 | }
55 |
--------------------------------------------------------------------------------
/rstest/tests/resources/fixture/rename.rs:
--------------------------------------------------------------------------------
1 | use rstest::*;
2 |
3 | #[fixture]
4 | fn very_long_and_boring_name(#[default(42)] inject: u32) -> u32 {
5 | inject
6 | }
7 |
8 | mod sub_module {
9 | use super::*;
10 |
11 | #[fixture]
12 | pub fn mod_fixture() -> u32 {
13 | 42
14 | }
15 | }
16 |
17 | #[fixture(very_long_and_boring_name as foo)]
18 | fn compact(foo: u32) -> u32 {
19 | foo
20 | }
21 |
22 | #[fixture(very_long_and_boring_name(21) as foo)]
23 | fn compact_injected(foo: u32) -> u32 {
24 | foo
25 | }
26 |
27 | #[fixture(sub_module::mod_fixture as foo)]
28 | fn compact_from_mod(foo: u32) -> u32 {
29 | foo
30 | }
31 |
32 | #[fixture]
33 | fn attribute(#[from(very_long_and_boring_name)] foo: u32) -> u32 {
34 | foo
35 | }
36 |
37 | #[fixture]
38 | fn attribute_mod(#[from(sub_module::mod_fixture)] foo: u32) -> u32 {
39 | foo
40 | }
41 |
42 | #[fixture]
43 | fn attribute_injected(
44 | #[from(very_long_and_boring_name)]
45 | #[with(21)]
46 | foo: u32,
47 | ) -> u32 {
48 | foo
49 | }
50 |
51 | #[rstest]
52 | fn test(
53 | compact: u32,
54 | attribute: u32,
55 | attribute_mod: u32,
56 | compact_from_mod: u32,
57 | compact_injected: u32,
58 | attribute_injected: u32,
59 | ) {
60 | assert_eq!(compact, attribute);
61 | assert_eq!(attribute, attribute_mod);
62 | assert_eq!(attribute_mod, compact_from_mod);
63 | assert_eq!(compact_injected, attribute_injected);
64 | }
65 |
--------------------------------------------------------------------------------
/rstest/tests/resources/fixture/resolve.rs:
--------------------------------------------------------------------------------
1 | use rstest::{rstest, fixture};
2 |
3 | pub trait Tr {
4 | fn get() -> Self;
5 | }
6 |
7 | impl Tr for i32 {
8 | fn get() -> Self {
9 | 42
10 | }
11 | }
12 |
13 | impl Tr for u32 {
14 | fn get() -> Self {
15 | 42
16 | }
17 | }
18 |
19 | #[fixture]
20 | pub fn f() -> T {
21 | T::get()
22 | }
23 |
24 | #[fixture]
25 | pub fn fu32(f: u32) -> u32 {
26 | f
27 | }
28 |
29 | #[fixture]
30 | pub fn fi32(f: i32) -> i32 {
31 | f
32 | }
33 |
34 | #[rstest]
35 | fn test_u32(fu32: u32) {
36 | assert_eq!(fu32, 42)
37 | }
38 |
39 | #[rstest]
40 | fn test_i32(fi32: i32) {
41 | assert_eq!(fi32, 42)
42 | }
43 |
--------------------------------------------------------------------------------
/rstest/tests/resources/fixture/simple_injection.rs:
--------------------------------------------------------------------------------
1 | use rstest::{rstest, fixture};
2 |
3 | #[fixture]
4 | fn root() -> u32 { 21 }
5 |
6 | #[fixture]
7 | fn injection(root: u32) -> u32 { 2 * root }
8 |
9 | #[rstest]
10 | fn success(injection: u32) {
11 | assert_eq!(42, injection);
12 | }
13 |
14 | #[rstest]
15 | fn fail(injection: u32) {
16 | assert_eq!(41, injection);
17 | }
18 |
--------------------------------------------------------------------------------
/rstest/tests/resources/rstest/by_ref.rs:
--------------------------------------------------------------------------------
1 | use rstest::*;
2 | use std::fs::File;
3 | use std::io::Read;
4 | use std::path::PathBuf;
5 |
6 | #[rstest]
7 | fn start_with_name(
8 | #[files("files/**/*.txt")]
9 | #[by_ref]
10 | path: &PathBuf,
11 | ) {
12 | let name = path.file_name().unwrap();
13 | let mut f = File::open(&path).unwrap();
14 | let mut contents = String::new();
15 | f.read_to_string(&mut contents).unwrap();
16 |
17 | assert!(contents.starts_with(name.to_str().unwrap()))
18 | }
19 |
20 | #[fixture]
21 | fn f() -> u32 {
22 | 42
23 | }
24 |
25 | #[rstest]
26 | #[case(42)]
27 | fn test(
28 | #[by_ref] f: &u32,
29 | #[case]
30 | #[by_ref]
31 | c: &u32,
32 | #[values(42, 142)]
33 | #[by_ref]
34 | v: &u32,
35 | ) {
36 | assert_eq!(f, c);
37 | assert_eq!(*c, *v % 100);
38 | }
39 |
--------------------------------------------------------------------------------
/rstest/tests/resources/rstest/cases/args_with_no_cases.rs:
--------------------------------------------------------------------------------
1 | use rstest::rstest;
2 |
3 | #[rstest(one, two, three)]
4 | fn should_show_error_for_no_case(one: u32, two: u32, three: u32) {}
5 |
--------------------------------------------------------------------------------
/rstest/tests/resources/rstest/cases/async.rs:
--------------------------------------------------------------------------------
1 | use rstest::*;
2 |
3 | #[rstest]
4 | #[case::pass(42, async { 42 })]
5 | #[case::fail(42, async { 41 })]
6 | #[should_panic]
7 | #[case::pass_panic(42, async { 41 })]
8 | #[should_panic]
9 | #[case::fail_panic(42, async { 42 })]
10 | #[async_std::test]
11 | async fn my_async_test(
12 | #[case] expected: u32,
13 | #[case]
14 | #[future]
15 | value: u32,
16 | ) {
17 | assert_eq!(expected, value.await);
18 | }
19 |
20 | #[rstest]
21 | #[case::pass(42, async { 42 })]
22 | #[async_std::test]
23 | async fn my_async_test_revert(
24 | #[case] expected: u32,
25 | #[future]
26 | #[case]
27 | value: u32,
28 | ) {
29 | assert_eq!(expected, value.await);
30 | }
31 |
--------------------------------------------------------------------------------
/rstest/tests/resources/rstest/cases/async_awt.rs:
--------------------------------------------------------------------------------
1 | use rstest::*;
2 |
3 | #[rstest]
4 | #[case::pass(42, async { 42 })]
5 | #[case::fail(42, async { 41 })]
6 | #[should_panic]
7 | #[case::pass_panic(42, async { 41 })]
8 | #[should_panic]
9 | #[case::fail_panic(42, async { 42 })]
10 | #[async_std::test]
11 | async fn my_async_test(
12 | #[case] expected: u32,
13 | #[case]
14 | #[future(awt)]
15 | value: u32,
16 | ) {
17 | assert_eq!(expected, value);
18 | }
19 |
20 | #[rstest]
21 | #[case::pass(42, async { 42 })]
22 | #[async_std::test]
23 | async fn my_async_test_revert(
24 | #[case] expected: u32,
25 | #[future(awt)]
26 | #[case]
27 | value: u32,
28 | ) {
29 | assert_eq!(expected, value);
30 | }
31 |
--------------------------------------------------------------------------------
/rstest/tests/resources/rstest/cases/async_awt_global.rs:
--------------------------------------------------------------------------------
1 | use rstest::*;
2 |
3 | #[rstest]
4 | #[case::pass(42, async { 42 })]
5 | #[case::fail(42, async { 41 })]
6 | #[should_panic]
7 | #[case::pass_panic(42, async { 41 })]
8 | #[should_panic]
9 | #[case::fail_panic(42, async { 42 })]
10 | #[awt]
11 | #[async_std::test]
12 | async fn my_async_test(
13 | #[case] expected: u32,
14 | #[case]
15 | #[future]
16 | value: u32,
17 | ) {
18 | assert_eq!(expected, value);
19 | }
20 |
21 | #[rstest]
22 | #[case::pass(42, async { 42 })]
23 | #[awt]
24 | #[async_std::test]
25 | async fn my_async_test_revert(
26 | #[case] expected: u32,
27 | #[future]
28 | #[case]
29 | value: u32,
30 | ) {
31 | assert_eq!(expected, value);
32 | }
33 |
--------------------------------------------------------------------------------
/rstest/tests/resources/rstest/cases/async_awt_mut.rs:
--------------------------------------------------------------------------------
1 | use rstest::*;
2 |
3 | #[rstest]
4 | #[case::pass(async { 3 })]
5 | #[awt]
6 | #[async_std::test]
7 | async fn my_mut_test_global_awt(
8 | #[future]
9 | #[case]
10 | mut a: i32,
11 | ) {
12 | a = 4;
13 | assert_eq!(a, 4);
14 | }
15 |
16 | #[rstest]
17 | #[case::pass(async { 3 })]
18 | #[async_std::test]
19 | async fn my_mut_test_local_awt(
20 | #[future(awt)]
21 | #[case]
22 | mut a: i32,
23 | ) {
24 | a = 4;
25 | assert_eq!(a, 4);
26 | }
27 |
--------------------------------------------------------------------------------
/rstest/tests/resources/rstest/cases/case_attributes.rs:
--------------------------------------------------------------------------------
1 | use rstest::rstest;
2 |
3 | #[rstest(
4 | val,
5 | case::no_panic(0),
6 | #[should_panic]
7 | case::panic(2),
8 | #[should_panic(expected="expected")]
9 | case::panic_with_message(3),
10 | case::no_panic_but_fail(1),
11 | #[should_panic]
12 | case::panic_but_fail(0),
13 | #[should_panic(expected="other")]
14 | case::panic_with_wrong_message(3),
15 | )]
16 | fn attribute_per_case(val: i32) {
17 | match val {
18 | 0 => assert!(true),
19 | 1 => assert!(false),
20 | 2 => panic!("No catch"),
21 | 3 => panic!("expected"),
22 | _ => panic!("Not defined"),
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/rstest/tests/resources/rstest/cases/case_with_wrong_args.rs:
--------------------------------------------------------------------------------
1 | use rstest::rstest;
2 |
3 | #[cfg(test)]
4 | #[rstest(a, b, case(42), case(1, 2), case(43))]
5 | fn error_less_arguments(a: u32, b: u32) {}
6 |
7 | #[cfg(test)]
8 | #[rstest(a, case(42, 43), case(12), case(24, 34))]
9 | fn error_too_much_arguments(a: u32) {}
10 |
11 |
--------------------------------------------------------------------------------
/rstest/tests/resources/rstest/cases/description.rs:
--------------------------------------------------------------------------------
1 | use rstest::rstest;
2 |
3 | #[rstest(
4 | expected,
5 | case::user_test_description(true),
6 | case(true),
7 | case::user_test_description_fail(false)
8 | )]
9 | fn description(expected: bool) {
10 | assert!(expected);
11 | }
12 |
--------------------------------------------------------------------------------
/rstest/tests/resources/rstest/cases/dump_just_one_case.rs:
--------------------------------------------------------------------------------
1 | use rstest::*;
2 |
3 | #[rstest]
4 | #[case::first_no_dump("Please don't trace me")]
5 | #[trace]
6 | #[case::dump_me("Trace it!")]
7 | #[case::last_no_dump("Please don't trace me")]
8 | fn cases(#[case] s: &str) {
9 | assert!(false);
10 | }
11 |
--------------------------------------------------------------------------------
/rstest/tests/resources/rstest/cases/inject.rs:
--------------------------------------------------------------------------------
1 | use rstest::*;
2 | use actix_rt;
3 | use std::future::Future;
4 |
5 | #[rstest(expected, value,
6 | case::pass(42, 42),
7 | #[should_panic]
8 | case::panic(41, 42),
9 | case::fail(1, 42)
10 | )]
11 | #[test]
12 | fn sync(expected: u32, value: u32) { assert_eq!(expected, value); }
13 |
14 | #[rstest(expected, value,
15 | case::pass(42, async { 42 }),
16 | #[should_panic]
17 | case::panic(41, async { 42 }),
18 | case::fail(1, async { 42 })
19 | )]
20 | #[actix_rt::test]
21 | async fn fn_async(expected: u32, value: impl Future