Tests
Cargo can run your tests with the cargo test
command. Cargo looks for teststo 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 beintegration-style tests. As such, you’ll need to import your crates intothe files in tests
.
Here's an example of running cargo test
in our package, which currently hasno tests:
$ cargo test
Compiling rand v0.1.0 (https://github.com/rust-lang-nursery/rand.git#9f35b8e)
Compiling hello_world v0.1.0 (file:///path/to/package/hello_world)
Running target/test/hello_world-9c2b65bbb79eabce
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
If our package had tests, we would see more output with the correct number oftests.
You can also run a specific test by passing a filter:
$ cargo test foo
This will run any test with foo
in its name.
cargo test
runs additional checks as well. For example, it will compile anyexamples you’ve included and will also test the examples in yourdocumentation. Please see the testing guide in the Rustdocumentation for more details.