简体   繁体   English

如何通过 cargo test 分离测试的执行?

[英]How do I separate the execution of tests via cargo test?

I'm working on a Rust project that has many units test (almost 200).我正在从事一个 Rust 项目,该项目有很多单元测试(将近 200 个)。

About 20 of those tests are really heavy and they create problems when executed with the others in my pipeline (take more than an hour).这些测试中大约有 20 个非常繁重,并且在与我的管道中的其他测试一起执行时会产生问题(需要一个多小时)。

If I execute them separately they are fast enough.如果我分别执行它们,它们就足够快了。

I'm actually using a workaround:我实际上正在使用一种解决方法:

I've create a project feature on my Cargo.toml:我在 Cargo.toml 上创建了一个项目功能:

skipped_tests = []

Then I launch my test with:然后我启动我的测试:

$ cargo test

then the others:然后是其他人:

$ cargo test module::my_test --features skipped_tests

Is there a proper way or a best practice to separate the tests execution in Cargo?是否有适当的方法或最佳实践来分离 Cargo 中的测试执行?

You can add the #[ignore] attribute to expensive tests, which causes them to be skipped by default.您可以将#[ignore]属性添加到昂贵的测试中,这会导致它们在默认情况下被跳过。

#[ignore]
#[test]
fn test_something() {
    assert_eq!(1 + 1, 2);
}

You can run only the ignored tests using您可以使用仅运行被忽略的测试

cargo test -- --ignored

or all tests using或所有测试使用

cargo test -- --include-ignored

One way would be to put this part of your project into a separate translation unit/crate, eg with Cargo.toml一种方法是将项目的这一部分放入单独的翻译单元/板条箱中,例如使用Cargo.toml

[package]
name = "heavy_weight"

and then run tests with cargo test --exclude heavy_weight .然后使用cargo test --exclude heavy_weight运行测试。 You can add several exclusions.您可以添加多个排除项。

Then run the heavy-weight ones with something along the lines of cargo test --manifest-path=heavy_weight/Cargo.toml然后用cargo test --manifest-path=heavy_weight/Cargo.toml

I recommend you run those tests in --release mode if there are no blockers, maybe it will be enough for you to not bother with any other changes.如果没有阻止程序,我建议您在--release模式下运行这些测试,也许这足以让您不必为任何其他更改而烦恼。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM