[英]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
參數,但它們都不起作用:
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
這將跳過 package 中的所有測試。cargo test --workspace --exclude 'package1*test1'
我如何運行除一個之外的所有工作區測試?
運行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
將給出以下 output:
running 1 test
test tests::test_add ... ok
如果要在工作區中排除 package 的特定測試,可以通過以下方式執行此操作,將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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.