运行 `cargo test --workspace` 并排除一个测试
Run `cargo test --workspace` and exclude one test
我有一个包含多个板条箱的工作区。我需要排除一个特定的测试。
我尝试添加环境变量检查,但这不起作用。我想 cargo test
过滤掉了环境变量。
// package1/src/lib.rs
// ...
#[cfg(test)]
mod tests {
#[test]
fn test1() {
if std::env::var("CI").is_ok() {
return;
}
// ...
}
}
然后我尝试通过各种选项传递 --exclude
参数,但其中 none 有效:
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
这将跳过包中的所有测试。
cargo test --workspace --exclude 'package1*test1'
如何 运行 除了一个工作区测试之外的所有工作区测试?
排除测试
运行ning cargo test -- --help
的帮助文件列出了有用的选项:
--skip FILTER Skip tests whose names contain FILTER (this flag can
be used multiple times)
关于test
之后的--
,参见:
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);
}
}
使用 cargo test -- --skip test_mul
运行上面的代码将得到以下输出:
running 1 test
test tests::test_add ... ok
排除特定包中的测试
如果您想在工作区内排除包的特定测试,您可以按以下方式进行,将 my_package
和 my_test
替换为适当的名称:
全部测试,但排除 my_package
cargo test --workspace --exclude my_package
然后测试 my_package
本身,通过添加 --skip my_test
:
排除特定测试
cargo test --package my_package -- --skip my_test
有关更多选项,请参阅:
默认排除测试
或者,您可以将 #[ignore]
属性添加到默认情况下不应 运行 的测试。如果您愿意,您仍然可以 运行 它们分开:
src/lib.rs
#[test]
#[ignore]
fn test_add() {
assert_eq!(add(21, 21), 42);
}
运行 测试使用 cargo test -- --ignored
:
running 1 test
test tests::test_add ... ok
如果您正在使用 Rust >= 1.51
并且想要 运行 所有测试,包括那些标有 #[ignore]
属性的测试,您可以通过 --include-ignored
。
我有一个包含多个板条箱的工作区。我需要排除一个特定的测试。
我尝试添加环境变量检查,但这不起作用。我想 cargo test
过滤掉了环境变量。
// package1/src/lib.rs
// ...
#[cfg(test)]
mod tests {
#[test]
fn test1() {
if std::env::var("CI").is_ok() {
return;
}
// ...
}
}
然后我尝试通过各种选项传递 --exclude
参数,但其中 none 有效:
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
这将跳过包中的所有测试。cargo test --workspace --exclude 'package1*test1'
如何 运行 除了一个工作区测试之外的所有工作区测试?
排除测试
运行ning cargo test -- --help
的帮助文件列出了有用的选项:
--skip FILTER Skip tests whose names contain FILTER (this flag can
be used multiple times)
关于test
之后的--
,参见:
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);
}
}
使用 cargo test -- --skip test_mul
运行上面的代码将得到以下输出:
running 1 test
test tests::test_add ... ok
排除特定包中的测试
如果您想在工作区内排除包的特定测试,您可以按以下方式进行,将 my_package
和 my_test
替换为适当的名称:
全部测试,但排除 my_package
cargo test --workspace --exclude my_package
然后测试 my_package
本身,通过添加 --skip my_test
:
cargo test --package my_package -- --skip my_test
有关更多选项,请参阅:
默认排除测试
或者,您可以将 #[ignore]
属性添加到默认情况下不应 运行 的测试。如果您愿意,您仍然可以 运行 它们分开:
src/lib.rs
#[test]
#[ignore]
fn test_add() {
assert_eq!(add(21, 21), 42);
}
运行 测试使用 cargo test -- --ignored
:
running 1 test
test tests::test_add ... ok
如果您正在使用 Rust >= 1.51
并且想要 运行 所有测试,包括那些标有 #[ignore]
属性的测试,您可以通过 --include-ignored
。