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.
You'll put unit tests in the src directory in each file with the code that they're testing. The convention is to create a module named tests in each file to contain the test functions and to annotate the module with cfg(test) .
Cargo can run your tests with the cargo test command. Cargo looks for tests to run in two places: in each of your src files and any tests in tests/ . Tests in your src files should be unit tests, and tests in tests/ should be integration-style tests. As such, you'll need to import your crates into the files in tests .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With