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 current working directory. In the root of a
55       virtual workspace, all workspace members are selected (--all is
56       implied). Otherwise, only the package in the current directory will be
57       selected. The default packages may be overridden with the
58       workspace.default-members key in the root Cargo.toml manifest.
59
60       -p SPEC..., --package SPEC...
61           Test only the specified packages. See cargo-pkgid(1) for the SPEC
62           format. This flag may be specified multiple times.
63
64       --all
65           Test all members in the workspace.
66
67       --exclude SPEC...
68           Exclude the specified packages. Must be used in conjunction with
69           the --all flag. This flag may be specified multiple times.
70
71   Target Selection
72       When no target selection options are given, cargo test will build the
73       following targets of the selected packages:
74
75       ·   lib — used to link with binaries, examples, integration tests, and
76           doc tests
77
78       ·   bins (only if integration tests are built and required features are
79           available)
80
81       ·   examples — to ensure they compile
82
83       ·   lib as a unit test
84
85       ·   bins as unit tests
86
87       ·   integration tests
88
89       ·   doc tests for the lib target
90
91       The default behavior can be changed by setting the test flag for the
92       target in the manifest settings. Setting examples to test = true will
93       build and run the example as a test. Setting targets to test = false
94       will stop them from being tested by default. Target selection options
95       that take a target by name ignore the test flag and will always test
96       the given target.
97
98       Doc tests for libraries may be disabled by setting doctest = false for
99       the library in the manifest.
100
101       Passing target selection flags will test only the specified targets.
102
103       --lib
104           Test the package’s library.
105
106       --bin NAME...
107           Test the specified binary. This flag may be specified multiple
108           times.
109
110       --bins
111           Test all binary targets.
112
113       --example NAME...
114           Test the specified example. This flag may be specified multiple
115           times.
116
117       --examples
118           Test all example targets.
119
120       --test NAME...
121           Test the specified integration test. This flag may be specified
122           multiple times.
123
124       --tests
125           Test all targets in test mode that have the test = true manifest
126           flag set. By default this includes the library and binaries built
127           as unittests, and integration tests. Be aware that this will also
128           build any required dependencies, so the lib target may be built
129           twice (once as a unittest, and once as a dependency for binaries,
130           integration tests, etc.). Targets may be enabled or disabled by
131           setting the test flag in the manifest settings for the target.
132
133       --bench NAME...
134           Test the specified benchmark. This flag may be specified multiple
135           times.
136
137       --benches
138           Test all targets in benchmark mode that have the bench = true
139           manifest flag set. By default this includes the library and
140           binaries built as benchmarks, and bench targets. Be aware that this
141           will also build any required dependencies, so the lib target may be
142           built twice (once as a benchmark, and once as a dependency for
143           binaries, benchmarks, etc.). Targets may be enabled or disabled by
144           setting the bench flag in the manifest settings for the target.
145
146       --all-targets
147           Test all targets. This is equivalent to specifying --lib --bins
148           --tests --benches --examples.
149
150       --doc
151           Test only the library’s documentation. This cannot be mixed with
152           other target options.
153
154   Feature Selection
155       When no feature options are given, the default feature is activated for
156       every selected package.
157
158       --features FEATURES
159           Space or comma separated list of features to activate. These
160           features only apply to the current directory’s package. Features of
161           direct dependencies may be enabled with <dep-name>/<feature-name>
162           syntax.
163
164       --all-features
165           Activate all available features of all selected packages.
166
167       --no-default-features
168           Do not activate the default feature of the current directory’s
169           package.
170
171   Compilation Options
172       --target TRIPLE
173           Test for the given architecture. The default is the host
174           architecture. The general format of the triple is
175           <arch><sub>-<vendor>-<sys>-<abi>. Run rustc --print target-list for
176           a list of supported targets.
177
178           This may also be specified with the build.target config value
179           <https://doc.rust-lang.org/cargo/reference/config.html>.
180
181       --release
182           Test optimized artifacts with the release profile. See the PROFILES
183           section for details on how this affects profile selection.
184
185   Output Options
186       --target-dir DIRECTORY
187           Directory for all generated artifacts and intermediate files. May
188           also be specified with the CARGO_TARGET_DIR environment variable,
189           or the build.target-dir config value
190           <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults
191           to target in the root of the workspace.
192
193   Display Options
194       By default the Rust test harness hides output from test execution to
195       keep results readable. Test output can be recovered (e.g., for
196       debugging) by passing --nocapture to the test binaries:
197
198           cargo test -- --nocapture
199
200       -v, --verbose
201           Use verbose output. May be specified twice for "very verbose"
202           output which includes extra output such as dependency warnings and
203           build script output. May also be specified with the term.verbose
204           config value
205           <https://doc.rust-lang.org/cargo/reference/config.html>.
206
207       -q, --quiet
208           No output printed to stdout.
209
210       --color WHEN
211           Control when colored output is used. Valid values:
212
213           ·   auto (default): Automatically detect if color support is
214               available on the terminal.
215
216           ·   always: Always display colors.
217
218           ·   never: Never display colors.
219
220           May also be specified with the term.color config value
221           <https://doc.rust-lang.org/cargo/reference/config.html>.
222
223       --message-format FMT
224           The output format for diagnostic messages. Valid values:
225
226           ·   human (default): Display in a human-readable text format.
227
228           ·   json: Emit JSON messages to stdout.
229
230           ·   short: Emit shorter, human-readable text messages.
231
232   Manifest Options
233       --manifest-path PATH
234           Path to the Cargo.toml file. By default, Cargo searches in the
235           current directory or any parent directory for the Cargo.toml file.
236
237       --frozen, --locked
238           Either of these flags requires that the Cargo.lock file is
239           up-to-date. If the lock file is missing, or it needs to be updated,
240           Cargo will exit with an error. The --frozen flag also prevents
241           Cargo from attempting to access the network to determine if it is
242           out-of-date.
243
244           These may be used in environments where you want to assert that the
245           Cargo.lock file is up-to-date (such as a CI build) or want to avoid
246           network access.
247
248   Common Options
249       -h, --help
250           Prints help information.
251
252       -Z FLAG...
253           Unstable (nightly-only) flags to Cargo. Run cargo -Z help for
254           details.
255
256   Miscellaneous Options
257       The --jobs argument affects the building of the test executable but
258       does not affect how many threads are used when running the tests. The
259       Rust test harness includes an option to control the number of threads
260       used:
261
262           cargo test -j 2 -- --test-threads=2
263
264       -j N, --jobs N
265           Number of parallel jobs to run. May also be specified with the
266           build.jobs config value
267           <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults
268           to the number of CPUs.
269

