If you find this helpful, please like, bookmark, and follow. To keep learning along, follow this series.
11.8.1 Ignore Some Tests and Run the Rest
Some tests take a long time to run, so in most cases you may want to ignore them when running cargo test unless you explicitly run them.
For these tests, Rust provides the ignore attribute, which marks them as not run by default.
For example:
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
#[test]
#[ignore]
fn expensive_test() {
assert_eq!(5, 1 + 1 + 1 + 1 + 1)
}
}
Because expensive_test is marked with the ignore attribute, it will not run under cargo test unless you explicitly ask for it.
Here is the test output:
$ cargo test
Compiling adder v0.1.0 (file:///projects/adder)
Finished `test` profile [unoptimized +debuginfo] target(s) in 0.60s
Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4)
running 2 tests
test tests::expensive_test ... ignored
test tests::it_works ... ok
test result: ok. 1 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out; finished in 0.00s
Doc-tests adder
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
11.8.2 Run Only Ignored Tests
How do you run only the ignored tests? Add the argument cargo test -- --ignored:
$ cargo test -- --ignored
Compiling adder v0.1.0 (file:///projects/adder)
Finished `test` profile [unoptimized +debuginfo] target(s) in 0.61s
Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4)
running 1 test
test expensive_test ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out; finished in 0.00s
Doc-tests adder
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Controlling which tests the program runs ensures that cargo test returns quickly. If you have plenty of time and want to run all tests, including both ignored and non-ignored ones, use cargo test -- --include-ignored.
Top comments (0)