[英]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):我正在尝试在我的项目 (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.这对我来说毫无意义,因为 async 在那里。
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.我正在使用 tokio = { version = "0.2.22", features = ["full"]},从 src/main.rs 导出宏。
I tried use test::test;我试过使用 test::test; to get the std test macro but that gives an ambiguous import compilation error.获取 std 测试宏,但这会产生不明确的导入编译错误。
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.我查看了这篇文章Rust 单元测试中的错误:“函数声明中缺少 async 关键字”,但据我所知,它没有解决我的问题,我需要宏导出。
Full reproducable example.完全可重现的示例。 Win10, rustc 1.46.0. Win10,rustc 1.46.0。 Just a main.rs:只是一个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 宏作为 tokio:: ex。 tokio::try_join! tokio::try_join! solves the immediate problem, although it would be nice to know why this happens.解决了眼前的问题,尽管很高兴知道为什么会发生这种情况。
This is a bug in tokio_macros
, versions 0.2.4 and 0.2.5.这是tokio_macros
版本 0.2.4 和 0.2.5 中的一个错误。 The following minimal example also fails to build:以下最小示例也无法构建:
use tokio::test;
#[test]
async fn it_works() {}
The underlying problem is with the code this test macro expands to.根本问题在于此测试宏扩展到的代码。 In the currently released version, it is roughly the following:在目前发布的版本中,大致是这样的:
#[test]
fn it_works() {
tokio::runtime::Builder::new()
.basic_scheduler()
.enable_all()
.build()
.unwrap()
.block_on(async { {} })
}
Note the #[test]
attribute.注意#[test]
属性。 It is intended to refer to the standard test
attribute, ie to the ordinary test function marker, but, since tokio::test
is in scope, it is invoked instead again - and, since the new function is not async, it throws an error.它旨在引用标准test
属性,即普通测试函数标记,但是,由于tokio::test
在范围内,它会再次被调用 - 而且,由于新函数不是异步的,它会引发错误.
The issue was fixed with this commit , where test
is replaced with ::core::prelude::v1::test
, ie explicitly pulled in from core
. 此提交修复了该问题,其中test
替换为::core::prelude::v1::test
,即从core
显式拉入。 But the corresponding change didn't make it yet into the released version, and I suspect this won't be fast, since this is technically a breaking change - bumping minimally supported Rust version.但是相应的更改还没有进入发布版本,我怀疑这不会很快,因为这在技术上是一个突破性的更改 - 冲击最低支持的 Rust 版本。
For now, the only workaround seems not to use wildcard imports with tokio
, either explicitly or through macro_use
, and use
anything you need explicitly instead.目前,唯一的解决方法似乎不是明确地或通过macro_use
使用通配符导入tokio
,而是use
您需要明确的任何东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.