PROFILES

271       Profiles may be used to configure compiler options such as optimization
272       levels and debug settings. See the reference
273       <https://doc.rust-lang.org/cargo/reference/manifest.html#the-profile-sections>
274       for more details.
275
276       Profile selection depends on the target and crate being built. By
277       default the dev or test profiles are used. If the --release flag is
278       given, then the release or bench profiles are used.
279
280       ┌────────────────────┬─────────────────┬───────────────────┐
281       │                    │                 │                   │
282       │Target              │ Default Profile │ --release Profile │
283       ├────────────────────┼─────────────────┼───────────────────┤
284       │                    │                 │                   │
285       │lib, bin, example   │ dev             release           
286       ├────────────────────┼─────────────────┼───────────────────┤
287       │                    │                 │                   │
288       │test, bench, or any │ test            bench             
289       │target              │                 │                   │
290       │in "test" or        │                 │                   │
291       │"bench" mode        │                 │                   │
292       └────────────────────┴─────────────────┴───────────────────┘
293
294       Dependencies use the dev/release profiles.
295
296       Unit tests are separate executable artifacts which use the test/bench
297       profiles. Example targets are built the same as with cargo build (using
298       the dev/release profiles) unless you are building them with the test
299       harness (by setting test = true in the manifest or using the --example
300       flag) in which case they use the test/bench profiles. Library targets
301       are built with the dev/release profiles when linked to an integration
302       test, binary, or doctest.
303

ENVIRONMENT

305       See the reference
306       <https://doc.rust-lang.org/cargo/reference/environment-variables.html>
307       for details on environment variables that Cargo reads.
308

EXIT STATUS

310       0
311           Cargo succeeded.
312
313       101
314           Cargo failed to complete.
315

EXAMPLES

317        1. Execute all the unit and integration tests of the current package:
318
319               cargo test
320
321        2. Run only a specific test within a specific integration test:
322
323               cargo test --test int_test_name -- modname::test_name
324

SEE ALSO

326       cargo(1), cargo-bench(1)
327
328
329
330                                  2018-12-23                     CARGO-TEST(1)
Impressum