]> git.proxmox.com Git - cargo.git/blame - src/doc/src/commands/cargo-test.md
Auto merge of #8712 - dtolnay:workspace, r=ehuss
[cargo.git] / src / doc / src / commands / cargo-test.md
CommitLineData
2d4aa38b
EH
1# cargo-test(1)
2
3
4
5## NAME
6
7cargo-test - Execute unit and integration tests of a package
8
9## SYNOPSIS
10
11`cargo test` [_options_] [_testname_] [`--` _test-options_]
12
13## DESCRIPTION
14
15Compile and execute unit and integration tests.
16
17The test filtering argument `TESTNAME` and all the arguments following the two
18dashes (`--`) are passed to the test binaries and thus to _libtest_ (rustc's
19built in unit-test and micro-benchmarking framework). If you're passing
20arguments to both Cargo and the binary, the ones after `--` go to the binary,
21the ones before go to Cargo. For details about libtest's arguments see the
22output of `cargo test -- --help`.
23
24As an example, this will filter for tests with `foo` in their name and run them
25on 3 threads in parallel:
26
27 cargo test foo -- --test-threads 3
28
29Tests are built with the `--test` option to `rustc` which creates an
30executable with a `main` function that automatically runs all functions
31annotated with the `#[test]` attribute in multiple threads. `#[bench]`
32annotated functions will also be run with one iteration to verify that they
33are functional.
34
35The libtest harness may be disabled by setting `harness = false` in the target
36manifest settings, in which case your code will need to provide its own `main`
37function to handle running tests.
38
39Documentation tests are also run by default, which is handled by `rustdoc`. It
40extracts code samples from documentation comments and executes them. See the
41[rustdoc book](https://doc.rust-lang.org/rustdoc/) for more information on
42writing doc tests.
43
44## OPTIONS
45
46### Test Options
47
48<dl>
49
50<dt class="option-term" id="option-cargo-test---no-run"><a class="option-anchor" href="#option-cargo-test---no-run"></a><code>--no-run</code></dt>
51<dd class="option-desc">Compile, but don't run tests.</dd>
52
53
54<dt class="option-term" id="option-cargo-test---no-fail-fast"><a class="option-anchor" href="#option-cargo-test---no-fail-fast"></a><code>--no-fail-fast</code></dt>
55<dd class="option-desc">Run all tests regardless of failure. Without this flag, Cargo will exit
56after the first executable fails. The Rust test harness will run all tests
57within the executable to completion, this flag only applies to the executable
58as a whole.</dd>
59
60
61</dl>
62
63
64### Package Selection
65
66By default, when no package selection options are given, the packages selected
67depend on the selected manifest file (based on the current working directory if
68`--manifest-path` is not given). If the manifest is the root of a workspace then
69the workspaces default members are selected, otherwise only the package defined
70by the manifest will be selected.
71
72The default members of a workspace can be set explicitly with the
73`workspace.default-members` key in the root manifest. If this is not set, a
74virtual workspace will include all workspace members (equivalent to passing
75`--workspace`), and a non-virtual workspace will include only the root crate itself.
76
77<dl>
78
79<dt class="option-term" id="option-cargo-test--p"><a class="option-anchor" href="#option-cargo-test--p"></a><code>-p</code> <em>spec</em>...</dt>
80<dt class="option-term" id="option-cargo-test---package"><a class="option-anchor" href="#option-cargo-test---package"></a><code>--package</code> <em>spec</em>...</dt>
81<dd class="option-desc">Test only the specified packages. See <a href="https://doc.rust-lang.org/cargo/commands/cargo-pkgid.md">cargo-pkgid(1)</a> for the
82SPEC format. This flag may be specified multiple times.</dd>
83
84
85<dt class="option-term" id="option-cargo-test---workspace"><a class="option-anchor" href="#option-cargo-test---workspace"></a><code>--workspace</code></dt>
86<dd class="option-desc">Test all members in the workspace.</dd>
87
88
89
90<dt class="option-term" id="option-cargo-test---all"><a class="option-anchor" href="#option-cargo-test---all"></a><code>--all</code></dt>
91<dd class="option-desc">Deprecated alias for <code>--workspace</code>.</dd>
92
93
94
95<dt class="option-term" id="option-cargo-test---exclude"><a class="option-anchor" href="#option-cargo-test---exclude"></a><code>--exclude</code> <em>SPEC</em>...</dt>
96<dd class="option-desc">Exclude the specified packages. Must be used in conjunction with the
97<code>--workspace</code> flag. This flag may be specified multiple times.</dd>
98
99
100</dl>
101
102
103### Target Selection
104
105When no target selection options are given, `cargo test` will build the
106following targets of the selected packages:
107
108- lib — used to link with binaries, examples, integration tests, and doc tests
109- bins (only if integration tests are built and required features are
110 available)
111- examples — to ensure they compile
112- lib as a unit test
113- bins as unit tests
114- integration tests
115- doc tests for the lib target
116
117The default behavior can be changed by setting the `test` flag for the target
118in the manifest settings. Setting examples to `test = true` will build and run
119the example as a test. Setting targets to `test = false` will stop them from
120being tested by default. Target selection options that take a target by name
121ignore the `test` flag and will always test the given target.
122
123Doc tests for libraries may be disabled by setting `doctest = false` for the
124library in the manifest.
125
126Binary targets are automatically built if there is an integration test or
127benchmark. This allows an integration test to execute the binary to exercise
128and test its behavior. The `CARGO_bin_EXE_<name>`
129[environment variable](../reference/environment-variables.html#environment-variables-cargo-sets-for-crates)
130is set when the integration test is built so that it can use the
131[`env` macro](https://doc.rust-lang.org/std/macro.env.html) to locate the
132executable.
133
134Passing target selection flags will test only the specified
135targets.
136
137<dl>
138
139<dt class="option-term" id="option-cargo-test---lib"><a class="option-anchor" href="#option-cargo-test---lib"></a><code>--lib</code></dt>
140<dd class="option-desc">Test the package's library.</dd>
141
142
143<dt class="option-term" id="option-cargo-test---bin"><a class="option-anchor" href="#option-cargo-test---bin"></a><code>--bin</code> <em>name</em>...</dt>
144<dd class="option-desc">Test the specified binary. This flag may be specified multiple times.</dd>
145
146
147<dt class="option-term" id="option-cargo-test---bins"><a class="option-anchor" href="#option-cargo-test---bins"></a><code>--bins</code></dt>
148<dd class="option-desc">Test all binary targets.</dd>
149
150
151
152<dt class="option-term" id="option-cargo-test---example"><a class="option-anchor" href="#option-cargo-test---example"></a><code>--example</code> <em>name</em>...</dt>
153<dd class="option-desc">Test the specified example. This flag may be specified multiple times.</dd>
154
155
156<dt class="option-term" id="option-cargo-test---examples"><a class="option-anchor" href="#option-cargo-test---examples"></a><code>--examples</code></dt>
157<dd class="option-desc">Test all example targets.</dd>
158
159
160<dt class="option-term" id="option-cargo-test---test"><a class="option-anchor" href="#option-cargo-test---test"></a><code>--test</code> <em>name</em>...</dt>
161<dd class="option-desc">Test the specified integration test. This flag may be specified
162multiple times.</dd>
163
164
165<dt class="option-term" id="option-cargo-test---tests"><a class="option-anchor" href="#option-cargo-test---tests"></a><code>--tests</code></dt>
166<dd class="option-desc">Test all targets in test mode that have the <code>test = true</code> manifest
167flag set. By default this includes the library and binaries built as
168unittests, and integration tests. Be aware that this will also build any
169required dependencies, so the lib target may be built twice (once as a
170unittest, and once as a dependency for binaries, integration tests, etc.).
171Targets may be enabled or disabled by setting the <code>test</code> flag in the
172manifest settings for the target.</dd>
173
174
175<dt class="option-term" id="option-cargo-test---bench"><a class="option-anchor" href="#option-cargo-test---bench"></a><code>--bench</code> <em>name</em>...</dt>
176<dd class="option-desc">Test the specified benchmark. This flag may be specified multiple times.</dd>
177
178
179<dt class="option-term" id="option-cargo-test---benches"><a class="option-anchor" href="#option-cargo-test---benches"></a><code>--benches</code></dt>
180<dd class="option-desc">Test all targets in benchmark mode that have the <code>bench = true</code>
181manifest flag set. By default this includes the library and binaries built
182as benchmarks, and bench targets. Be aware that this will also build any
183required dependencies, so the lib target may be built twice (once as a
184benchmark, and once as a dependency for binaries, benchmarks, etc.).
185Targets may be enabled or disabled by setting the <code>bench</code> flag in the
186manifest settings for the target.</dd>
187
188
189<dt class="option-term" id="option-cargo-test---all-targets"><a class="option-anchor" href="#option-cargo-test---all-targets"></a><code>--all-targets</code></dt>
190<dd class="option-desc">Test all targets. This is equivalent to specifying <code>--lib --bins --tests --benches --examples</code>.</dd>
191
192
193</dl>
194
195
196<dl>
197
198<dt class="option-term" id="option-cargo-test---doc"><a class="option-anchor" href="#option-cargo-test---doc"></a><code>--doc</code></dt>
199<dd class="option-desc">Test only the library's documentation. This cannot be mixed with other
200target options.</dd>
201
202
203</dl>
204
205### Feature Selection
206
207The feature flags allow you to control the enabled features for the "current"
208package. The "current" package is the package in the current directory, or the
209one specified in `--manifest-path`. If running in the root of a virtual
210workspace, then the default features are selected for all workspace members,
211or all features if `--all-features` is specified.
212
213When no feature options are given, the `default` feature is activated for
214every selected package.
215
216<dl>
217
218<dt class="option-term" id="option-cargo-test---features"><a class="option-anchor" href="#option-cargo-test---features"></a><code>--features</code> <em>features</em></dt>
219<dd class="option-desc">Space or comma separated list of features to activate. These features only
220apply to the current directory's package. Features of direct dependencies
221may be enabled with <code>&lt;dep-name&gt;/&lt;feature-name&gt;</code> syntax. This flag may be
222specified multiple times, which enables all specified features.</dd>
223
224
225<dt class="option-term" id="option-cargo-test---all-features"><a class="option-anchor" href="#option-cargo-test---all-features"></a><code>--all-features</code></dt>
226<dd class="option-desc">Activate all available features of all selected packages.</dd>
227
228
229<dt class="option-term" id="option-cargo-test---no-default-features"><a class="option-anchor" href="#option-cargo-test---no-default-features"></a><code>--no-default-features</code></dt>
230<dd class="option-desc">Do not activate the <code>default</code> feature of the current directory's package.</dd>
231
232
233</dl>
234
235
236### Compilation Options
237
238<dl>
239
240<dt class="option-term" id="option-cargo-test---target"><a class="option-anchor" href="#option-cargo-test---target"></a><code>--target</code> <em>triple</em></dt>
241<dd class="option-desc">Test for the given architecture. The default is the host
242architecture. The general format of the triple is
243<code>&lt;arch&gt;&lt;sub&gt;-&lt;vendor&gt;-&lt;sys&gt;-&lt;abi&gt;</code>. Run <code>rustc --print target-list</code> for a
244list of supported targets.</p>
245<p>This may also be specified with the <code>build.target</code>
246<a href="https://doc.rust-lang.org/cargo/reference/config.html">config value</a>.</p>
247<p>Note that specifying this flag makes Cargo run in a different mode where the
248target artifacts are placed in a separate directory. See the
249<a href="https://doc.rust-lang.org/cargo/guide/build-cache.html">build cache</a> documentation for more details.</dd>
250
251
252
253<dt class="option-term" id="option-cargo-test---release"><a class="option-anchor" href="#option-cargo-test---release"></a><code>--release</code></dt>
254<dd class="option-desc">Test optimized artifacts with the <code>release</code> profile. See the
255<a href="#profiles">PROFILES</a> section for details on how this affects profile
256selection.</dd>
257
258
259
260</dl>
261
262### Output Options
263
264<dl>
265<dt class="option-term" id="option-cargo-test---target-dir"><a class="option-anchor" href="#option-cargo-test---target-dir"></a><code>--target-dir</code> <em>directory</em></dt>
266<dd class="option-desc">Directory for all generated artifacts and intermediate files. May also be
267specified with the <code>CARGO_TARGET_DIR</code> environment variable, or the
268<code>build.target-dir</code> <a href="https://doc.rust-lang.org/cargo/reference/config.html">config value</a>. Defaults
269to <code>target</code> in the root of the workspace.</dd>
270
271
272</dl>
273
274### Display Options
275
276By default the Rust test harness hides output from test execution to keep
277results readable. Test output can be recovered (e.g., for debugging) by passing
278`--nocapture` to the test binaries:
279
280 cargo test -- --nocapture
281
282<dl>
283
284<dt class="option-term" id="option-cargo-test--v"><a class="option-anchor" href="#option-cargo-test--v"></a><code>-v</code></dt>
285<dt class="option-term" id="option-cargo-test---verbose"><a class="option-anchor" href="#option-cargo-test---verbose"></a><code>--verbose</code></dt>
286<dd class="option-desc">Use verbose output. May be specified twice for &quot;very verbose&quot; output which
287includes extra output such as dependency warnings and build script output.
288May also be specified with the <code>term.verbose</code>
289<a href="https://doc.rust-lang.org/cargo/reference/config.html">config value</a>.</dd>
290
291
292<dt class="option-term" id="option-cargo-test--q"><a class="option-anchor" href="#option-cargo-test--q"></a><code>-q</code></dt>
293<dt class="option-term" id="option-cargo-test---quiet"><a class="option-anchor" href="#option-cargo-test---quiet"></a><code>--quiet</code></dt>
294<dd class="option-desc">No output printed to stdout.</dd>
295
296
297<dt class="option-term" id="option-cargo-test---color"><a class="option-anchor" href="#option-cargo-test---color"></a><code>--color</code> <em>when</em></dt>
298<dd class="option-desc">Control when colored output is used. Valid values:</p>
299<ul>
300<li><code>auto</code> (default): Automatically detect if color support is available on the
301terminal.</li>
302<li><code>always</code>: Always display colors.</li>
303<li><code>never</code>: Never display colors.</li>
304</ul>
305<p>May also be specified with the <code>term.color</code>
306<a href="https://doc.rust-lang.org/cargo/reference/config.html">config value</a>.</dd>
307
308
309
310<dt class="option-term" id="option-cargo-test---message-format"><a class="option-anchor" href="#option-cargo-test---message-format"></a><code>--message-format</code> <em>fmt</em></dt>
311<dd class="option-desc">The output format for diagnostic messages. Can be specified multiple times
312and consists of comma-separated values. Valid values:</p>
313<ul>
314<li><code>human</code> (default): Display in a human-readable text format.</li>
315<li><code>short</code>: Emit shorter, human-readable text messages.</li>
316<li><code>json</code>: Emit JSON messages to stdout. See
317<a href="https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages">the reference</a>
318for more details.</li>
319<li><code>json-diagnostic-short</code>: Ensure the <code>rendered</code> field of JSON messages contains
320the &quot;short&quot; rendering from rustc.</li>
321<li><code>json-diagnostic-rendered-ansi</code>: Ensure the <code>rendered</code> field of JSON messages
322contains embedded ANSI color codes for respecting rustc's default color
323scheme.</li>
324<li><code>json-render-diagnostics</code>: Instruct Cargo to not include rustc diagnostics in
325in JSON messages printed, but instead Cargo itself should render the
326JSON diagnostics coming from rustc. Cargo's own JSON diagnostics and others
327coming from rustc are still emitted.</li>
328</ul></dd>
329
330
331
332</dl>
333
334### Manifest Options
335
336<dl>
337
338<dt class="option-term" id="option-cargo-test---manifest-path"><a class="option-anchor" href="#option-cargo-test---manifest-path"></a><code>--manifest-path</code> <em>path</em></dt>
339<dd class="option-desc">Path to the <code>Cargo.toml</code> file. By default, Cargo searches for the
340<code>Cargo.toml</code> file in the current directory or any parent directory.</dd>
341
342
343
344<dt class="option-term" id="option-cargo-test---frozen"><a class="option-anchor" href="#option-cargo-test---frozen"></a><code>--frozen</code></dt>
345<dt class="option-term" id="option-cargo-test---locked"><a class="option-anchor" href="#option-cargo-test---locked"></a><code>--locked</code></dt>
346<dd class="option-desc">Either of these flags requires that the <code>Cargo.lock</code> file is
347up-to-date. If the lock file is missing, or it needs to be updated, Cargo will
348exit with an error. The <code>--frozen</code> flag also prevents Cargo from
349attempting to access the network to determine if it is out-of-date.</p>
350<p>These may be used in environments where you want to assert that the
351<code>Cargo.lock</code> file is up-to-date (such as a CI build) or want to avoid network
352access.</dd>
353
354
355<dt class="option-term" id="option-cargo-test---offline"><a class="option-anchor" href="#option-cargo-test---offline"></a><code>--offline</code></dt>
356<dd class="option-desc">Prevents Cargo from accessing the network for any reason. Without this
357flag, Cargo will stop with an error if it needs to access the network and
358the network is not available. With this flag, Cargo will attempt to
359proceed without the network if possible.</p>
360<p>Beware that this may result in different dependency resolution than online
361mode. Cargo will restrict itself to crates that are downloaded locally, even
362if there might be a newer version as indicated in the local copy of the index.
363See the <a href="https://doc.rust-lang.org/cargo/commands/cargo-fetch.md">cargo-fetch(1)</a> command to download dependencies before going
364offline.</p>
365<p>May also be specified with the <code>net.offline</code> <a href="https://doc.rust-lang.org/cargo/reference/config.html">config value</a>.</dd>
366
367
368
369</dl>
370
371### Common Options
372
373<dl>
374
375<dt class="option-term" id="option-cargo-test-+toolchain"><a class="option-anchor" href="#option-cargo-test-+toolchain"></a><code>+</code><em>toolchain</em></dt>
376<dd class="option-desc">If Cargo has been installed with rustup, and the first argument to <code>cargo</code>
377begins with <code>+</code>, it will be interpreted as a rustup toolchain name (such
378as <code>+stable</code> or <code>+nightly</code>).
379See the <a href="https://github.com/rust-lang/rustup/">rustup documentation</a>
380for more information about how toolchain overrides work.</dd>
381
382
383<dt class="option-term" id="option-cargo-test--h"><a class="option-anchor" href="#option-cargo-test--h"></a><code>-h</code></dt>
384<dt class="option-term" id="option-cargo-test---help"><a class="option-anchor" href="#option-cargo-test---help"></a><code>--help</code></dt>
385<dd class="option-desc">Prints help information.</dd>
386
387
388<dt class="option-term" id="option-cargo-test--Z"><a class="option-anchor" href="#option-cargo-test--Z"></a><code>-Z</code> <em>flag</em></dt>
389<dd class="option-desc">Unstable (nightly-only) flags to Cargo. Run <code>cargo -Z help</code> for details.</dd>
390
391
392</dl>
393
394
395### Miscellaneous Options
396
397The `--jobs` argument affects the building of the test executable but does not
398affect how many threads are used when running the tests. The Rust test harness
399includes an option to control the number of threads used:
400
401 cargo test -j 2 -- --test-threads=2
402
403<dl>
404
405<dt class="option-term" id="option-cargo-test--j"><a class="option-anchor" href="#option-cargo-test--j"></a><code>-j</code> <em>N</em></dt>
406<dt class="option-term" id="option-cargo-test---jobs"><a class="option-anchor" href="#option-cargo-test---jobs"></a><code>--jobs</code> <em>N</em></dt>
407<dd class="option-desc">Number of parallel jobs to run. May also be specified with the
408<code>build.jobs</code> <a href="https://doc.rust-lang.org/cargo/reference/config.html">config value</a>. Defaults to
409the number of CPUs.</dd>
410
411
412
413</dl>
414
415## PROFILES
416
417Profiles may be used to configure compiler options such as optimization levels
418and debug settings. See [the reference](../reference/profiles.html) for more
419details.
420
421Profile selection depends on the target and crate being built. By default the
422`dev` or `test` profiles are used. If the `--release` flag is given, then the
423`release` or `bench` profiles are used.
424
425Target | Default Profile | `--release` Profile
426-------|-----------------|---------------------
427lib, bin, example | `dev` | `release`
428test, bench, or any target in "test" or "bench" mode | `test` | `bench`
429
430Dependencies use the `dev`/`release` profiles.
431
432
433Unit tests are separate executable artifacts which use the `test`/`bench`
434profiles. Example targets are built the same as with `cargo build` (using the
435`dev`/`release` profiles) unless you are building them with the test harness
436(by setting `test = true` in the manifest or using the `--example` flag) in
437which case they use the `test`/`bench` profiles. Library targets are built
438with the `dev`/`release` profiles when linked to an integration test, binary,
439or doctest.
440
441## ENVIRONMENT
442
443See [the reference](../reference/environment-variables.html) for
444details on environment variables that Cargo reads.
445
446
447## EXIT STATUS
448
449* `0`: Cargo succeeded.
450* `101`: Cargo failed to complete.
451
452
453## EXAMPLES
454
4551. Execute all the unit and integration tests of the current package:
456
457 cargo test
458
4592. Run only tests whose names match against a filter string:
460
461 cargo test name_filter
462
4633. Run only a specific test within a specific integration test:
464
465 cargo test --test int_test_name -- modname::test_name
466
467## SEE ALSO
468[cargo(1)](cargo.md), [cargo-bench(1)](cargo-bench.md)