cargo 0.38.0

Cargo, a package manager for Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
<h2 id="cargo_test_name">NAME</h2>
<div class="sectionbody">
<p>cargo-test - Execute unit and integration tests of a package</p>
</div>
<div class="sect1">
<h2 id="cargo_test_synopsis">SYNOPSIS</h2>
<div class="sectionbody">
<div class="paragraph">
<p><code>cargo test [<em>OPTIONS</em>] [TESTNAME] [-- <em>TEST-OPTIONS</em>]</code></p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="cargo_test_description">DESCRIPTION</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Compile and execute unit and integration tests.</p>
</div>
<div class="paragraph">
<p>The test filtering argument <code>TESTNAME</code> and all the arguments following the two
dashes (<code>--</code>) are passed to the test binaries and thus to <em>libtest</em> (rustc&#8217;s
built in unit-test and micro-benchmarking framework).  If you&#8217;re passing
arguments to both Cargo and the binary, the ones after <code>--</code> go to the binary,
the ones before go to Cargo.  For details about libtest&#8217;s arguments see the
output of <code>cargo test&#8201;&#8212;&#8201;--help</code>.  As an example, this will run all tests with
<code>foo</code> in their name on 3 threads in parallel:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cargo test foo -- --test-threads 3</pre>
</div>
</div>
<div class="paragraph">
<p>Tests are built with the <code>--test</code> option to <code>rustc</code> which creates an
executable with a <code>main</code> function that automatically runs all functions
annotated with the <code>#[test]</code> attribute in multiple threads. <code>#[bench]</code>
annotated functions will also be run with one iteration to verify that they
are functional.</p>
</div>
<div class="paragraph">
<p>The libtest harness may be disabled by setting <code>harness = false</code> in the target
manifest settings, in which case your code will need to provide its own <code>main</code>
function to handle running tests.</p>
</div>
<div class="paragraph">
<p>Documentation tests are also run by default, which is handled by <code>rustdoc</code>. It
extracts code samples from documentation comments and executes them. See the
<a href="https://doc.rust-lang.org/rustdoc/">rustdoc book</a> for more information on
writing doc tests.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="cargo_test_options">OPTIONS</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="cargo_test_test_options">Test Options</h3>
<div class="dlist">
<dl>
<dt class="hdlist1"><strong>--no-run</strong></dt>
<dd>
<p>Compile, but don&#8217;t run tests.</p>
</dd>
<dt class="hdlist1"><strong>--no-fail-fast</strong></dt>
<dd>
<p>Run all tests regardless of failure. Without this flag, Cargo will exit
after the first executable fails. The Rust test harness will run all
tests within the executable to completion, this flag only applies to
the executable as a whole.</p>
</dd>
</dl>
</div>
</div>
<div class="sect2">
<h3 id="cargo_test_package_selection">Package Selection</h3>
<div class="paragraph">
<p>By default, when no package selection options are given, the packages selected
depend on the current working directory. In the root of a virtual workspace,
all workspace members are selected (<code>--all</code> is implied). Otherwise, only the
package in the current directory will be selected. The default packages may be
overridden with the <code>workspace.default-members</code> key in the root <code>Cargo.toml</code>
manifest.</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><strong>-p</strong> <em>SPEC</em>&#8230;&#8203;</dt>
<dt class="hdlist1"><strong>--package</strong> <em>SPEC</em>&#8230;&#8203;</dt>
<dd>
<p>Test only the specified packages. See <a href="commands/cargo-pkgid.html">cargo-pkgid(1)</a> for the
SPEC format. This flag may be specified multiple times.</p>
</dd>
<dt class="hdlist1"><strong>--all</strong></dt>
<dd>
<p>Test all members in the workspace.</p>
</dd>
<dt class="hdlist1"><strong>--exclude</strong> <em>SPEC</em>&#8230;&#8203;</dt>
<dd>
<p>Exclude the specified packages. Must be used in conjunction with the
<code>--all</code> flag. This flag may be specified multiple times.</p>
</dd>
</dl>
</div>
</div>
<div class="sect2">
<h3 id="cargo_test_target_selection">Target Selection</h3>
<div class="paragraph">
<p>When no target selection options are given, <code>cargo test</code> will build the
following targets of the selected packages:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>lib — used to link with binaries, examples, integration tests, and doc tests</p>
</li>
<li>
<p>bins (only if integration tests are built and required features are
available)</p>
</li>
<li>
<p>examples — to ensure they compile</p>
</li>
<li>
<p>lib as a unit test</p>
</li>
<li>
<p>bins as unit tests</p>
</li>
<li>
<p>integration tests</p>
</li>
<li>
<p>doc tests for the lib target</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The default behavior can be changed by setting the <code>test</code> flag for the target
in the manifest settings. Setting examples to <code>test = true</code> will build and run
the example as a test. Setting targets to <code>test = false</code> will stop them from
being tested by default. Target selection options that take a target by name
ignore the <code>test</code> flag and will always test the given target.</p>
</div>
<div class="paragraph">
<p>Doc tests for libraries may be disabled by setting <code>doctest = false</code> for the
library in the manifest.</p>
</div>
<div class="paragraph">
<p>Passing target selection flags will test only the
specified targets.</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><strong>--lib</strong></dt>
<dd>
<p>Test the package&#8217;s library.</p>
</dd>
<dt class="hdlist1"><strong>--bin</strong> <em>NAME</em>&#8230;&#8203;</dt>
<dd>
<p>Test the specified binary. This flag may be specified multiple times.</p>
</dd>
<dt class="hdlist1"><strong>--bins</strong></dt>
<dd>
<p>Test all binary targets.</p>
</dd>
<dt class="hdlist1"><strong>--example</strong> <em>NAME</em>&#8230;&#8203;</dt>
<dd>
<p>Test the specified example. This flag may be specified multiple times.</p>
</dd>
<dt class="hdlist1"><strong>--examples</strong></dt>
<dd>
<p>Test all example targets.</p>
</dd>
<dt class="hdlist1"><strong>--test</strong> <em>NAME</em>&#8230;&#8203;</dt>
<dd>
<p>Test the specified integration test. This flag may be specified multiple
times.</p>
</dd>
<dt class="hdlist1"><strong>--tests</strong></dt>
<dd>
<p>Test all targets in test mode that have the <code>test = true</code> manifest
flag set. By default this includes the library and binaries built as
unittests, and integration tests. Be aware that this will also build any
required dependencies, so the lib target may be built twice (once as a
unittest, and once as a dependency for binaries, integration tests, etc.).
Targets may be enabled or disabled by setting the <code>test</code> flag in the
manifest settings for the target.</p>
</dd>
<dt class="hdlist1"><strong>--bench</strong> <em>NAME</em>&#8230;&#8203;</dt>
<dd>
<p>Test the specified benchmark. This flag may be specified multiple times.</p>
</dd>
<dt class="hdlist1"><strong>--benches</strong></dt>
<dd>
<p>Test all targets in benchmark mode that have the <code>bench = true</code>
manifest flag set. By default this includes the library and binaries built
as benchmarks, and bench targets. Be aware that this will also build any
required dependencies, so the lib target may be built twice (once as a
benchmark, and once as a dependency for binaries, benchmarks, etc.).
Targets may be enabled or disabled by setting the <code>bench</code> flag in the
manifest settings for the target.</p>
</dd>
<dt class="hdlist1"><strong>--all-targets</strong></dt>
<dd>
<p>Test all targets. This is equivalent to specifying <code>--lib --bins
--tests --benches --examples</code>.</p>
</dd>
<dt class="hdlist1"><strong>--doc</strong></dt>
<dd>
<p>Test only the library&#8217;s documentation. This cannot be mixed with other
target options.</p>
</dd>
</dl>
</div>
</div>
<div class="sect2">
<h3 id="cargo_test_feature_selection">Feature Selection</h3>
<div class="paragraph">
<p>When no feature options are given, the <code>default</code> feature is activated for
every selected package.</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><strong>--features</strong> <em>FEATURES</em></dt>
<dd>
<p>Space or comma separated list of features to activate. These features only
apply to the current directory&#8217;s package. Features of direct dependencies
may be enabled with <code>&lt;dep-name&gt;/&lt;feature-name&gt;</code> syntax.</p>
</dd>
<dt class="hdlist1"><strong>--all-features</strong></dt>
<dd>
<p>Activate all available features of all selected packages.</p>
</dd>
<dt class="hdlist1"><strong>--no-default-features</strong></dt>
<dd>
<p>Do not activate the <code>default</code> feature of the current directory&#8217;s
package.</p>
</dd>
</dl>
</div>
</div>
<div class="sect2">
<h3 id="cargo_test_compilation_options">Compilation Options</h3>
<div class="dlist">
<dl>
<dt class="hdlist1"><strong>--target</strong> <em>TRIPLE</em></dt>
<dd>
<p>Test for the given architecture. The default is the host
architecture. The general format of the triple is
<code>&lt;arch&gt;&lt;sub&gt;-&lt;vendor&gt;-&lt;sys&gt;-&lt;abi&gt;</code>. Run <code>rustc --print target-list</code> for a
list of supported targets.</p>
<div class="paragraph">
<p>This may also be specified with the <code>build.target</code>
<a href="reference/config.html">config value</a>.</p>
</div>
</dd>
<dt class="hdlist1"><strong>--release</strong></dt>
<dd>
<p>Test optimized artifacts with the <code>release</code> profile. See the
<a href="#cargo_test_profiles">PROFILES</a> section for details on how this affects profile selection.</p>
</dd>
</dl>
</div>
</div>
<div class="sect2">
<h3 id="cargo_test_output_options">Output Options</h3>
<div class="dlist">
<dl>
<dt class="hdlist1"><strong>--target-dir</strong> <em>DIRECTORY</em></dt>
<dd>
<p>Directory for all generated artifacts and intermediate files. May also be
specified with the <code>CARGO_TARGET_DIR</code> environment variable, or the
<code>build.target-dir</code> <a href="reference/config.html">config value</a>. Defaults
to <code>target</code> in the root of the workspace.</p>
</dd>
</dl>
</div>
</div>
<div class="sect2">
<h3 id="cargo_test_display_options">Display Options</h3>
<div class="paragraph">
<p>By default the Rust test harness hides output from test execution to keep
results readable. Test output can be recovered (e.g., for debugging) by passing
<code>--nocapture</code> to the test binaries:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cargo test -- --nocapture</pre>
</div>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><strong>-v</strong></dt>
<dt class="hdlist1"><strong>--verbose</strong></dt>
<dd>
<p>Use verbose output. May be specified twice for "very verbose" output which
includes extra output such as dependency warnings and build script output.
May also be specified with the <code>term.verbose</code>
<a href="reference/config.html">config value</a>.</p>
</dd>
<dt class="hdlist1"><strong>-q</strong></dt>
<dt class="hdlist1"><strong>--quiet</strong></dt>
<dd>
<p>No output printed to stdout.</p>
</dd>
<dt class="hdlist1"><strong>--color</strong> <em>WHEN</em></dt>
<dd>
<p>Control when colored output is used. Valid values:</p>
<div class="ulist">
<ul>
<li>
<p><code>auto</code> (default): Automatically detect if color support is available on the
terminal.</p>
</li>
<li>
<p><code>always</code>: Always display colors.</p>
</li>
<li>
<p><code>never</code>: Never display colors.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>May also be specified with the <code>term.color</code>
<a href="reference/config.html">config value</a>.</p>
</div>
</dd>
<dt class="hdlist1"><strong>--message-format</strong> <em>FMT</em></dt>
<dd>
<p>The output format for diagnostic messages. Valid values:</p>
<div class="ulist">
<ul>
<li>
<p><code>human</code> (default): Display in a human-readable text format.</p>
</li>
<li>
<p><code>json</code>: Emit JSON messages to stdout.</p>
</li>
<li>
<p><code>short</code>: Emit shorter, human-readable text messages.</p>
</li>
</ul>
</div>
</dd>
</dl>
</div>
</div>
<div class="sect2">
<h3 id="cargo_test_manifest_options">Manifest Options</h3>
<div class="dlist">
<dl>
<dt class="hdlist1"><strong>--manifest-path</strong> <em>PATH</em></dt>
<dd>
<p>Path to the <code>Cargo.toml</code> file. By default, Cargo searches in the current
directory or any parent directory for the <code>Cargo.toml</code> file.</p>
</dd>
<dt class="hdlist1"><strong>--frozen</strong></dt>
<dt class="hdlist1"><strong>--locked</strong></dt>
<dd>
<p>Either of these flags requires that the <code>Cargo.lock</code> file is
up-to-date. If the lock file is missing, or it needs to be updated, Cargo will
exit with an error. The <code>--frozen</code> flag also prevents Cargo from
attempting to access the network to determine if it is out-of-date.</p>
<div class="paragraph">
<p>These may be used in environments where you want to assert that the
<code>Cargo.lock</code> file is up-to-date (such as a CI build) or want to avoid network
access.</p>
</div>
</dd>
<dt class="hdlist1"><strong>--offline</strong></dt>
<dd>
<p>Prevents Cargo from accessing the network for any reason. Without this
flag, Cargo will stop with an error if it needs to access the network and
the network is not available. With this flag, Cargo will attempt to
proceed without the network if possible.</p>
<div class="paragraph">
<p>Beware that this may result in different dependency resolution than online
mode. Cargo will restrict itself to crates that are downloaded locally, even
if there might be a newer version as indicated in the local copy of the index.
See the <a href="commands/cargo-fetch.html">cargo-fetch(1)</a> command to download dependencies before going
offline.</p>
</div>
<div class="paragraph">
<p>May also be specified with the <code>net.offline</code> <a href="reference/config.html">config value</a>.</p>
</div>
</dd>
</dl>
</div>
</div>
<div class="sect2">
<h3 id="cargo_test_common_options">Common Options</h3>
<div class="dlist">
<dl>
<dt class="hdlist1"><strong>-h</strong></dt>
<dt class="hdlist1"><strong>--help</strong></dt>
<dd>
<p>Prints help information.</p>
</dd>
<dt class="hdlist1"><strong>-Z</strong> <em>FLAG</em>&#8230;&#8203;</dt>
<dd>
<p>Unstable (nightly-only) flags to Cargo. Run <code>cargo -Z help</code> for
details.</p>
</dd>
</dl>
</div>
</div>
<div class="sect2">
<h3 id="cargo_test_miscellaneous_options">Miscellaneous Options</h3>
<div class="paragraph">
<p>The <code>--jobs</code> argument affects the building of the test executable but does not
affect how many threads are used when running the tests. The Rust test harness
includes an option to control the number of threads used:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cargo test -j 2 -- --test-threads=2</pre>
</div>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><strong>-j</strong> <em>N</em></dt>
<dt class="hdlist1"><strong>--jobs</strong> <em>N</em></dt>
<dd>
<p>Number of parallel jobs to run. May also be specified with the
<code>build.jobs</code> <a href="reference/config.html">config value</a>. Defaults to
the number of CPUs.</p>
</dd>
</dl>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="cargo_test_profiles">PROFILES</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Profiles may be used to configure compiler options such as optimization levels
and debug settings. See
<a href="reference/manifest.html#the-profile-sections">the reference</a>
for more details.</p>
</div>
<div class="paragraph">
<p>Profile selection depends on the target and crate being built. By default the
<code>dev</code> or <code>test</code> profiles are used. If the <code>--release</code> flag is given, then the
<code>release</code> or <code>bench</code> profiles are used.</p>
</div>
<table class="tableblock frame-all grid-all fit-content">
<colgroup>
<col>
<col>
<col>
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Target</th>
<th class="tableblock halign-left valign-top">Default Profile</th>
<th class="tableblock halign-left valign-top"><code>--release</code> Profile</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">lib, bin, example</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>dev</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>release</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">test, bench, or any target<br>
 in "test" or "bench" mode</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>test</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>bench</code></p></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>Dependencies use the <code>dev</code>/<code>release</code> profiles.</p>
