1CARGO-TEST(1) 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. As an
20 example, this will run all tests with foo in their name on 3 threads in
21 parallel:
22
23 cargo test foo -- --test-threads 3
24
25 Tests are built with the --test option to rustc which creates an
26 executable with a main function that automatically runs all functions
27 annotated with the #[test] attribute in multiple threads. #[bench]
28 annotated functions will also be run with one iteration to verify that
29 they are functional.
30
31 The libtest harness may be disabled by setting harness = false in the
32 target manifest settings, in which case your code will need to provide
33 its own main function to handle running tests.
34
35 Documentation tests are also run by default, which is handled by
36 rustdoc. It extracts code samples from documentation comments and
37 executes them. See the rustdoc book
38 <https://doc.rust-lang.org/rustdoc/> for more information on writing
39 doc tests.
40
42 Test Options
43 --no-run
44 Compile, but don’t run tests.
45
46 --no-fail-fast
47 Run all tests regardless of failure. Without this flag, Cargo will
48 exit after the first executable fails. The Rust test harness will
49 run all tests within the executable to completion, this flag only
50 applies to the executable as a whole.
51
52 Package Selection
53 By default, when no package selection options are given, the packages
54 selected depend on the selected manifest file (based on the current
55 working directory if --manifest-path is not given). If the manifest is
56 the root of a workspace then the workspaces default members are
57 selected, otherwise only the package defined by the manifest will be
58 selected.
59
60 The default members of a workspace can be set explicitly with the
61 workspace.default-members key in the root manifest. If this is not set,
62 a virtual workspace will include all workspace members (equivalent to
63 passing --workspace), and a non-virtual workspace will include only the
64 root crate itself.
65
66 -p SPEC..., --package SPEC...
67 Test only the specified packages. See cargo-pkgid(1) for the SPEC
68 format. This flag may be specified multiple times.
69
70 --workspace
71 Test all members in the workspace.
72
73 --all
74 Deprecated alias for --workspace.
75
76 --exclude SPEC...
77 Exclude the specified packages. Must be used in conjunction with
78 the --workspace flag. This flag may be specified multiple times.
79
80 Target Selection
81 When no target selection options are given, cargo test will build the
82 following targets of the selected packages:
83
84 · lib — used to link with binaries, examples, integration tests, and
85 doc tests
86
87 · bins (only if integration tests are built and required features are
88 available)
89
90 · examples — to ensure they compile
91
92 · lib as a unit test
93
94 · bins as unit tests
95
96 · integration tests
97
98 · doc tests for the lib target
99
100 The default behavior can be changed by setting the test flag for the
101 target in the manifest settings. Setting examples to test = true will
102 build and run the example as a test. Setting targets to test = false
103 will stop them from being tested by default. Target selection options
104 that take a target by name ignore the test flag and will always test
105 the given target.
106
107 Doc tests for libraries may be disabled by setting doctest = false for
108 the library in the manifest.
109
110 Passing target selection flags will test only the specified targets.
111
112 --lib
113 Test the package’s library.
114
115 --bin NAME...
116 Test the specified binary. This flag may be specified multiple
117 times.
118
119 --bins
120 Test all binary targets.
121
122 --example NAME...
123 Test the specified example. This flag may be specified multiple
124 times.
125
126 --examples
127 Test all example targets.
128
129 --test NAME...
130 Test the specified integration test. This flag may be specified
131 multiple times.
132
133 --tests
134 Test all targets in test mode that have the test = true manifest
135 flag set. By default this includes the library and binaries built
136 as unittests, and integration tests. Be aware that this will also
137 build any required dependencies, so the lib target may be built
138 twice (once as a unittest, and once as a dependency for binaries,
139 integration tests, etc.). Targets may be enabled or disabled by
140 setting the test flag in the manifest settings for the target.
141
142 --bench NAME...
143 Test the specified benchmark. This flag may be specified multiple
144 times.
145
146 --benches
147 Test all targets in benchmark mode that have the bench = true
148 manifest flag set. By default this includes the library and
149 binaries built as benchmarks, and bench targets. Be aware that this
150 will also build any required dependencies, so the lib target may be
151 built twice (once as a benchmark, and once as a dependency for
152 binaries, benchmarks, etc.). Targets may be enabled or disabled by
153 setting the bench flag in the manifest settings for the target.
154
155 --all-targets
156 Test all targets. This is equivalent to specifying --lib --bins
157 --tests --benches --examples.
158
159 --doc
160 Test only the library’s documentation. This cannot be mixed with
161 other target options.
162
163 Feature Selection
164 When no feature options are given, the default feature is activated for
165 every selected package.
166
167 --features FEATURES
168 Space or comma separated list of features to activate. These
169 features only apply to the current directory’s package. Features of
170 direct dependencies may be enabled with <dep-name>/<feature-name>
171 syntax.
172
173 --all-features
174 Activate all available features of all selected packages.
175
176 --no-default-features
177 Do not activate the default feature of the current directory’s
178 package.
179
180 Compilation Options
181 --target TRIPLE
182 Test for the given architecture. The default is the host
183 architecture. The general format of the triple is
184 <arch><sub>-<vendor>-<sys>-<abi>. Run rustc --print target-list for
185 a list of supported targets.
186
187 This may also be specified with the build.target config value
188 <https://doc.rust-lang.org/cargo/reference/config.html>.
189
190 --release
191 Test optimized artifacts with the release profile. See the PROFILES
192 section for details on how this affects profile selection.
193
194 Output Options
195 --target-dir DIRECTORY
196 Directory for all generated artifacts and intermediate files. May
197 also be specified with the CARGO_TARGET_DIR environment variable,
198 or the build.target-dir config value
199 <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults
200 to target in the root of the workspace.
201
202 Display Options
203 By default the Rust test harness hides output from test execution to
204 keep results readable. Test output can be recovered (e.g., for
205 debugging) by passing --nocapture to the test binaries:
206
207 cargo test -- --nocapture
208
209 -v, --verbose
210 Use verbose output. May be specified twice for "very verbose"
211 output which includes extra output such as dependency warnings and
212 build script output. May also be specified with the term.verbose
213 config value
214 <https://doc.rust-lang.org/cargo/reference/config.html>.
215
216 -q, --quiet
217 No output printed to stdout.
218
219 --color WHEN
220 Control when colored output is used. Valid values:
221
222 · auto (default): Automatically detect if color support is
223 available on the terminal.
224
225 · always: Always display colors.
226
227 · never: Never display colors.
228
229 May also be specified with the term.color config value
230 <https://doc.rust-lang.org/cargo/reference/config.html>.
231
232 --message-format FMT
233 The output format for diagnostic messages. Can be specified
234 multiple times and consists of comma-separated values. Valid
235 values:
236
237 · human (default): Display in a human-readable text format.
238
239 · short: Emit shorter, human-readable text messages.
240
241 · json: Emit JSON messages to stdout.
242
243 · json-diagnostic-short: Ensure the rendered field of JSON
244 messages contains the "short" rendering from rustc.
245
246 · json-diagnostic-rendered-ansi: Ensure the rendered field of
247 JSON messages contains embedded ANSI color codes for respecting
248 rustc’s default color scheme.
249
250 · json-render-diagnostics: Instruct Cargo to not include rustc
251 diagnostics in in JSON messages printed, but instead Cargo
252 itself should render the JSON diagnostics coming from rustc.
253 Cargo’s own JSON diagnostics and others coming from rustc are
254 still emitted.
255
256 Manifest Options
257 --manifest-path PATH
258 Path to the Cargo.toml file. By default, Cargo searches in the
259 current directory or any parent directory for the Cargo.toml file.
260
261 --frozen, --locked
262 Either of these flags requires that the Cargo.lock file is
263 up-to-date. If the lock file is missing, or it needs to be updated,
264 Cargo will exit with an error. The --frozen flag also prevents
265 Cargo from attempting to access the network to determine if it is
266 out-of-date.
267
268 These may be used in environments where you want to assert that the
269 Cargo.lock file is up-to-date (such as a CI build) or want to avoid
270 network access.
271
272 --offline
273 Prevents Cargo from accessing the network for any reason. Without
274 this flag, Cargo will stop with an error if it needs to access the
275 network and the network is not available. With this flag, Cargo
276 will attempt to proceed without the network if possible.
277
278 Beware that this may result in different dependency resolution than
279 online mode. Cargo will restrict itself to crates that are
280 downloaded locally, even if there might be a newer version as
281 indicated in the local copy of the index. See the cargo-fetch(1)
282 command to download dependencies before going offline.
283
284 May also be specified with the net.offline config value
285 <https://doc.rust-lang.org/cargo/reference/config.html>.
286
287 Common Options
288 -h, --help
289 Prints help information.
290
291 -Z FLAG...
292 Unstable (nightly-only) flags to Cargo. Run cargo -Z help for
293 details.
294
295 Miscellaneous Options
296 The --jobs argument affects the building of the test executable but
297 does not affect how many threads are used when running the tests. The
298 Rust test harness includes an option to control the number of threads
299 used:
300
301 cargo test -j 2 -- --test-threads=2
302
303 -j N, --jobs N
304 Number of parallel jobs to run. May also be specified with the
305 build.jobs config value
306 <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults
307 to the number of CPUs.
308
310 Profiles may be used to configure compiler options such as optimization
311 levels and debug settings. See the reference
312 <https://doc.rust-lang.org/cargo/reference/manifest.html#the-profile-sections>
313 for more details.
314
315 Profile selection depends on the target and crate being built. By
316 default the dev or test profiles are used. If the --release flag is
317 given, then the release or bench profiles are used.
318
319 ┌────────────────────┬─────────────────┬───────────────────┐
320 │ │ │ │
321 │Target │ Default Profile │ --release Profile │
322 ├────────────────────┼─────────────────┼───────────────────┤
323 │ │ │ │
324 │lib, bin, example │ dev │ release │
325 ├────────────────────┼─────────────────┼───────────────────┤
326 │ │ │ │
327 │test, bench, or any │ test │ bench │
328 │target │ │ │
329 │in "test" or │ │ │
330 │"bench" mode │ │ │
331 └────────────────────┴─────────────────┴───────────────────┘
332
333 Dependencies use the dev/release profiles.
334
335 Unit tests are separate executable artifacts which use the test/bench
336 profiles. Example targets are built the same as with cargo build (using
337 the dev/release profiles) unless you are building them with the test
338 harness (by setting test = true in the manifest or using the --example
339 flag) in which case they use the test/bench profiles. Library targets
340 are built with the dev/release profiles when linked to an integration
341 test, binary, or doctest.
342
344 See the reference
345 <https://doc.rust-lang.org/cargo/reference/environment-variables.html>
346 for details on environment variables that Cargo reads.
347
349 0
350 Cargo succeeded.
351
352 101
353 Cargo failed to complete.
354
356 1. Execute all the unit and integration tests of the current package:
357
358 cargo test
359
360 2. Run only a specific test within a specific integration test:
361
362 cargo test --test int_test_name -- modname::test_name
363
365 cargo(1), cargo-bench(1)
366
367
368
369 2019-08-19 CARGO-TEST(1)