跳到主要内容

test

搜索

Attribute Macro test 

Source
#[test]
展开描述

将异步函数标记为由 runtime 执行,适用于测试环境。 该宏有助于设置 Runtime,而无需用户直接使用 RuntimeBuilder

注意:这个宏设计着简单易用,靶向不需要复杂配置的应用程序。如果提供的功能不足 足使用 Builder,它提供更强大的接口。

§Multi-threaded runtime

要使用多线程 runtime,可以通过如下方式配置该宏

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn my_test() {
    assert!(true);
}

worker_threads 选项用于配置工作线程数, 默认为系统上的 CPU 数量。

注意:多线程运行时需要 rt-multi-thread 特性 标志。

§Current thread runtime

默认的测试运行时是单线程的。每个测试使用一个独立的 current-thread 运行时。

#[tokio::test]
async fn my_test() {
    assert!(true);
}

§用法

§设置运行时名称

#[tokio::test(name = "my-test-runtime")]
async fn my_test() {
    assert!(true);
}

不使用 #[tokio::test] 的等价代码

#[test]
fn my_test() {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .name("my-test-runtime")
        .build()
        .unwrap()
        .block_on(async {
            assert!(true);
        })
}

§使用多线程运行时

#[tokio::test(flavor = "multi_thread")]
async fn my_test() {
    assert!(true);
}

不使用 #[tokio::test] 的等价代码

#[test]
fn my_test() {
    tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            assert!(true);
        })
}

§使用当前线程运行时

#[tokio::test]
async fn my_test() {
    assert!(true);
}

不使用 #[tokio::test] 的等价代码

#[test]
fn my_test() {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            assert!(true);
        })
}

§设置工作线程数

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn my_test() {
    assert!(true);
}

不使用 #[tokio::test] 的等价代码

#[test]
fn my_test() {
    tokio::runtime::Builder::new_multi_thread()
        .worker_threads(2)
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            assert!(true);
        })
}

§配置运行时启动时暂停时间

#[tokio::test(start_paused = true)]
async fn my_test() {
    assert!(true);
}

不使用 #[tokio::test] 的等价代码

#[test]
fn my_test() {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .start_paused(true)
        .build()
        .unwrap()
        .block_on(async {
            assert!(true);
        })
}

注意,start_paused 需要启用 test-util 特性。

§重命名包

use tokio as tokio1;

#[tokio1::test(crate = "tokio1")]
async fn my_test() {
    println!("Hello world");
}

§配置未处理 panic 的行为

可用选项有 shutdown_runtimeignore。更多详情,请参阅 Builder::unhandled_panic

该选项仅与 current_thread 运行时兼容。

#[cfg(tokio_unstable)]
#[tokio::test(flavor = "current_thread", unhandled_panic = "shutdown_runtime")]
async fn my_test() {
    let _ = tokio::spawn(async {
        panic!("This panic will shutdown the runtime.");
    }).await;
}

不使用 #[tokio::test] 的等价代码

#[cfg(tokio_unstable)]
#[test]
fn my_test() {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .unhandled_panic(UnhandledPanic::ShutdownRuntime)
        .build()
        .unwrap()
        .block_on(async {
            let _ = tokio::spawn(async {
                panic!("This panic will shutdown the runtime.");
            }).await;
        })
}

注意:该选项依赖于 Tokio 的不稳定 API。详见不稳定特性的相关文档, 了解如何启用 Tokio 的不稳定特性。