26

A module contains multiple tests like following:

#[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);
    }
}

When working on the second tests with output visible (cargo test -- --nocapture), I do not want to see the output of the first tests.

Is there an option to disable the commented unit test? Or is there an option to just run the second unit test?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
chriamue
  • 961
  • 5
  • 15
  • 3
    If you only want the output of a single test, you can request this like `cargo test module::tests::test_2` – Kendas Oct 31 '21 at 05:43
  • 3
    Does this answer your question? [How to run a specific unit test in Rust?](https://stackoverflow.com/questions/54585804/how-to-run-a-specific-unit-test-in-rust) – Ömer Erden Oct 31 '21 at 06:40

2 Answers2

46

Another option is to add the #[ignore] attribute.

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

This adds a nice colored ignored to the test results.

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

Source: Ignoring Some Tests Unless Specifically Requested

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
chriamue
  • 961
  • 5
  • 15
6

If you remove the #[test] attribute from the unwanted test, then cargo test will ignore it.

Refael Sheinker
  • 713
  • 7
  • 20