如何通过货物测试分离测试的执行?

发布于 2025-01-14 22:38:56 字数 487 浏览 1 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感情旳空白 2025-01-21 22:38:56

您可以将 #[ignore] 属性添加到昂贵的测试中,这会导致默认情况下跳过它们。

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

您可以使用仅运行被忽略的测试

cargo test -- --ignored

或使用所有测试

cargo test -- --include-ignored

You can add the #[ignore] attribute to expensive tests, which causes them to be skipped by default.

#[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
魂归处 2025-01-21 22:38:56

一种方法是将项目的这一部分放入单独的翻译单元/箱中,例如使用 Cargo.toml

[package]
name = "heavy_weight"

,然后使用 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

[package]
name = "heavy_weight"

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文