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