How to run a specific unit test in Rust

I have unit tests in a cargo named school-info and there is a test method called repeat_students_should_not_get_full_marks() .

I can run all tests in the module by cargo test --package school_info .

How may I only run the test repeat_students_should_not_get_full_marks other than running all the tests? I could not find a command in Rust documentation to do it .What will be the command?

cargo test name_of_test

From the cargo test --help:

USAGE:
    cargo test [OPTIONS] [TESTNAME] [-- <args>...]

@baumanj Negative it will run all the tests which similar to that name

<TESTNAME> If specified, only run tests containing this string in their names

If you need to run only that one, I think you need to make the test name unique.

It clearly says If specified, only run tests containing this string in their names so this wont work . I tried out this

You can use the --exact argument. For example:

cargo test -- --exact mymodule::mytest
3 Likes

Yep thats me asking there

Add --all option

cargo test --all func_name_of_test

would do the job.

--all is very important here.

This helps if you have lots of crates in your code base.

Drawbacks

If you name tests like..

  • func_name_of_test1
  • func_name_of_test2
  • func_name_of_test3

it will execute all of them.

It also shows skipped tests

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/xxxx