如何通过货物测试分离测试的执行?
我正在开发一个 Rust 项目,该项目有很多单元测试(近 200 个)。
其中大约 20 个测试非常繁重,当与我的管道中的其他测试一起执行时(需要一个多小时),它们会产生问题。
如果我单独执行它们,它们就足够快了。
我实际上正在使用一种解决方法:
我在 Cargo.toml 上创建了一个项目功能:
skipped_tests = []
然后我启动测试:
$ cargo test
然后是其他:
$ cargo test module::my_test --features skipped_tests
是否有正确的方法或最佳实践来分离 Cargo 中的测试执行?
I'm working on a Rust project that has many units test (almost 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).
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:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将
#[ignore]
属性添加到昂贵的测试中,这会导致默认情况下跳过它们。您可以使用仅运行被忽略的测试
或使用所有测试
You can add the
#[ignore]
attribute to expensive tests, which causes them to be skipped by default.You can run only the ignored tests using
or all tests using
一种方法是将项目的这一部分放入单独的翻译单元/箱中,例如使用
Cargo.toml
,然后使用
cargo test --exclude Heavy_weight
运行测试。您可以添加多个排除项。然后使用类似于
cargo test --manifest-path=heavy_weight/Cargo.toml
的方式运行重量级测试,我建议您在
--release
中运行这些测试模式如果没有阻止程序,也许您就不必费心进行任何其他更改。One way would be to put this part of your project into a separate translation unit/crate, e.g. with
Cargo.toml
and then run tests with
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
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.