How to exclude a test case from command line?

I have 2 test functions defined as:

#[test]
fn test_delta_new_upsert() {
    ...
}
#[test]
fn test_delta_new_delete() {
    ...
}

While running the test from command line, like cargo test, I want to exclude test_delta_new_delete from running. Any suggestion how to do it ?

1 Like

What are you trying to do?
If you're just using test_delta_new_delete in test_delta_new_upsert or others, you can just remove the #[test] attribute.

1 Like

$ cargo test upsert
should run just test_delta_new_upsert()
https://doc.rust-lang.org/book/ch11-02-running-tests.html

1 Like

I have many test cases and I would like to filter out few of them out. Is there a way to exclude test cases by specifying a pattern ? Now deleting #[test] is that I might forget to add them back.

Thanks @JManInPhoenix, though in my case I have several test cases. Looks like my example above, which I tried to simplify, ended up confusing my question.

Hmm, perhaps you can use the #[ignore] attribute and the command to run them anyway? Or perhaps you could use a separate module, to group the ones you want to run into a single module, and all the others, into another?

4 Likes

Right now for me, writing/fixing code is a multi-day activity. I thought it would be useful if there is some way to ignore test cases from cargo command like cargo test --exclude-pat "my-patt"

1 Like

I think there is an exclude option on the test executable itself, where you can use -- to separate from cargo options. Try cargo test -- --help.

1 Like

If you want to ignore specific tests one time, you can use cargo test -- --skip test_delta_new_delete. As cuviper says, you can see the cargo test options by using cargo test -- --help.

In general think OptimisticPeach is also right. Use #[ignore] on the tests you don't want to run every time. Those tests will then be ignored. If you want to run the ignored tests you just do cargo test -- --ignored (or include-ignored to run both ignored and the other tests).

See: Controlling How Tests Are Run - The Rust Programming Language

3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.