63

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.

Ömer Erden
  • 7,680
  • 5
  • 36
  • 45
not 0x12
  • 19,360
  • 22
  • 67
  • 133

1 Answers1

82

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
Ömer Erden
  • 7,680
  • 5
  • 36
  • 45
  • 2
    Not sure if this was something that changed in the last few years, but `cargo test test-name` does not filter for tests that _start with_ `test-name`, but instead filter for tests which _contain_ `test-name`. – Mark Hildreth Nov 17 '21 at 22:54
  • 1
    @MarkHildreth I am not sure also, I might have misjudge back then but thanks for pointing out, edited. – Ömer Erden Nov 18 '21 at 07:24
  • This doesn't work anymore- `unrecognized option --exact` – Jim Mar 24 '23 at 14:03
  • 2
    @Jim are you using `cargo` to execute tests? or `rustc` ? If you are using `rustc` you need to compile your source first: `rustc --test src/test_file.rs` then execute the test with running the compiled binary, example: `.\test_file.exe --exact test_name` – Ömer Erden Mar 24 '23 at 15:31