【Rust自学】11.7. 按测试的名称运行测试

喜欢的话别忘了点赞、收藏加关注哦(加关注即可阅读全文),对接下来的教程有兴趣的可以关注专栏。谢谢喵!(=・ω・=)

11.7.1. 按名称运行测试的子集

如果想要选择运行的测试,就将测试的名称(一个或多个)作为cargo test的参数。

看个例子:

rust 复制代码
pub fn add_two(a: usize) -> usize {
    a + 2
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn add_two_and_two() {
        let result = add_two(2);
        assert_eq!(result, 4);
    }

    #[test]
    fn add_three_and_two() {
        let result = add_two(3);
        assert_eq!(result, 5);
    }

    #[test]
    fn one_hundred() {
        let result = add_two(100);
        assert_eq!(result, 102);
    }
}

这里有三个测试,假如我们只想要测试one_hundred这个参数,就这么写:cargo test onne_hundred:

复制代码
$ cargo test one_hundred
   Compiling adder v0.1.0 (file:///projects/adder)
    Finished `test` profile [unoptimized + debuginfo] target(s) in 0.69s
     Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4)

running 1 test
test tests::one_hundred ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 2 filtered out; finished in 0.00s

运行单个测试直接指定测试名就可以。运行多个测试指定测试名的一部分(模块名也可以)作为参数,这样任何匹配这一名称的测试都会被执行。

举个例子,假如我想要执行add_two_and_two()add_three_and_two,这两个测试的名称都含有add这个部分,就写:cargo test add:

复制代码
$ cargo test add
   Compiling adder v0.1.0 (file:///projects/adder)
    Finished `test` profile [unoptimized + debuginfo] target(s) in 0.61s
     Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4)

running 2 tests
test tests::add_three_and_two ... ok
test tests::add_two_and_two ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out; finished in 0.00s
相关推荐
踩着两条虫14 小时前
「AI + 低代码」的可视化设计器
开发语言·前端·低代码·设计模式·架构
JoneBB14 小时前
ABAP Webservice连接
运维·开发语言·数据库·学习
Spider Cat 蜘蛛猫14 小时前
Springboot SSO系统设计文档
java·spring boot·后端
即使再小的船也能远航15 小时前
【Python】安装
开发语言·python
Irissgwe15 小时前
类与对象(三)
开发语言·c++·类和对象·友元
雪度娃娃15 小时前
转向现代C++——优先选用nullptr而不是0和NULL
开发语言·c++
zyk_computer16 小时前
AI 时代,或许 Rust 比 Python 更合适
人工智能·后端·python·ai·rust·ai编程·vibe coding
萌新小码农‍16 小时前
python装饰器
开发语言·前端·python
KK溜了溜了16 小时前
Python从入门到精通
服务器·开发语言·python
雨辰AI16 小时前
SpringBoot3 项目国产化改造完整流程|从 MySQL 到人大金仓落地
java·数据库·后端·mysql·政务