</div>
<div class="paragraph">
<p>Unit tests are separate executable artifacts which use the <code>test</code>/<code>bench</code>
profiles. Example targets are built the same as with <code>cargo build</code> (using the
<code>dev</code>/<code>release</code> profiles) unless you are building them with the test harness
(by setting <code>test = true</code> in the manifest or using the <code>--example</code> flag) in
which case they use the <code>test</code>/<code>bench</code> profiles. Library targets are built
with the <code>dev</code>/<code>release</code> profiles when linked to an integration test, binary,
or doctest.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="cargo_test_environment">ENVIRONMENT</h2>
<div class="sectionbody">
<div class="paragraph">
<p>See <a href="reference/environment-variables.html">the reference</a> for
details on environment variables that Cargo reads.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="cargo_test_exit_status">Exit Status</h2>
<div class="sectionbody">
<div class="dlist">
<dl>
<dt class="hdlist1">0</dt>
<dd>
<p>Cargo succeeded.</p>
</dd>
<dt class="hdlist1">101</dt>
<dd>
<p>Cargo failed to complete.</p>
</dd>
</dl>
</div>
</div>
</div>
<div class="sect1">
<h2 id="cargo_test_examples">EXAMPLES</h2>
<div class="sectionbody">
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Execute all the unit and integration tests of the current package:</p>
<div class="literalblock">
<div class="content">
<pre>cargo test</pre>
</div>
</div>
</li>
<li>
<p>Run only a specific test within a specific integration test:</p>
<div class="literalblock">
<div class="content">
<pre>cargo test --test int_test_name -- modname::test_name</pre>
</div>
</div>
</li>
</ol>
</div>
</div>
</div>
<div class="sect1">
<h2 id="cargo_test_see_also">SEE ALSO</h2>
<div class="sectionbody">
<div class="paragraph">
<p><a href="commands/index.html">cargo(1)</a>, <a href="commands/cargo-bench.html">cargo-bench(1)</a></p>
</div>
</div>
</div>