1CARGO-TEST(1)                                                    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. 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

OPTIONS

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       Binary targets are automatically built if there is an integration test
111       or benchmark. This allows an integration test to execute the binary to
112       exercise and test its behavior. The CARGO_BIN_EXE_<name> environment
113       variable
114       <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates>
115       is set when the integration test is built so that it can use the env
116       macro <https://doc.rust-lang.org/std/macro.env.html> to locate the
117       executable.
118
119       Passing target selection flags will test only the specified targets.
120
121       --lib
122           Test the package’s library.
123
124       --bin NAME...
125           Test the specified binary. This flag may be specified multiple
126           times.
127
128       --bins
129           Test all binary targets.
130
131       --example NAME...
132           Test the specified example. This flag may be specified multiple
133           times.
134
135       --examples
136           Test all example targets.
137
138       --test NAME...
139           Test the specified integration test. This flag may be specified
140           multiple times.
141
142       --tests
143           Test all targets in test mode that have the test = true manifest
144           flag set. By default this includes the library and binaries built
145           as unittests, and integration tests. Be aware that this will also
146           build any required dependencies, so the lib target may be built
147           twice (once as a unittest, and once as a dependency for binaries,
148           integration tests, etc.). Targets may be enabled or disabled by
149           setting the test flag in the manifest settings for the target.
150
151       --bench NAME...
152           Test the specified benchmark. This flag may be specified multiple
153           times.
154
155       --benches
156           Test all targets in benchmark mode that have the bench = true
157           manifest flag set. By default this includes the library and
158           binaries built as benchmarks, and bench targets. Be aware that this
159           will also build any required dependencies, so the lib target may be
160           built twice (once as a benchmark, and once as a dependency for
161           binaries, benchmarks, etc.). Targets may be enabled or disabled by
162           setting the bench flag in the manifest settings for the target.
163
164       --all-targets
165           Test all targets. This is equivalent to specifying --lib --bins
166           --tests --benches --examples.
167
168       --doc
169           Test only the library’s documentation. This cannot be mixed with
170           other target options.
171
172   Feature Selection
173       The feature flags allow you to control the enabled features for the
174       "current" package. The "current" package is the package in the current
175       directory, or the one specified in --manifest-path. If running in the
176       root of a virtual workspace, then the default features are selected for
177       all workspace members, or all features if --all-features is specified.
178
179       When no feature options are given, the default feature is activated for
180       every selected package.
181
182       --features FEATURES
183           Space or comma separated list of features to activate. These
184           features only apply to the current directory’s package. Features of
185           direct dependencies may be enabled with <dep-name>/<feature-name>
186           syntax. This flag may be specified multiple times, which enables
187           all specified features.
188
189       --all-features
190           Activate all available features of all selected packages.
191
192       --no-default-features
193           Do not activate the default feature of the current directory’s
194           package.
195
196   Compilation Options
197       --target TRIPLE
198           Test for the given architecture. The default is the host
199           architecture. The general format of the triple is
200           <arch><sub>-<vendor>-<sys>-<abi>. Run rustc --print target-list for
201           a list of supported targets.
202
203           This may also be specified with the build.target config value
204           <https://doc.rust-lang.org/cargo/reference/config.html>.
205
206           Note that specifying this flag makes Cargo run in a different mode
207           where the target artifacts are placed in a separate directory. See
208           the build cache
209           <https://doc.rust-lang.org/cargo/guide/build-cache.html>
210           documentation for more details.
211
212       --release
213           Test optimized artifacts with the release profile. See the PROFILES
214           section for details on how this affects profile selection.
215
216   Output Options
217       --target-dir DIRECTORY
218           Directory for all generated artifacts and intermediate files. May
219           also be specified with the CARGO_TARGET_DIR environment variable,
220           or the build.target-dir config value
221           <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults
222           to target in the root of the workspace.
223
224   Display Options
225       By default the Rust test harness hides output from test execution to
226       keep results readable. Test output can be recovered (e.g., for
227       debugging) by passing --nocapture to the test binaries:
228
229           cargo test -- --nocapture
230
231       -v, --verbose
232           Use verbose output. May be specified twice for "very verbose"
233           output which includes extra output such as dependency warnings and
234           build script output. May also be specified with the term.verbose
235           config value
236           <https://doc.rust-lang.org/cargo/reference/config.html>.
237
238       -q, --quiet
239           No output printed to stdout.
240
241       --color WHEN
242           Control when colored output is used. Valid values:
243
244           ·   auto (default): Automatically detect if color support is
245               available on the terminal.
246
247           ·   always: Always display colors.
248
249           ·   never: Never display colors.
250
251           May also be specified with the term.color config value
252           <https://doc.rust-lang.org/cargo/reference/config.html>.
253
254       --message-format FMT
255           The output format for diagnostic messages. Can be specified
256           multiple times and consists of comma-separated values. Valid
257           values:
258
259           ·   human (default): Display in a human-readable text format.
260
261           ·   short: Emit shorter, human-readable text messages.
262
263           ·   json: Emit JSON messages to stdout. See the reference
264               <https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages>
265               for more details.
266
267           ·   json-diagnostic-short: Ensure the rendered field of JSON
268               messages contains the "short" rendering from rustc.
269
270           ·   json-diagnostic-rendered-ansi: Ensure the rendered field of
271               JSON messages contains embedded ANSI color codes for respecting
272               rustc’s default color scheme.
273
274           ·   json-render-diagnostics: Instruct Cargo to not include rustc
275               diagnostics in in JSON messages printed, but instead Cargo
276               itself should render the JSON diagnostics coming from rustc.
277               Cargo’s own JSON diagnostics and others coming from rustc are
278               still emitted.
279
280   Manifest Options
281       --manifest-path PATH
282           Path to the Cargo.toml file. By default, Cargo searches for the
283           Cargo.toml file in the current directory or any parent directory.
284
285       --frozen, --locked
286           Either of these flags requires that the Cargo.lock file is
287           up-to-date. If the lock file is missing, or it needs to be updated,
288           Cargo will exit with an error. The --frozen flag also prevents
289           Cargo from attempting to access the network to determine if it is
290           out-of-date.
291
292           These may be used in environments where you want to assert that the
293           Cargo.lock file is up-to-date (such as a CI build) or want to avoid
294           network access.
295
296       --offline
297           Prevents Cargo from accessing the network for any reason. Without
298           this flag, Cargo will stop with an error if it needs to access the
299           network and the network is not available. With this flag, Cargo
300           will attempt to proceed without the network if possible.
301
302           Beware that this may result in different dependency resolution than
303           online mode. Cargo will restrict itself to crates that are
304           downloaded locally, even if there might be a newer version as
305           indicated in the local copy of the index. See the cargo-fetch(1)
306           command to download dependencies before going offline.
307
308           May also be specified with the net.offline config value
309           <https://doc.rust-lang.org/cargo/reference/config.html>.
310
311   Common Options
312       -h, --help
313           Prints help information.
314
315       -Z FLAG...
316           Unstable (nightly-only) flags to Cargo. Run cargo -Z help for
317           details.
318
319   Miscellaneous Options
320       The --jobs argument affects the building of the test executable but
321       does not affect how many threads are used when running the tests. The
322       Rust test harness includes an option to control the number of threads
323       used:
324
325           cargo test -j 2 -- --test-threads=2
326
327       -j N, --jobs N
328           Number of parallel jobs to run. May also be specified with the
329           build.jobs config value
330           <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults
331           to the number of CPUs.
332

