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