1CARGO-TEST(1)               General Commands Manual              CARGO-TEST(1)
2
3
4

NAME

6       cargo-test - Execute unit and integration tests of a package
7

SYNOPSIS

9       cargo test [options] [testname] [-- test-options]
10

DESCRIPTION

12       Compile and execute unit and integration tests.
13
14       The test filtering argument TESTNAME and all the arguments following
15       the two dashes (--) are passed to the test binaries and thus to libtest
16       (rustc's built in unit-test and micro-benchmarking framework). If
17       you're passing arguments to both Cargo and the binary, the ones after
18       -- go to the binary, the ones before go to Cargo. For details about
19       libtest's arguments see the output of cargo test -- --help.
20
21       As an example, this will filter for tests with foo in their name and
22       run them on 3 threads in parallel:
23
24           cargo test foo -- --test-threads 3
25
26       Tests are built with the --test option to rustc which creates an
27       executable with a main function that automatically runs all functions
28       annotated with the #[test] attribute in multiple threads. #[bench]
29       annotated functions will also be run with one iteration to verify that
30       they are functional.
31
32       The libtest harness may be disabled by setting harness = false in the
33       target manifest settings, in which case your code will need to provide
34       its own main function to handle running tests.
35
36       Documentation tests are also run by default, which is handled by
37       rustdoc. It extracts code samples from documentation comments and
38       executes them. See the rustdoc book
39       <https://doc.rust-lang.org/rustdoc/> for more information on writing
40       doc tests.
41

OPTIONS

43   Test Options
44       --no-run
45           Compile, but don't run tests.
46
47       --no-fail-fast
48           Run all tests regardless of failure. Without this flag, Cargo will
49           exit after the first executable fails. The Rust test harness will
50           run all tests within the executable to completion, this flag only
51           applies to the executable as a whole.
52
53   Package Selection
54       By default, when no package selection options are given, the packages
55       selected depend on the selected manifest file (based on the current
56       working directory if --manifest-path is not given). If the manifest is
57       the root of a workspace then the workspaces default members are
58       selected, otherwise only the package defined by the manifest will be
59       selected.
60
61       The default members of a workspace can be set explicitly with the
62       workspace.default-members key in the root manifest. If this is not set,
63       a virtual workspace will include all workspace members (equivalent to
64       passing --workspace), and a non-virtual workspace will include only the
65       root crate itself.
66
67       -p spec..., --package spec...
68           Test only the specified packages. See cargo-pkgid(1) for the SPEC
69           format. This flag may be specified multiple times and supports
70           common Unix glob patterns like *, ? and []. However, to avoid your
71           shell accidentally expanding glob patterns before Cargo handles
72           them, you must use single quotes or double quotes around each
73           pattern.
74
75       --workspace
76           Test all members in the workspace.
77
78       --all
79           Deprecated alias for --workspace.
80
81       --exclude SPEC...
82           Exclude the specified packages. Must be used in conjunction with
83           the --workspace flag. This flag may be specified multiple times and
84           supports common Unix glob patterns like *, ? and []. However, to
85           avoid your shell accidentally expanding glob patterns before Cargo
86           handles them, you must use single quotes or double quotes around
87           each pattern.
88
89   Target Selection
90       When no target selection options are given, cargo test will build the
91       following targets of the selected packages:
92
93       •  lib — used to link with binaries, examples, integration tests, and
94           doc tests
95
96       •  bins (only if integration tests are built and required features are
97           available)
98
99       •  examples — to ensure they compile
100
101       •  lib as a unit test
102
103       •  bins as unit tests
104
105       •  integration tests
106
107       •  doc tests for the lib target
108
109       The default behavior can be changed by setting the test flag for the
110       target in the manifest settings. Setting examples to test = true will
111       build and run the example as a test. Setting targets to test = false
112       will stop them from being tested by default. Target selection options
113       that take a target by name ignore the test flag and will always test
114       the given target.
115
116       Doc tests for libraries may be disabled by setting doctest = false for
117       the library in the manifest.
118
119       Binary targets are automatically built if there is an integration test
120       or benchmark. This allows an integration test to execute the binary to
121       exercise and test its behavior. The CARGO_BIN_EXE_<name> environment
122       variable
123       <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates>
124       is set when the integration test is built so that it can use the env
125       macro <https://doc.rust-lang.org/std/macro.env.html> to locate the
126       executable.
127
128       Passing target selection flags will test only the specified targets.
129
130       Note that --bin, --example, --test and --bench flags also support
131       common Unix glob patterns like *, ? and []. However, to avoid your
132       shell accidentally expanding glob patterns before Cargo handles them,
133       you must use single quotes or double quotes around each glob pattern.
134
135       --lib
136           Test the package's library.
137
138       --bin name...
139           Test the specified binary. This flag may be specified multiple
140           times and supports common Unix glob patterns.
141
142       --bins
143           Test all binary targets.
144
145       --example name...
146           Test the specified example. This flag may be specified multiple
147           times and supports common Unix glob patterns.
148
149       --examples
150           Test all example targets.
151
152       --test name...
153           Test the specified integration test. This flag may be specified
154           multiple times and supports common Unix glob patterns.
155
156       --tests
157           Test all targets in test mode that have the test = true manifest
158           flag set. By default this includes the library and binaries built
159           as unittests, and integration tests. Be aware that this will also
160           build any required dependencies, so the lib target may be built
161           twice (once as a unittest, and once as a dependency for binaries,
162           integration tests, etc.). Targets may be enabled or disabled by
163           setting the test flag in the manifest settings for the target.
164
165       --bench name...
166           Test the specified benchmark. This flag may be specified multiple
167           times and supports common Unix glob patterns.
168
169       --benches
170           Test all targets in benchmark mode that have the bench = true
171           manifest flag set. By default this includes the library and
172           binaries built as benchmarks, and bench targets. Be aware that this
173           will also build any required dependencies, so the lib target may be
174           built twice (once as a benchmark, and once as a dependency for
175           binaries, benchmarks, etc.). Targets may be enabled or disabled by
176           setting the bench flag in the manifest settings for the target.
177
178       --all-targets
179           Test all targets. This is equivalent to specifying --lib --bins
180           --tests --benches --examples.
181
182       --doc
183           Test only the library's documentation. This cannot be mixed with
184           other target options.
185
186   Feature Selection
187       The feature flags allow you to control which features are enabled. When
188       no feature options are given, the default feature is activated for
189       every selected package.
190
191       See the features documentation
192       <https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options>
193       for more details.
194
195       --features features
196           Space or comma separated list of features to activate. Features of
197           workspace members may be enabled with package-name/feature-name
198           syntax. This flag may be specified multiple times, which enables
199           all specified features.
200
201       --all-features
202           Activate all available features of all selected packages.
203
204       --no-default-features
205           Do not activate the default feature of the selected packages.
206
207   Compilation Options
208       --target triple
209           Test for the given architecture. The default is the host
210           architecture. The general format of the triple is
211           <arch><sub>-<vendor>-<sys>-<abi>. Run rustc --print target-list for
212           a list of supported targets.
213
214           This may also be specified with the build.target config value
215           <https://doc.rust-lang.org/cargo/reference/config.html>.
216
217           Note that specifying this flag makes Cargo run in a different mode
218           where the target artifacts are placed in a separate directory. See
219           the build cache
220           <https://doc.rust-lang.org/cargo/guide/build-cache.html>
221           documentation for more details.
222
223       --release
224           Test optimized artifacts with the release profile. See the PROFILES
225           section for details on how this affects profile selection.
226
227   Output Options
228       --target-dir directory
229           Directory for all generated artifacts and intermediate files. May
230           also be specified with the CARGO_TARGET_DIR environment variable,
231           or the build.target-dir config value
232           <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults
233           to target in the root of the workspace.
234
235   Display Options
236       By default the Rust test harness hides output from test execution to
237       keep results readable. Test output can be recovered (e.g., for
238       debugging) by passing --nocapture to the test binaries:
239
240           cargo test -- --nocapture
241
242       -v, --verbose
243           Use verbose output. May be specified twice for "very verbose"
244           output which includes extra output such as dependency warnings and
245           build script output. May also be specified with the term.verbose
246           config value
247           <https://doc.rust-lang.org/cargo/reference/config.html>.
248
249       -q, --quiet
250           No output printed to stdout.
251
252       --color when
253           Control when colored output is used. Valid values:
254
255auto (default): Automatically detect if color support is
256               available on the terminal.
257
258always: Always display colors.
259
260never: Never display colors.
261
262           May also be specified with the term.color config value
263           <https://doc.rust-lang.org/cargo/reference/config.html>.
264
265       --message-format fmt
266           The output format for diagnostic messages. Can be specified
267           multiple times and consists of comma-separated values. Valid
268           values:
269
270human (default): Display in a human-readable text format.
271               Conflicts with short and json.
272
273short: Emit shorter, human-readable text messages. Conflicts
274               with human and json.
275
276json: Emit JSON messages to stdout. See the reference
277               <https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages>
278               for more details. Conflicts with human and short.
279
280json-diagnostic-short: Ensure the rendered field of JSON
281               messages contains the "short" rendering from rustc. Cannot be
282               used with human or short.
283
284json-diagnostic-rendered-ansi: Ensure the rendered field of JSON
285               messages contains embedded ANSI color codes for respecting
286               rustc's default color scheme. Cannot be used with human or
287               short.
288
289json-render-diagnostics: Instruct Cargo to not include rustc
290               diagnostics in in JSON messages printed, but instead Cargo
291               itself should render the JSON diagnostics coming from rustc.
292               Cargo's own JSON diagnostics and others coming from rustc are
293               still emitted. Cannot be used with human or short.
294
295   Manifest Options
296       --manifest-path path
297           Path to the Cargo.toml file. By default, Cargo searches for the
298           Cargo.toml file in the current directory or any parent directory.
299
300       --frozen, --locked
301           Either of these flags requires that the Cargo.lock file is
302           up-to-date. If the lock file is missing, or it needs to be updated,
303           Cargo will exit with an error. The --frozen flag also prevents
304           Cargo from attempting to access the network to determine if it is
305           out-of-date.
306
307           These may be used in environments where you want to assert that the
308           Cargo.lock file is up-to-date (such as a CI build) or want to avoid
309           network access.
310
311       --offline
312           Prevents Cargo from accessing the network for any reason. Without
313           this flag, Cargo will stop with an error if it needs to access the
314           network and the network is not available. With this flag, Cargo
315           will attempt to proceed without the network if possible.
316
317           Beware that this may result in different dependency resolution than
318           online mode. Cargo will restrict itself to crates that are
319           downloaded locally, even if there might be a newer version as
320           indicated in the local copy of the index. See the cargo-fetch(1)
321           command to download dependencies before going offline.
322
323           May also be specified with the net.offline config value
324           <https://doc.rust-lang.org/cargo/reference/config.html>.
325
326   Common Options
327       +toolchain
328           If Cargo has been installed with rustup, and the first argument to
329           cargo begins with +, it will be interpreted as a rustup toolchain
330           name (such as +stable or +nightly). See the rustup documentation
331           <https://rust-lang.github.io/rustup/overrides.html> for more
332           information about how toolchain overrides work.
333
334       -h, --help
335           Prints help information.
336
337       -Z flag
338           Unstable (nightly-only) flags to Cargo. Run cargo -Z help for
339           details.
340
341   Miscellaneous Options
342       The --jobs argument affects the building of the test executable but
343       does not affect how many threads are used when running the tests. The
344       Rust test harness includes an option to control the number of threads
345       used:
346
347           cargo test -j 2 -- --test-threads=2
348
349       -j N, --jobs N
350           Number of parallel jobs to run. May also be specified with the
351           build.jobs config value
352           <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults
353           to the number of CPUs.
354

PROFILES

356       Profiles may be used to configure compiler options such as optimization
357       levels and debug settings. See the reference
358       <https://doc.rust-lang.org/cargo/reference/profiles.html> for more
359       details.
360
361       Profile selection depends on the target and crate being built. By
362       default the dev or test profiles are used. If the --release flag is
363       given, then the release or bench profiles are used.
364
365
366       ┌────────────────────┬─────────────────┬───────────────────┐
367       │Target              │ Default Profile │ --release Profile │
368       ├────────────────────┼─────────────────┼───────────────────┤
369       │lib, bin, example   │ dev             release           
370       ├────────────────────┼─────────────────┼───────────────────┤
371       │test, bench, or any │ test            bench             
372       │target in "test" or │                 │                   │
373       │"bench" mode        │                 │                   │
374       └────────────────────┴─────────────────┴───────────────────┘
375
376       Dependencies use the dev/release profiles.
377
378       Unit tests are separate executable artifacts which use the test/bench
379       profiles. Example targets are built the same as with cargo build (using
380       the dev/release profiles) unless you are building them with the test
381       harness (by setting test = true in the manifest or using the --example
382       flag) in which case they use the test/bench profiles. Library targets
383       are built with the dev/release profiles when linked to an integration
384       test, binary, or doctest.
385

ENVIRONMENT

387       See the reference
388       <https://doc.rust-lang.org/cargo/reference/environment-variables.html>
389       for details on environment variables that Cargo reads.
390

EXIT STATUS

3920: Cargo succeeded.
393
394101: Cargo failed to complete.
395

EXAMPLES

397        1. Execute all the unit and integration tests of the current package:
398
399               cargo test
400
401        2. Run only tests whose names match against a filter string:
402
403               cargo test name_filter
404
405        3. Run only a specific test within a specific integration test:
406
407               cargo test --test int_test_name -- modname::test_name
408

SEE ALSO

410       cargo(1), cargo-bench(1)
411
412
413
414                                                                 CARGO-TEST(1)
Impressum