簡體   English   中英

如何在帶有屬性的 Rust 中禁用單元測試?

[英]How to disable a unit test in rust with an attribute?

一個模塊包含多個測試,如下所示:

#[cfg(test)]
mod tests_cases {

    /*
    #[test]
    fn test_1() {
        let a = 1;
        let b = 2;
        println!("{},{}", a, b);
        assert_ne!(a, b);
    }
    */

    #[test]
    fn test_2() {
        let c = 1;
        let d = 1;
        println!("currently working on this: {},{}", c, d);
        assert_eq!(c, d);
    }
}

在打印輸出( cargo test -- --nocapture )的第二個測試中工作時,我不想看到第一個測試的輸出。

是否有禁用注釋單元測試的選項?

或者是否可以選擇只運行第二個單元測試?

謝謝你。

另一種選擇是添加#[ignore]屬性。

#[ignore]
#[test]
fn test_1() {
    let a = 1;
    let b = 2;
    println!("{},{}", a, b);
    assert_ne!(a, b);
}

這為測試結果添加了一個漂亮的顏色。

test tests::test_1 ... ignored
test tests::test_2 ... ok

test result: ok. 1 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out; finished in 0.03s

如果您從不需要的測試中刪除#[test]屬性,則cargo test將忽略它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM