rust - 使用 cargo-nextest 替代 cargo test

cargo-nextest 是新一代的rust测试程序,能够极大提升测试性能,可以完全替代 cargo test 命令。

1. 安装

cargo install cargo-nextest

2. 执行测试

project
├── Cargo.toml
├── LICENSE
├── README.md
├── build.rs
├── core_utils
│   ├── Cargo.toml
│   ├── build.rs
│   ├── deny.toml
│   ├── src
│   │   ├── random
│   │   │   ├── arbitrary
│   │   │   │   ├── arbitrary.rs
│   │   │   │   ├── mod.rs
│   │   │   │   ├── option.rs
│   │   │   │   └── result.rs
│   │   │   ├── gen.rs
│   │   │   ├── mod.rs
│   │   │   └── utils.rs
│   │   │   └── lib.rs
│   ├── tests
│   │   ├── test_random.rs

tests/test_random.rs 包含两个测试函数

  • test_random_string
  • test_random_string_2

src/random/option.rs 包含测试

#[cfg(test)]
mod tests {
    use crate::random::arby;

    #[test]
    fn test_option() {
        let x = arby::<Option<bool>>(5);
        println!("{:#?}", x);
        let x = arby::<Option<bool>>(5);
        println!("{:#?}", x);
        let x = arby::<Option<bool>>(5);
        println!("{:#?}", x);
    }
}

2.1 查找所有测试

cargo nextest list
cargo nextest list test_random

2.2 找出慢测试、泄露测试,并设置超时时间,超时就自动终止

cargo nextest run --slow-timeout 60 -leak-timeout 1024

2.3 并发测试

cargo nextest run --release -- --jobs 4
cargo nextest --jobs 4

2.4 重试失败的测试用例

cargo nextest run --retries 3

2.5 运行上次失败的测试

cargo nextest run -- --failed

2.6 测试指定的包

cargo nextest run -p core_utils

2.7 测试 lib 中的所有测试用例

cd core_utils
cargo nextest run :cargo nextest run --lib

2.8 运行项目中的所有测试

cargo nextest run
# 会包含文档字符串中的测试用例
cargo nextest run --tests

2.9 测试 tests 文件夹中的指定函数(模糊匹配)

cd core_utils
cargo nextest run test_random_string
cargo nextest run -- test_random_string
cargo nextest run -E 'test(test_random_string_2)'
cargo nextest run -E 'test(test_random)'

2.10 测试 tests 文件夹中的指定函数(精确匹配)

cd core_utils
cargo nextest run -E 'test(=test_random_string)'

2.11 测试库中的指定函数

cargo nextest run --lib random::arbitrary::option::tests::test_option
cargo nextest run random::arbitrary::option::tests::test_option
cargo nextest run random::arbitrary::option::tests
cargo nextest run random::arbitrary::option::
cargo nextest run random::arbitrary:
cargo nextest run random::

2.12 测试 tests 的一个文件

cargo nextest run --test test_random
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值