Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a specific unit test in Rust?

Tags:

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.

like image 379
Gayan Kalanamith Avatar asked Feb 08 '19 03:02

Gayan Kalanamith


People also ask

Where do I place unit tests in Rust?

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) .

How do you run a cargo 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 .


1 Answers

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 
like image 112
Ömer Erden Avatar answered Oct 29 '22 04:10

Ömer Erden