测试不编译:"the async keyword is missing from the function declaration"



我正试图在我的项目中进行工作测试(src/subdir/subdr2/file.rs(:

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

并得到这个错误编译:

error: the async keyword is missing from the function declaration
--> srcdomainmodelsproduct.rs:185:11
|
185 |     async fn test_format_str() {
|           ^^
error: aborting due to previous error

这对我来说毫无意义,因为存在异步。

我最初的计划是:

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

由于所有测试都不是异步的,但这会产生相同的错误:

error: the async keyword is missing from the function declaration
--> srcdomainmodelsproduct.rs:185:5
|
185 |     fn test_format_str() {
|     ^^
error: aborting due to previous error

我正在使用tokio={version="0.2.22",features=[quot;full"]},从src/main.rs导出宏。

我尝试使用测试::test;获取std测试宏,但这会产生一个不明确的导入编译错误。

我查看了Rust单元测试中的错误:;函数声明"中缺少async关键字;但据我所知,它并没有解决我的问题,我需要宏导出。

完整的可复制示例。Win10,防锈1.46.0。只是一个主要.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);
}
}

具有单一依赖项:

[dependencies]
tokio = { version = "0.2.22", features = ["full"]}

删除

#[macro_use]
extern crate tokio;

并使用tokio宏作为tokio::ex.tokio::try_join!解决了眼前的问题,尽管很高兴知道为什么会发生这种情况。

这是tokio_macros0.2.4和0.2.5版本中的一个错误。以下最小示例也无法构建:

use tokio::test;
#[test]
async fn it_works() {}

潜在的问题是这个测试宏扩展到的代码。在当前发布的版本中,大致如下:

#[test]
fn it_works() {
tokio::runtime::Builder::new()
.basic_scheduler()
.enable_all()
.build()
.unwrap()
.block_on(async { {} })
}

注意#[test]属性。它旨在引用标准的test属性,即普通的测试函数标记,但由于tokio::test在作用域中,因此会再次调用它,并且由于新函数不是异步的,因此会引发错误。

该问题已通过此提交得到修复,其中test::core::prelude::v1::test替换,即从core显式引入。但相应的更改还没有进入发布版本,我怀疑这不会很快,因为从技术上讲,这是一个突破性的更改——冲击了最低支持的Rust版本
目前,唯一的解决方法似乎不是将通配符导入与tokio一起使用,无论是显式导入还是通过macro_use导入,以及use,而是显式导入任何您需要的内容。

相关内容

最新更新