PROFILES

334       Profiles may be used to configure compiler options such as optimization
335       levels and debug settings. See the reference
336       <https://doc.rust-lang.org/cargo/reference/profiles.html> for more
337       details.
338
339       Profile selection depends on the target and crate being built. By
340       default the dev or test profiles are used. If the --release flag is
341       given, then the release or bench profiles are used.
342
343       ┌────────────────────┬─────────────────┬───────────────────┐
344       │                    │                 │                   │
345       │Target              │ Default Profile │ --release Profile │
346       ├────────────────────┼─────────────────┼───────────────────┤
347       │                    │                 │                   │
348       │lib, bin, example   │ dev             release           
349       ├────────────────────┼─────────────────┼───────────────────┤
350       │                    │                 │                   │
351       │test, bench, or any │ test            bench             
352       │target              │                 │                   │
353       │in "test" or        │                 │                   │
354       │"bench" mode        │                 │                   │
355       └────────────────────┴─────────────────┴───────────────────┘
356
357       Dependencies use the dev/release profiles.
358
359       Unit tests are separate executable artifacts which use the test/bench
360       profiles. Example targets are built the same as with cargo build (using
361       the dev/release profiles) unless you are building them with the test
362       harness (by setting test = true in the manifest or using the --example
363       flag) in which case they use the test/bench profiles. Library targets
364       are built with the dev/release profiles when linked to an integration
365       test, binary, or doctest.
366

ENVIRONMENT

368       See the reference
369       <https://doc.rust-lang.org/cargo/reference/environment-variables.html>
370       for details on environment variables that Cargo reads.
371

EXIT STATUS

373       0
374           Cargo succeeded.
375
376       101
377           Cargo failed to complete.
378

EXAMPLES

380        1. Execute all the unit and integration tests of the current package:
381
382               cargo test
383
384        2. Run only a specific test within a specific integration test:
385
386               cargo test --test int_test_name -- modname::test_name
387

SEE ALSO

389       cargo(1), cargo-bench(1)
390
391
392
393                                  2020-02-19                     CARGO-TEST(1)
Impressum