2.7. 测试 Rust 项目
使用 Cargo 构建工具测试您的 Rust 程序。首先重新构建您的项目,然后运行在项目中找到的测试。请注意,您只能测试免费的 monomorphic,且无参数。功能返回类型必须是 () 或 Result< (),E> where E: Error。
默认情况下,Rust 项目在 debug 模式中测试。有关以发行模式测试项目的详情,请参考在 发行版本模式中测试 Rust 项目。
先决条件
-
构建的 Rust 项目。
有关如何构建 Rust 项目的详情,请参考 构建 Rust 项目。
流程
-
在功能前添加 test 属性
#[test]。 要运行由 Cargo 管理的 Rust 项目的测试,请在项目目录中运行:
在 Red Hat Enterprise Linux 7 上:
$ scl enable rust-toolset-1.62 'cargo test'在 Red Hat Enterprise Linux 8 中:
$ cargo test在 Red Hat Enterprise Linux 9 中:
$ cargo test