[英]How to move tests into a separate file for binaries in Rust's Cargo?
[英]How do I separate the execution of tests via cargo test?
我正在从事一个 Rust 项目,该项目有很多单元测试(将近 200 个)。
这些测试中大约有 20 个非常繁重,并且在与我的管道中的其他测试一起执行时会产生问题(需要一个多小时)。
如果我分别执行它们,它们就足够快了。
我实际上正在使用一种解决方法:
我在 Cargo.toml 上创建了一个项目功能:
skipped_tests = []
然后我启动我的测试:
$ cargo test
然后是其他人:
$ cargo test module::my_test --features skipped_tests
是否有适当的方法或最佳实践来分离 Cargo 中的测试执行?
您可以将#[ignore]
属性添加到昂贵的测试中,这会导致它们在默认情况下被跳过。
#[ignore]
#[test]
fn test_something() {
assert_eq!(1 + 1, 2);
}
您可以使用仅运行被忽略的测试
cargo test -- --ignored
或所有测试使用
cargo test -- --include-ignored
一种方法是将项目的这一部分放入单独的翻译单元/板条箱中,例如使用Cargo.toml
[package]
name = "heavy_weight"
然后使用cargo test --exclude heavy_weight
运行测试。 您可以添加多个排除项。
然后用cargo test --manifest-path=heavy_weight/Cargo.toml
如果没有阻止程序,我建议您在--release
模式下运行这些测试,也许这足以让您不必为任何其他更改而烦恼。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.