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:

  1. $ cargo test
  2. Compiling rand v0.1.0 (https://github.com/rust-lang-nursery/rand.git#9f35b8e)
  3. Compiling hello_world v0.1.0 (file:///path/to/package/hello_world)
  4. Running target/test/hello_world-9c2b65bbb79eabce
  5. running 0 tests
  6. 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:

  1. $ 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.