dimanche 27 septembre 2020

Rust, Test does not compile: "the async keyword is missing from the function declaration"

I'm trying to get working tests in my project (src/subdir/subdir2/file.rs):

#[cfg(test)]
mod tests {
    #[tokio::test]
    async fn test_format_str() {
        let src = "a";
        let expect = "a";
        assert_eq!(expect, src);
    }
}

And get this error compiling:

error: the async keyword is missing from the function declaration
   --> src\domain\models\product.rs:185:11
    |
185 |     async fn test_format_str() {
    |           ^^

error: aborting due to previous error

Which makes no sense to me since async is there.

My original plan was this:

#[cfg(test)]
mod tests {
    #[test]
    fn test_format_str() {
        let src = "a";
        let expect = "a";
        assert_eq!(expect, src);
    }
}

Since all tests aren't async, but that gives the same error:

error: the async keyword is missing from the function declaration
   --> src\domain\models\product.rs:185:5
    |
185 |     fn test_format_str() {
    |     ^^

error: aborting due to previous error

I'm using tokio = { version = "0.2.22", features = ["full"]}, exporting macros from src/main.rs.

I tried use test::test; to get the std test macro but that gives an ambiguous import compilation error.

I checked out this post Error in Rust unit test: "The async keyword is missing from the function declaration" but it doesn't address my issue as far as I can tell, I need the macro export.

Aucun commentaire:

Enregistrer un commentaire