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 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
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
255 · auto (default): Automatically detect if color support is
256 available on the terminal.
257
258 · always: Always display colors.
259
260 · never: 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
270 · human (default): Display in a human-readable text format.
271
272 · short: Emit shorter, human-readable text messages.
273
274 · json: Emit JSON messages to stdout. See the reference
275 <https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages>
276 for more details.
277
278 · json-diagnostic-short: Ensure the rendered field of JSON
279 messages contains the "short" rendering from rustc.
280
281 · json-diagnostic-rendered-ansi: Ensure the rendered field of JSON
282 messages contains embedded ANSI color codes for respecting
283 rustc's default color scheme.
284
285 · json-render-diagnostics: Instruct Cargo to not include rustc
286 diagnostics in in JSON messages printed, but instead Cargo
287 itself should render the JSON diagnostics coming from rustc.
288 Cargo's own JSON diagnostics and others coming from rustc are
289 still emitted.
290
291 Manifest Options
292 --manifest-path path
293 Path to the Cargo.toml file. By default, Cargo searches for the
294 Cargo.toml file in the current directory or any parent directory.
295
296 --frozen, --locked
297 Either of these flags requires that the Cargo.lock file is
298 up-to-date. If the lock file is missing, or it needs to be updated,
299 Cargo will exit with an error. The --frozen flag also prevents
300 Cargo from attempting to access the network to determine if it is
301 out-of-date.
302
303 These may be used in environments where you want to assert that the
304 Cargo.lock file is up-to-date (such as a CI build) or want to avoid
305 network access.
306
307 --offline
308 Prevents Cargo from accessing the network for any reason. Without
309 this flag, Cargo will stop with an error if it needs to access the
310 network and the network is not available. With this flag, Cargo
311 will attempt to proceed without the network if possible.
312
313 Beware that this may result in different dependency resolution than
314 online mode. Cargo will restrict itself to crates that are
315 downloaded locally, even if there might be a newer version as
316 indicated in the local copy of the index. See the cargo-fetch(1)
317 command to download dependencies before going offline.
318
319 May also be specified with the net.offline config value
320 <https://doc.rust-lang.org/cargo/reference/config.html>.
321
322 Common Options
323 +toolchain
324 If Cargo has been installed with rustup, and the first argument to
325 cargo begins with +, it will be interpreted as a rustup toolchain
326 name (such as +stable or +nightly). See the rustup documentation
327 <https://rust-lang.github.io/rustup/overrides.html> for more
328 information about how toolchain overrides work.
329
330 -h, --help
331 Prints help information.
332
333 -Z flag
334 Unstable (nightly-only) flags to Cargo. Run cargo -Z help for
335 details.
336
337 Miscellaneous Options
338 The --jobs argument affects the building of the test executable but
339 does not affect how many threads are used when running the tests. The
340 Rust test harness includes an option to control the number of threads
341 used:
342
343 cargo test -j 2 -- --test-threads=2
344
345 -j N, --jobs N
346 Number of parallel jobs to run. May also be specified with the
347 build.jobs config value
348 <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults
349 to the number of CPUs.
350
352 Profiles may be used to configure compiler options such as optimization
353 levels and debug settings. See the reference
354 <https://doc.rust-lang.org/cargo/reference/profiles.html> for more
355 details.
356
357 Profile selection depends on the target and crate being built. By
358 default the dev or test profiles are used. If the --release flag is
359 given, then the release or bench profiles are used.
360
361
362 ┌────────────────────┬─────────────────┬───────────────────┐
363 │Target │ Default Profile │ --release Profile │
364 ├────────────────────┼─────────────────┼───────────────────┤
365 │lib, bin, example │ dev │ release │
366 ├────────────────────┼─────────────────┼───────────────────┤
367 │test, bench, or any │ test │ bench │
368 │target in "test" or │ │ │
369 │"bench" mode │ │ │
370 └────────────────────┴─────────────────┴───────────────────┘
371
372 Dependencies use the dev/release profiles.
373
374 Unit tests are separate executable artifacts which use the test/bench
375 profiles. Example targets are built the same as with cargo build (using
376 the dev/release profiles) unless you are building them with the test
377 harness (by setting test = true in the manifest or using the --example
378 flag) in which case they use the test/bench profiles. Library targets
379 are built with the dev/release profiles when linked to an integration
380 test, binary, or doctest.
381
383 See the reference
384 <https://doc.rust-lang.org/cargo/reference/environment-variables.html>
385 for details on environment variables that Cargo reads.
386
388 · 0: Cargo succeeded.
389
390 · 101: Cargo failed to complete.
391
393 1. Execute all the unit and integration tests of the current package:
394
395 cargo test
396
397 2. Run only tests whose names match against a filter string:
398
399 cargo test name_filter
400
401 3. Run only a specific test within a specific integration test:
402
403 cargo test --test int_test_name -- modname::test_name
404
406 cargo(1), cargo-bench(1)
407
408
409
410 CARGO-TEST(1)