la10736 / rstest

Fixture-based test framework for Rust
Apache License 2.0
1.21k stars 43 forks source link

rstest_reuse: "the `async` keyword is missing from the function declaration" #248

Closed sharksforarms closed 6 months ago

sharksforarms commented 6 months ago

Hello, I'm getting the following error when attempting to use rstest_reuse with tokio async tests, wondering if this is currently supported?

example

use rstest_reuse::{self, *};
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    Ok(())
}

mod tests {
    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
    #[template]
    #[rstest]
    #[case(2, 2)]
    #[case(4/2, 2)]
    #[tokio::test]
    async fn two_simple_cases(#[case] a: u32,#[case] b: u32) {}

    // Here we apply the `two_simple_cases` template: That is expanded in
    // #[rstest]
    // #[case(2, 2)]
    // #[case(4/2, 2)]
    // fn it_works(#[case] a: u32,#[case] b: u32) {
    //     assert!(a == b);
    // }
    #[apply(two_simple_cases)]
    #[tokio::test]
    async fn it_works(a: u32, b: u32) {
        assert!(a == b);
    }

    // Here we reuse the `two_simple_cases` template to create two
    // other tests
    #[apply(two_simple_cases)]
    #[tokio::test]
    async fn it_fail(a: u32, b: u32) {
        assert!(a != b);
    }
}
error: the `async` keyword is missing from the function declaration
  --> src/main.rs:19:5
   |
19 |     #[rstest]
   |     ^^^^^^^^^
...
41 |     #[apply(two_simple_cases)]
   |     -------------------------- in this procedural macro expansion
   |
   = note: this error originates in the attribute macro `rstest` which comes from the expansion of the attribute macro `apply` (in Nightly builds, run with -Z macro-backtrace for more info)

error: the `async` keyword is missing from the function declaration
  --> src/main.rs:19:5
   |
19 |     #[rstest]
   |     ^^^^^^^^^
...
32 |     #[apply(two_simple_cases)]
   |     -------------------------- in this procedural macro expansion
   |
   = note: this error originates in the attribute macro `rstest` which comes from the expansion of the attribute macro `apply` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: unused import: `*`
 --> src/main.rs:1:26
  |
1 | use rstest_reuse::{self, *};
  |                          ^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: `tester2` (bin "tester2" test) generated 1 warning
error: could not compile `tester2` (bin "tester2" test) due to 2 previous errors; 1 warning emitted
sharksforarms commented 6 months ago

Ah! Figured it out, had to move the #[tokio::test]

    #[template]
+    #[tokio::test]
    #[rstest]
    #[case(2, 2)]
    #[case(4/2, 2)]
-    #[tokio::test]