[英]Run `cargo test --workspace` and exclude one test
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.我猜
cargo test
会过滤掉环境变量。
// 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:然后我尝试使用各种选项传递
--exclude
参数,但它们都不起作用:
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
这将跳过 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:运行
cargo test -- --help
的帮助文件列出了有用的选项:
--skip FILTER Skip tests whose names contain FILTER (this flag can
be used multiple times)
Regarding the --
after test
, see:关于
--
test
,请参见:
src/lib.rs 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:使用
cargo test -- --skip test_mul
将给出以下 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:如果要在工作区中排除 package 的特定测试,可以通过以下方式执行此操作,将
my_package
和my_test
替换为相应的名称:
Test all, but exclude my_package
测试所有,但排除
my_package
cargo test --workspace --exclude my_package
And then test my_package
itself, with the specific test excluded by adding --skip my_test
:然后测试
my_package
本身,通过添加排除特定测试--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.或者,您可以将
#[ignore]
属性添加到默认情况下不应运行的测试。 You can still run them separately if you wish to do so:如果您愿意,您仍然可以单独运行它们:
src/lib.rs src/lib.rs
#[test]
#[ignore]
fn test_add() {
assert_eq!(add(21, 21), 42);
}
Running the tests using cargo test -- --ignored
:使用
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
.如果您使用 Rust >=
1.51
并且想要运行所有测试,包括标有#[ignore]
属性的测试,您可以通过--include-ignored
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.