Home > Mobile >  How do I separate the execution of tests via cargo test?
How do I separate the execution of tests via cargo test?

Time:03-18

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?

CodePudding user response:

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.

CodePudding user response:

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
  • Related