I have a workspace with several crates. I need to exclude a particular test.
I tried adding an environment variable check, but this doesn't work. I guess cargo test
filters out the environment variables.
// package1/src/lib.rs
// ...
#[cfg(test)]
mod tests {
#[test]
fn test1() {
if std::env::var("CI").is_ok() {
return;
}
// ...
}
}
Then I tried passing the --exclude
parameter with various options, but none of them work:
cargo test --workspace --exclude test1
cargo test --workspace --exclude tests:test1
cargo test --workspace --exclude tests::test1
cargo test --workspace --exclude '*test1'
cargo test --workspace --exclude 'tests*test1'
cargo test --workspace --exclude package1
This skips all of the tests in the package.cargo test --workspace --exclude 'package1*test1'
How can I run all workspace tests except one?
The help file by running cargo test -- --help
lists useful options:
--skip FILTER Skip tests whose names contain FILTER (this flag can
be used multiple times)
Regarding the --
after test
, see:
src/lib.rs
fn add(a: u64, b: u64) -> u64 {
a + b
}
fn mul(a: u64, b: u64) -> u64 {
a * b
}
#[cfg(test)]
mod tests {
use super::{add, mul};
#[test]
fn test_add() {
assert_eq!(add(21, 21), 42);
}
#[test]
fn test_mul() {
assert_eq!(mul(21, 2), 42);
}
}
Runing the above with cargo test -- --skip test_mul
will give the following output:
running 1 test
test tests::test_add ... ok
If you want to exclude a specific test for package within a workspace, you can do so in the following way, replacing my_package
and my_test
with their appropriate names:
Test all, but exclude my_package
cargo test --workspace --exclude my_package
And then test my_package
itself, with the specific test excluded by adding --skip my_test
:
cargo test --package my_package -- --skip my_test
For more options, see:
Alternatively, you could add the #[ignore]
attribute to tests that should not run by default. You can still run them separately if you wish to do so:
src/lib.rs
#[test]
#[ignore]
fn test_add() {
assert_eq!(add(21, 21), 42);
}
Running the tests using cargo test -- --ignored
:
running 1 test
test tests::test_add ... ok
If you're using Rust >= 1.51
and want to run all tests, including those marked with the #[ignore]
attribute, you can pass --include-ignored
.