Test dependencies

Is there a way to order tests? Or rather, to add some dependency rules to them?

I have a library which has some low level functions, "mid-level" and then the high level functions (the public interfaces). There's no need to run the mid-level tests if the low-level tests haven't passed, and there's no need to run the high-level tests if the mid-level tests haven't passed.

This is not at all important, it's just something I'm randomly curious about. :slight_smile:

May be we can create feature-gate in Cargo.toml file like:

[features]
low_level = []
mid_level = []
high_level = []

and subsequently attribute test cases for each features.

#[cfg(feature = "low_level")]
fn test_one() {
}

#[cfg(feature = "mid_level")]
fn test_two() {
}

#[cfg(feature = "high_level")]
fn test_three() {
}

While invoking the test cases:

cargo test --features=low_level && cargo test --features=mid_level && cargo test --features=high_level

Though I am not sure whether this is the preferred way to doing it rust.

2 Likes

For my particular case this looks perfectly okay. Thanks!

You can also use a search pattern when running cargo test. It must be supplied as an argument to the test harness, so you should use --:

cargo test -- mid_level

This would only run tests that contain the string mid_level in their fully qualified path. (e.g. because they live in a module named mid_level)

3 Likes

Additionally, if you're using features they way @prataprc suggested, you can apply the attribute to an entire file by putting #![cfg(feature = "low_level")] at the top.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.