]> git.proxmox.com Git - rustc.git/blob - src/tools/cargo/src/doc/src/guide/tests.md
New upstream version 1.74.1+dfsg1
[rustc.git] / src / tools / cargo / src / doc / src / guide / tests.md
1 # Tests
2
3 Cargo can run your tests with the `cargo test` command. Cargo looks for tests
4 to run in two places: in each of your `src` files and any tests in `tests/`.
5 Tests in your `src` files should be unit tests and [documentation tests].
6 Tests in `tests/` should be integration-style tests. As such, you’ll need to
7 import your crates into the files in `tests`.
8
9 Here's an example of running `cargo test` in our [package][def-package], which
10 currently has no tests:
11
12 ```console
13 $ cargo test
14 Compiling regex v1.5.0 (https://github.com/rust-lang/regex.git#9f9f693)
15 Compiling hello_world v0.1.0 (file:///path/to/package/hello_world)
16 Running target/test/hello_world-9c2b65bbb79eabce
17
18 running 0 tests
19
20 test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
21 ```
22
23 If our package had tests, we would see more output with the correct number of
24 tests.
25
26 You can also run a specific test by passing a filter:
27
28 ```console
29 $ cargo test foo
30 ```
31
32 This will run any test with `foo` in its name.
33
34 `cargo test` runs additional checks as well. It will compile any examples
35 you’ve included to ensure they still compile. It also runs documentation
36 tests to ensure your code samples from documentation comments compile.
37 Please see the [testing guide][testing] in the Rust documentation for a general
38 view of writing and organizing tests. See [Cargo Targets: Tests] to learn more
39 about different styles of tests in Cargo.
40
41 [documentation tests]: ../../rustdoc/write-documentation/documentation-tests.html
42 [def-package]: ../appendix/glossary.md#package '"package" (glossary entry)'
43 [testing]: ../../book/ch11-00-testing.html
44 [Cargo Targets: Tests]: ../reference/cargo-targets.html#tests