How to change the default behavior of VS Code's "Run test"

When I create a test in VS Code, a clickable "Run test" will display.

The problem is: when I click the "Run test", all of my tests will compile, and run, it's not handy at all. Can I set it to run the only test which it is floating at?

By the way, when you add #[should_panic] below #[test], the "Run test" won't display, I think it could be a bug, or not implemented by the rust team.

I don't think it's possible to only compile what you need but the button runs only the test(s) you ask for and filter out the rest (at least on my computer).

The #[should_panic] issue is fixed in rust-analyzer (part of RLS v2.0).

Thanks for the reply.

It's OK to compile all but it's not OK to run all tests by the button, but it does happening in my computer.

I am using Win10, VS Code with extension Rust (rls) 0.6.1, it says it has rust-analyzer component, but looks like it doesn't fix the #[should_panic] issue. I couldn't tell it's using RLS v2.0 or not.

This extension is RLS v1, v2 is not finished yet.

I have the same setup, can you share your code (the test part mainly)?

Do you mean the #[should_panic] part? It's in below; when no #[should_panic], "Run test" will take the place of #[should_panic]:

fn should_panic() {

No, the part that triggers all tests.

I didn't do anything to setup the test, it's all default. I just create a tests folder, and add test files, that's all.

More specifically, I create a lib, code nothing in the lib src file; then I create the tests folder, and add some test files under the tests folder, that's all.

After playing a bit with the names, it seems that naming a test "test" will not filter anything out. Also tests are not filtered out if they have the same name but are in different files.

If you can manage to somehow get the name of the test and its fully qualified path, you can run it like so:

cargo test --package {name} --lib {path::to::test} -- --exact

According to IntelliJ.

cargo test --package fir_tree --lib reflection::foo -- --exact

VSCode is a less generous in arguments:

RLS: cargo test -- --nocapture *test*
RA: cargo test --package *package* --test *file* -- *test* --nocapture

rust-analyzer needs --exact and should be good to go.

Thanks, you are right, I made most of my tests named "test", after I changed the names, those tests are filtered out.

But it still have the annoying messages displaying those filtered out tests, though not tested. Is that a way to suppress those other tests messages?

I guess this does not work for me, I tried:
cargo test --package my_package_name --lib tests::test_file_name::test_name -- --exact

and message displayed as:
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

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