Search code examples
unit-testingrustrust-cargo

How to run a specific unit test in Rust?


I have unit tests in a package named school-info and there is a test function called repeat_students_should_not_get_full_marks.

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

cargo test test-name will match and run tests which contain test_name though this wasn't helpful.

How can I run only the test repeat_students_should_not_get_full_marks without running all the tests? I could not find a command in the documentation to do it.


Solution

  • Using cargo test test-name filters tests that contain test-name. It is possible that it can run multiple tests. It doesn't matter if the test function is in some mod or not, it still able to execute multiple test.

    You can avoid this by adding -- --exact as argument.

    If your test is not in any mod you can simply execute like this:

    cargo test test_fn_name -- --exact
    

    Otherwise you need to provide test with full namespace:

    cargo test test_mod_name::test_fn_name -- --exact
    

    For your case the solution will be :

    cargo test --package school_info repeat_students_should_not_get_full_marks -- --exact
    

    See also: