blob: c856952c45a9edf8187bd1f8d1df7182703525e3 [file] [log] [blame]
Matthew Maurerbd398542019-09-05 16:25:08 -07001'\" t
2.\" Title: cargo-test
3.\" Author: [see the "AUTHOR(S)" section]
4.\" Generator: Asciidoctor 1.5.8
5.\" Date: 2019-05-08
6.\" Manual: \ \&
7.\" Source: \ \&
8.\" Language: English
9.\"
10.TH "CARGO\-TEST" "1" "2019-05-08" "\ \&" "\ \&"
11.ie \n(.g .ds Aq \(aq
12.el .ds Aq '
13.ss \n[.ss] 0
14.nh
15.ad l
16.de URL
17\fI\\$2\fP <\\$1>\\$3
18..
19.als MTO URL
20.if \n[.g] \{\
21. mso www.tmac
22. am URL
23. ad l
24. .
25. am MTO
26. ad l
27. .
28. LINKSTYLE blue R < >
29.\}
30.SH "NAME"
31cargo\-test \- Execute unit and integration tests of a package
32.SH "SYNOPSIS"
33.sp
34\fBcargo test [\fIOPTIONS\fP] [TESTNAME] [\-\- \fITEST\-OPTIONS\fP]\fP
35.SH "DESCRIPTION"
36.sp
37Compile and execute unit and integration tests.
38.sp
39The test filtering argument \fBTESTNAME\fP and all the arguments following the two
40dashes (\fB\-\-\fP) are passed to the test binaries and thus to \fIlibtest\fP (rustc\(cqs
41built in unit\-test and micro\-benchmarking framework). If you\(cqre passing
42arguments to both Cargo and the binary, the ones after \fB\-\-\fP go to the binary,
43the ones before go to Cargo. For details about libtest\(cqs arguments see the
44output of \fBcargo test \(em \-\-help\fP. As an example, this will run all tests with
45\fBfoo\fP in their name on 3 threads in parallel:
46.sp
47.if n .RS 4
48.nf
49cargo test foo \-\- \-\-test\-threads 3
50.fi
51.if n .RE
52.sp
53Tests are built with the \fB\-\-test\fP option to \fBrustc\fP which creates an
54executable with a \fBmain\fP function that automatically runs all functions
55annotated with the \fB#[test]\fP attribute in multiple threads. \fB#[bench]\fP
56annotated functions will also be run with one iteration to verify that they
57are functional.
58.sp
59The libtest harness may be disabled by setting \fBharness = false\fP in the target
60manifest settings, in which case your code will need to provide its own \fBmain\fP
61function to handle running tests.
62.sp
63Documentation tests are also run by default, which is handled by \fBrustdoc\fP. It
64extracts code samples from documentation comments and executes them. See the
65.URL "https://doc.rust\-lang.org/rustdoc/" "rustdoc book" " "
66for more information on
67writing doc tests.
68.SH "OPTIONS"
69.SS "Test Options"
70.sp
71\fB\-\-no\-run\fP
72.RS 4
73Compile, but don\(cqt run tests.
74.RE
75.sp
76\fB\-\-no\-fail\-fast\fP
77.RS 4
78Run all tests regardless of failure. Without this flag, Cargo will exit
79after the first executable fails. The Rust test harness will run all
80tests within the executable to completion, this flag only applies to
81the executable as a whole.
82.RE
83.SS "Package Selection"
84.sp
85By default, when no package selection options are given, the packages selected
86depend on the current working directory. In the root of a virtual workspace,
87all workspace members are selected (\fB\-\-all\fP is implied). Otherwise, only the
88package in the current directory will be selected. The default packages may be
89overridden with the \fBworkspace.default\-members\fP key in the root \fBCargo.toml\fP
90manifest.
91.sp
92\fB\-p\fP \fISPEC\fP..., \fB\-\-package\fP \fISPEC\fP...
93.RS 4
94Test only the specified packages. See \fBcargo\-pkgid\fP(1) for the
95SPEC format. This flag may be specified multiple times.
96.RE
97.sp
98\fB\-\-all\fP
99.RS 4
100Test all members in the workspace.
101.RE
102.sp
103\fB\-\-exclude\fP \fISPEC\fP...
104.RS 4
105Exclude the specified packages. Must be used in conjunction with the
106\fB\-\-all\fP flag. This flag may be specified multiple times.
107.RE
108.SS "Target Selection"
109.sp
110When no target selection options are given, \fBcargo test\fP will build the
111following targets of the selected packages:
112.sp
113.RS 4
114.ie n \{\
115\h'-04'\(bu\h'+03'\c
116.\}
117.el \{\
118. sp -1
119. IP \(bu 2.3
120.\}
121lib used to link with binaries, examples, integration tests, and doc tests
122.RE
123.sp
124.RS 4
125.ie n \{\
126\h'-04'\(bu\h'+03'\c
127.\}
128.el \{\
129. sp -1
130. IP \(bu 2.3
131.\}
132bins (only if integration tests are built and required features are
133available)
134.RE
135.sp
136.RS 4
137.ie n \{\
138\h'-04'\(bu\h'+03'\c
139.\}
140.el \{\
141. sp -1
142. IP \(bu 2.3
143.\}
144examples to ensure they compile
145.RE
146.sp
147.RS 4
148.ie n \{\
149\h'-04'\(bu\h'+03'\c
150.\}
151.el \{\
152. sp -1
153. IP \(bu 2.3
154.\}
155lib as a unit test
156.RE
157.sp
158.RS 4
159.ie n \{\
160\h'-04'\(bu\h'+03'\c
161.\}
162.el \{\
163. sp -1
164. IP \(bu 2.3
165.\}
166bins as unit tests
167.RE
168.sp
169.RS 4
170.ie n \{\
171\h'-04'\(bu\h'+03'\c
172.\}
173.el \{\
174. sp -1
175. IP \(bu 2.3
176.\}
177integration tests
178.RE
179.sp
180.RS 4
181.ie n \{\
182\h'-04'\(bu\h'+03'\c
183.\}
184.el \{\
185. sp -1
186. IP \(bu 2.3
187.\}
188doc tests for the lib target
189.RE
190.sp
191The default behavior can be changed by setting the \fBtest\fP flag for the target
192in the manifest settings. Setting examples to \fBtest = true\fP will build and run
193the example as a test. Setting targets to \fBtest = false\fP will stop them from
194being tested by default. Target selection options that take a target by name
195ignore the \fBtest\fP flag and will always test the given target.
196.sp
197Doc tests for libraries may be disabled by setting \fBdoctest = false\fP for the
198library in the manifest.
199.sp
200Passing target selection flags will test only the
201specified targets.
202.sp
203\fB\-\-lib\fP
204.RS 4
205Test the package\(cqs library.
206.RE
207.sp
208\fB\-\-bin\fP \fINAME\fP...
209.RS 4
210Test the specified binary. This flag may be specified multiple times.
211.RE
212.sp
213\fB\-\-bins\fP
214.RS 4
215Test all binary targets.
216.RE
217.sp
218\fB\-\-example\fP \fINAME\fP...
219.RS 4
220Test the specified example. This flag may be specified multiple times.
221.RE
222.sp
223\fB\-\-examples\fP
224.RS 4
225Test all example targets.
226.RE
227.sp
228\fB\-\-test\fP \fINAME\fP...
229.RS 4
230Test the specified integration test. This flag may be specified multiple
231times.
232.RE
233.sp
234\fB\-\-tests\fP
235.RS 4
236Test all targets in test mode that have the \fBtest = true\fP manifest
237flag set. By default this includes the library and binaries built as
238unittests, and integration tests. Be aware that this will also build any
239required dependencies, so the lib target may be built twice (once as a
240unittest, and once as a dependency for binaries, integration tests, etc.).
241Targets may be enabled or disabled by setting the \fBtest\fP flag in the
242manifest settings for the target.
243.RE
244.sp
245\fB\-\-bench\fP \fINAME\fP...
246.RS 4
247Test the specified benchmark. This flag may be specified multiple times.
248.RE
249.sp
250\fB\-\-benches\fP
251.RS 4
252Test all targets in benchmark mode that have the \fBbench = true\fP
253manifest flag set. By default this includes the library and binaries built
254as benchmarks, and bench targets. Be aware that this will also build any
255required dependencies, so the lib target may be built twice (once as a
256benchmark, and once as a dependency for binaries, benchmarks, etc.).
257Targets may be enabled or disabled by setting the \fBbench\fP flag in the
258manifest settings for the target.
259.RE
260.sp
261\fB\-\-all\-targets\fP
262.RS 4
263Test all targets. This is equivalent to specifying \fB\-\-lib \-\-bins
264\-\-tests \-\-benches \-\-examples\fP.
265.RE
266.sp
267\fB\-\-doc\fP
268.RS 4
269Test only the library\(cqs documentation. This cannot be mixed with other
270target options.
271.RE
272.SS "Feature Selection"
273.sp
274When no feature options are given, the \fBdefault\fP feature is activated for
275every selected package.
276.sp
277\fB\-\-features\fP \fIFEATURES\fP
278.RS 4
279Space or comma separated list of features to activate. These features only
280apply to the current directory\(cqs package. Features of direct dependencies
281may be enabled with \fB<dep\-name>/<feature\-name>\fP syntax.
282.RE
283.sp
284\fB\-\-all\-features\fP
285.RS 4
286Activate all available features of all selected packages.
287.RE
288.sp
289\fB\-\-no\-default\-features\fP
290.RS 4
291Do not activate the \fBdefault\fP feature of the current directory\(cqs
292package.
293.RE
294.SS "Compilation Options"
295.sp
296\fB\-\-target\fP \fITRIPLE\fP
297.RS 4
298Test for the given architecture. The default is the host
299architecture. The general format of the triple is
300\fB<arch><sub>\-<vendor>\-<sys>\-<abi>\fP. Run \fBrustc \-\-print target\-list\fP for a
301list of supported targets.
302.sp
303This may also be specified with the \fBbuild.target\fP
304.URL "https://doc.rust\-lang.org/cargo/reference/config.html" "config value" "."
305.RE
306.sp
307\fB\-\-release\fP
308.RS 4
309Test optimized artifacts with the \fBrelease\fP profile. See the
310PROFILES section for details on how this affects profile selection.
311.RE
312.SS "Output Options"
313.sp
314\fB\-\-target\-dir\fP \fIDIRECTORY\fP
315.RS 4
316Directory for all generated artifacts and intermediate files. May also be
317specified with the \fBCARGO_TARGET_DIR\fP environment variable, or the
318\fBbuild.target\-dir\fP \c
319.URL "https://doc.rust\-lang.org/cargo/reference/config.html" "config value" "."
320Defaults
321to \fBtarget\fP in the root of the workspace.
322.RE
323.SS "Display Options"
324.sp
325By default the Rust test harness hides output from test execution to keep
326results readable. Test output can be recovered (e.g., for debugging) by passing
327\fB\-\-nocapture\fP to the test binaries:
328.sp
329.if n .RS 4
330.nf
331cargo test \-\- \-\-nocapture
332.fi
333.if n .RE
334.sp
335\fB\-v\fP, \fB\-\-verbose\fP
336.RS 4
337Use verbose output. May be specified twice for "very verbose" output which
338includes extra output such as dependency warnings and build script output.
339May also be specified with the \fBterm.verbose\fP
340.URL "https://doc.rust\-lang.org/cargo/reference/config.html" "config value" "."
341.RE
342.sp
343\fB\-q\fP, \fB\-\-quiet\fP
344.RS 4
345No output printed to stdout.
346.RE
347.sp
348\fB\-\-color\fP \fIWHEN\fP
349.RS 4
350Control when colored output is used. Valid values:
351.sp
352.RS 4
353.ie n \{\
354\h'-04'\(bu\h'+03'\c
355.\}
356.el \{\
357. sp -1
358. IP \(bu 2.3
359.\}
360\fBauto\fP (default): Automatically detect if color support is available on the
361terminal.
362.RE
363.sp
364.RS 4
365.ie n \{\
366\h'-04'\(bu\h'+03'\c
367.\}
368.el \{\
369. sp -1
370. IP \(bu 2.3
371.\}
372\fBalways\fP: Always display colors.
373.RE
374.sp
375.RS 4
376.ie n \{\
377\h'-04'\(bu\h'+03'\c
378.\}
379.el \{\
380. sp -1
381. IP \(bu 2.3
382.\}
383\fBnever\fP: Never display colors.
384.RE
385.sp
386May also be specified with the \fBterm.color\fP
387.URL "https://doc.rust\-lang.org/cargo/reference/config.html" "config value" "."
388.RE
389.sp
390\fB\-\-message\-format\fP \fIFMT\fP
391.RS 4
392The output format for diagnostic messages. Valid values:
393.sp
394.RS 4
395.ie n \{\
396\h'-04'\(bu\h'+03'\c
397.\}
398.el \{\
399. sp -1
400. IP \(bu 2.3
401.\}
402\fBhuman\fP (default): Display in a human\-readable text format.
403.RE
404.sp
405.RS 4
406.ie n \{\
407\h'-04'\(bu\h'+03'\c
408.\}
409.el \{\
410. sp -1
411. IP \(bu 2.3
412.\}
413\fBjson\fP: Emit JSON messages to stdout.
414.RE
415.sp
416.RS 4
417.ie n \{\
418\h'-04'\(bu\h'+03'\c
419.\}
420.el \{\
421. sp -1
422. IP \(bu 2.3
423.\}
424\fBshort\fP: Emit shorter, human\-readable text messages.
425.RE
426.RE
427.SS "Manifest Options"
428.sp
429\fB\-\-manifest\-path\fP \fIPATH\fP
430.RS 4
431Path to the \fBCargo.toml\fP file. By default, Cargo searches in the current
432directory or any parent directory for the \fBCargo.toml\fP file.
433.RE
434.sp
435\fB\-\-frozen\fP, \fB\-\-locked\fP
436.RS 4
437Either of these flags requires that the \fBCargo.lock\fP file is
438up\-to\-date. If the lock file is missing, or it needs to be updated, Cargo will
439exit with an error. The \fB\-\-frozen\fP flag also prevents Cargo from
440attempting to access the network to determine if it is out\-of\-date.
441.sp
442These may be used in environments where you want to assert that the
443\fBCargo.lock\fP file is up\-to\-date (such as a CI build) or want to avoid network
444access.
445.RE
446.sp
447\fB\-\-offline\fP
448.RS 4
449Prevents Cargo from accessing the network for any reason. Without this
450flag, Cargo will stop with an error if it needs to access the network and
451the network is not available. With this flag, Cargo will attempt to
452proceed without the network if possible.
453.sp
454Beware that this may result in different dependency resolution than online
455mode. Cargo will restrict itself to crates that are downloaded locally, even
456if there might be a newer version as indicated in the local copy of the index.
457See the \fBcargo\-fetch\fP(1) command to download dependencies before going
458offline.
459.sp
460May also be specified with the \fBnet.offline\fP \c
461.URL "https://doc.rust\-lang.org/cargo/reference/config.html" "config value" "."
462.RE
463.SS "Common Options"
464.sp
465\fB\-h\fP, \fB\-\-help\fP
466.RS 4
467Prints help information.
468.RE
469.sp
470\fB\-Z\fP \fIFLAG\fP...
471.RS 4
472Unstable (nightly\-only) flags to Cargo. Run \fBcargo \-Z help\fP for
473details.
474.RE
475.SS "Miscellaneous Options"
476.sp
477The \fB\-\-jobs\fP argument affects the building of the test executable but does not
478affect how many threads are used when running the tests. The Rust test harness
479includes an option to control the number of threads used:
480.sp
481.if n .RS 4
482.nf
483cargo test \-j 2 \-\- \-\-test\-threads=2
484.fi
485.if n .RE
486.sp
487\fB\-j\fP \fIN\fP, \fB\-\-jobs\fP \fIN\fP
488.RS 4
489Number of parallel jobs to run. May also be specified with the
490\fBbuild.jobs\fP \c
491.URL "https://doc.rust\-lang.org/cargo/reference/config.html" "config value" "."
492Defaults to
493the number of CPUs.
494.RE
495.SH "PROFILES"
496.sp
497Profiles may be used to configure compiler options such as optimization levels
498and debug settings. See
499\c
500.URL "https://doc.rust\-lang.org/cargo/reference/manifest.html#the\-profile\-sections" "the reference"
501for more details.
502.sp
503Profile selection depends on the target and crate being built. By default the
504\fBdev\fP or \fBtest\fP profiles are used. If the \fB\-\-release\fP flag is given, then the
505\fBrelease\fP or \fBbench\fP profiles are used.
506.TS
507allbox tab(:);
508lt lt lt.
509T{
510.sp
511Target
512T}:T{
513.sp
514Default Profile
515T}:T{
516.sp
517\fB\-\-release\fP Profile
518T}
519T{
520.sp
521lib, bin, example
522T}:T{
523.sp
524\fBdev\fP
525T}:T{
526.sp
527\fBrelease\fP
528T}
529T{
530.sp
531test, bench, or any target
532.br
533in "test" or "bench" mode
534T}:T{
535.sp
536\fBtest\fP
537T}:T{
538.sp
539\fBbench\fP
540T}
541.TE
542.sp
543.sp
544Dependencies use the \fBdev\fP/\fBrelease\fP profiles.
545.sp
546Unit tests are separate executable artifacts which use the \fBtest\fP/\fBbench\fP
547profiles. Example targets are built the same as with \fBcargo build\fP (using the
548\fBdev\fP/\fBrelease\fP profiles) unless you are building them with the test harness
549(by setting \fBtest = true\fP in the manifest or using the \fB\-\-example\fP flag) in
550which case they use the \fBtest\fP/\fBbench\fP profiles. Library targets are built
551with the \fBdev\fP/\fBrelease\fP profiles when linked to an integration test, binary,
552or doctest.
553.SH "ENVIRONMENT"
554.sp
555See \c
556.URL "https://doc.rust\-lang.org/cargo/reference/environment\-variables.html" "the reference" " "
557for
558details on environment variables that Cargo reads.
559.SH "EXIT STATUS"
560.sp
5610
562.RS 4
563Cargo succeeded.
564.RE
565.sp
566101
567.RS 4
568Cargo failed to complete.
569.RE
570.SH "EXAMPLES"
571.sp
572.RS 4
573.ie n \{\
574\h'-04' 1.\h'+01'\c
575.\}
576.el \{\
577. sp -1
578. IP " 1." 4.2
579.\}
580Execute all the unit and integration tests of the current package:
581.sp
582.if n .RS 4
583.nf
584cargo test
585.fi
586.if n .RE
587.RE
588.sp
589.RS 4
590.ie n \{\
591\h'-04' 2.\h'+01'\c
592.\}
593.el \{\
594. sp -1
595. IP " 2." 4.2
596.\}
597Run only a specific test within a specific integration test:
598.sp
599.if n .RS 4
600.nf
601cargo test \-\-test int_test_name \-\- modname::test_name
602.fi
603.if n .RE
604.RE
605.SH "SEE ALSO"
606.sp
607\fBcargo\fP(1), \fBcargo\-bench\fP(1)