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.
Full reproducable example. Win10, rustc 1.46.0. Just a main.rs:
#[macro_use]
extern crate tokio;
#[tokio::main]
async fn main() -> std::io::Result<()> {
Ok(())
}
#[cfg(test)]
mod tests {
#[test]
async fn test_format_str() {
let src = "a";
let expect = "a";
assert_eq!(expect, src);
}
}
with a single dependency:
[dependencies]
tokio = { version = "0.2.22", features = ["full"]}
Removing
#[macro_use]
extern crate tokio;
and using tokio macros as tokio:: ex. tokio::try_join! solves the immediate problem, although it would be nice to know why this happens.
use
statements? – Cerberus