blob: 0f967958fc7e058cb1176bdbda8cd8d92326847f [file] [log] [blame]
Jeff Vander Stoep956d1432020-09-18 13:46:24 +02001'\" t
2.\" Title: cargo-test
3.\" Author: [see the "AUTHOR(S)" section]
4.\" Generator: Asciidoctor 2.0.10
5.\" Date: 2020-06-25
6.\" Manual: \ \&
7.\" Source: \ \&
8.\" Language: English
9.\"
10.TH "CARGO\-TEST" "1" "2020-06-25" "\ \&" "\ \&"
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 \-\- \-\-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 selected manifest file (based on the current working directory if
87\fB\-\-manifest\-path\fP is not given). If the manifest is the root of a workspace then
88the workspaces default members are selected, otherwise only the package defined
89by the manifest will be selected.
90.sp
91The default members of a workspace can be set explicitly with the
92\fBworkspace.default\-members\fP key in the root manifest. If this is not set, a
93virtual workspace will include all workspace members (equivalent to passing
94\fB\-\-workspace\fP), and a non\-virtual workspace will include only the root crate itself.
95.sp
96\fB\-p\fP \fISPEC\fP..., \fB\-\-package\fP \fISPEC\fP...
97.RS 4
98Test only the specified packages. See \fBcargo\-pkgid\fP(1) for the
99SPEC format. This flag may be specified multiple times.
100.RE
101.sp
102\fB\-\-workspace\fP
103.RS 4
104Test all members in the workspace.
105.RE
106.sp
107\fB\-\-all\fP
108.RS 4
109Deprecated alias for \fB\-\-workspace\fP.
110.RE
111.sp
112\fB\-\-exclude\fP \fISPEC\fP...
113.RS 4
114Exclude the specified packages. Must be used in conjunction with the
115\fB\-\-workspace\fP flag. This flag may be specified multiple times.
116.RE
117.SS "Target Selection"
118.sp
119When no target selection options are given, \fBcargo test\fP will build the
120following targets of the selected packages:
121.sp
122.RS 4
123.ie n \{\
124\h'-04'\(bu\h'+03'\c
125.\}
126.el \{\
127. sp -1
128. IP \(bu 2.3
129.\}
130lib used to link with binaries, examples, integration tests, and doc tests
131.RE
132.sp
133.RS 4
134.ie n \{\
135\h'-04'\(bu\h'+03'\c
136.\}
137.el \{\
138. sp -1
139. IP \(bu 2.3
140.\}
141bins (only if integration tests are built and required features are
142available)
143.RE
144.sp
145.RS 4
146.ie n \{\
147\h'-04'\(bu\h'+03'\c
148.\}
149.el \{\
150. sp -1
151. IP \(bu 2.3
152.\}
153examples to ensure they compile
154.RE
155.sp
156.RS 4
157.ie n \{\
158\h'-04'\(bu\h'+03'\c
159.\}
160.el \{\
161. sp -1
162. IP \(bu 2.3
163.\}
164lib as a unit test
165.RE
166.sp
167.RS 4
168.ie n \{\
169\h'-04'\(bu\h'+03'\c
170.\}
171.el \{\
172. sp -1
173. IP \(bu 2.3
174.\}
175bins as unit tests
176.RE
177.sp
178.RS 4
179.ie n \{\
180\h'-04'\(bu\h'+03'\c
181.\}
182.el \{\
183. sp -1
184. IP \(bu 2.3
185.\}
186integration tests
187.RE
188.sp
189.RS 4
190.ie n \{\
191\h'-04'\(bu\h'+03'\c
192.\}
193.el \{\
194. sp -1
195. IP \(bu 2.3
196.\}
197doc tests for the lib target
198.RE
199.sp
200The default behavior can be changed by setting the \fBtest\fP flag for the target
201in the manifest settings. Setting examples to \fBtest = true\fP will build and run
202the example as a test. Setting targets to \fBtest = false\fP will stop them from
203being tested by default. Target selection options that take a target by name
204ignore the \fBtest\fP flag and will always test the given target.
205.sp
206Doc tests for libraries may be disabled by setting \fBdoctest = false\fP for the
207library in the manifest.
208.sp
209Binary targets are automatically built if there is an integration test or
210benchmark. This allows an integration test to execute the binary to exercise
211and test its behavior. The \fBCARGO_BIN_EXE_<name>\fP
212\c
213.URL "https://doc.rust\-lang.org/cargo/reference/environment\-variables.html#environment\-variables\-cargo\-sets\-for\-crates" "environment variable"
214is set when the integration test is built so that it can use the
215.URL "https://doc.rust\-lang.org/std/macro.env.html" "\fBenv\fP macro" " "
216to locate the
217executable.
218.sp
219Passing target selection flags will test only the
220specified targets.
221.sp
222\fB\-\-lib\fP
223.RS 4
224Test the package\(cqs library.
225.RE
226.sp
227\fB\-\-bin\fP \fINAME\fP...
228.RS 4
229Test the specified binary. This flag may be specified multiple times.
230.RE
231.sp
232\fB\-\-bins\fP
233.RS 4
234Test all binary targets.
235.RE
236.sp
237\fB\-\-example\fP \fINAME\fP...
238.RS 4
239Test the specified example. This flag may be specified multiple times.
240.RE
241.sp
242\fB\-\-examples\fP
243.RS 4
244Test all example targets.
245.RE
246.sp
247\fB\-\-test\fP \fINAME\fP...
248.RS 4
249Test the specified integration test. This flag may be specified multiple
250times.
251.RE
252.sp
253\fB\-\-tests\fP
254.RS 4
255Test all targets in test mode that have the \fBtest = true\fP manifest
256flag set. By default this includes the library and binaries built as
257unittests, and integration tests. Be aware that this will also build any
258required dependencies, so the lib target may be built twice (once as a
259unittest, and once as a dependency for binaries, integration tests, etc.).
260Targets may be enabled or disabled by setting the \fBtest\fP flag in the
261manifest settings for the target.
262.RE
263.sp
264\fB\-\-bench\fP \fINAME\fP...
265.RS 4
266Test the specified benchmark. This flag may be specified multiple times.
267.RE
268.sp
269\fB\-\-benches\fP
270.RS 4
271Test all targets in benchmark mode that have the \fBbench = true\fP
272manifest flag set. By default this includes the library and binaries built
273as benchmarks, and bench targets. Be aware that this will also build any
274required dependencies, so the lib target may be built twice (once as a
275benchmark, and once as a dependency for binaries, benchmarks, etc.).
276Targets may be enabled or disabled by setting the \fBbench\fP flag in the
277manifest settings for the target.
278.RE
279.sp
280\fB\-\-all\-targets\fP
281.RS 4
282Test all targets. This is equivalent to specifying \fB\-\-lib \-\-bins
283\-\-tests \-\-benches \-\-examples\fP.
284.RE
285.sp
286\fB\-\-doc\fP
287.RS 4
288Test only the library\(cqs documentation. This cannot be mixed with other
289target options.
290.RE
291.SS "Feature Selection"
292.sp
293The feature flags allow you to control the enabled features for the "current"
294package. The "current" package is the package in the current directory, or the
295one specified in \fB\-\-manifest\-path\fP. If running in the root of a virtual
296workspace, then the default features are selected for all workspace members,
297or all features if \fB\-\-all\-features\fP is specified.
298.sp
299When no feature options are given, the \fBdefault\fP feature is activated for
300every selected package.
301.sp
302\fB\-\-features\fP \fIFEATURES\fP
303.RS 4
304Space or comma separated list of features to activate. These features only
305apply to the current directory\(cqs package. Features of direct dependencies
306may be enabled with \fB<dep\-name>/<feature\-name>\fP syntax. This flag may be
307specified multiple times, which enables all specified features.
308.RE
309.sp
310\fB\-\-all\-features\fP
311.RS 4
312Activate all available features of all selected packages.
313.RE
314.sp
315\fB\-\-no\-default\-features\fP
316.RS 4
317Do not activate the \fBdefault\fP feature of the current directory\(cqs
318package.
319.RE
320.SS "Compilation Options"
321.sp
322\fB\-\-target\fP \fITRIPLE\fP
323.RS 4
324Test for the given architecture. The default is the host
325architecture. The general format of the triple is
326\fB<arch><sub>\-<vendor>\-<sys>\-<abi>\fP. Run \fBrustc \-\-print target\-list\fP for a
327list of supported targets.
328.sp
329This may also be specified with the \fBbuild.target\fP
330.URL "https://doc.rust\-lang.org/cargo/reference/config.html" "config value" "."
331.sp
332Note that specifying this flag makes Cargo run in a different mode where the
333target artifacts are placed in a separate directory. See the
334.URL "https://doc.rust\-lang.org/cargo/guide/build\-cache.html" "build cache" " "
335documentation for more details.
336.RE
337.sp
338\fB\-\-release\fP
339.RS 4
340Test optimized artifacts with the \fBrelease\fP profile. See the
341PROFILES section for details on how this affects profile selection.
342.RE
343.SS "Output Options"
344.sp
345\fB\-\-target\-dir\fP \fIDIRECTORY\fP
346.RS 4
347Directory for all generated artifacts and intermediate files. May also be
348specified with the \fBCARGO_TARGET_DIR\fP environment variable, or the
349\fBbuild.target\-dir\fP \c
350.URL "https://doc.rust\-lang.org/cargo/reference/config.html" "config value" "."
351Defaults
352to \fBtarget\fP in the root of the workspace.
353.RE
354.SS "Display Options"
355.sp
356By default the Rust test harness hides output from test execution to keep
357results readable. Test output can be recovered (e.g., for debugging) by passing
358\fB\-\-nocapture\fP to the test binaries:
359.sp
360.if n .RS 4
361.nf
362cargo test \-\- \-\-nocapture
363.fi
364.if n .RE
365.sp
366\fB\-v\fP, \fB\-\-verbose\fP
367.RS 4
368Use verbose output. May be specified twice for "very verbose" output which
369includes extra output such as dependency warnings and build script output.
370May also be specified with the \fBterm.verbose\fP
371.URL "https://doc.rust\-lang.org/cargo/reference/config.html" "config value" "."
372.RE
373.sp
374\fB\-q\fP, \fB\-\-quiet\fP
375.RS 4
376No output printed to stdout.
377.RE
378.sp
379\fB\-\-color\fP \fIWHEN\fP
380.RS 4
381Control when colored output is used. Valid values:
382.sp
383.RS 4
384.ie n \{\
385\h'-04'\(bu\h'+03'\c
386.\}
387.el \{\
388. sp -1
389. IP \(bu 2.3
390.\}
391\fBauto\fP (default): Automatically detect if color support is available on the
392terminal.
393.RE
394.sp
395.RS 4
396.ie n \{\
397\h'-04'\(bu\h'+03'\c
398.\}
399.el \{\
400. sp -1
401. IP \(bu 2.3
402.\}
403\fBalways\fP: Always display colors.
404.RE
405.sp
406.RS 4
407.ie n \{\
408\h'-04'\(bu\h'+03'\c
409.\}
410.el \{\
411. sp -1
412. IP \(bu 2.3
413.\}
414\fBnever\fP: Never display colors.
415.RE
416.sp
417May also be specified with the \fBterm.color\fP
418.URL "https://doc.rust\-lang.org/cargo/reference/config.html" "config value" "."
419.RE
420.sp
421\fB\-\-message\-format\fP \fIFMT\fP
422.RS 4
423The output format for diagnostic messages. Can be specified multiple times
424and consists of comma\-separated values. Valid values:
425.sp
426.RS 4
427.ie n \{\
428\h'-04'\(bu\h'+03'\c
429.\}
430.el \{\
431. sp -1
432. IP \(bu 2.3
433.\}
434\fBhuman\fP (default): Display in a human\-readable text format.
435.RE
436.sp
437.RS 4
438.ie n \{\
439\h'-04'\(bu\h'+03'\c
440.\}
441.el \{\
442. sp -1
443. IP \(bu 2.3
444.\}
445\fBshort\fP: Emit shorter, human\-readable text messages.
446.RE
447.sp
448.RS 4
449.ie n \{\
450\h'-04'\(bu\h'+03'\c
451.\}
452.el \{\
453. sp -1
454. IP \(bu 2.3
455.\}
456\fBjson\fP: Emit JSON messages to stdout. See
457\c
458.URL "https://doc.rust\-lang.org/cargo/reference/external\-tools.html#json\-messages" "the reference"
459for more details.
460.RE
461.sp
462.RS 4
463.ie n \{\
464\h'-04'\(bu\h'+03'\c
465.\}
466.el \{\
467. sp -1
468. IP \(bu 2.3
469.\}
470\fBjson\-diagnostic\-short\fP: Ensure the \fBrendered\fP field of JSON messages contains
471the "short" rendering from rustc.
472.RE
473.sp
474.RS 4
475.ie n \{\
476\h'-04'\(bu\h'+03'\c
477.\}
478.el \{\
479. sp -1
480. IP \(bu 2.3
481.\}
482\fBjson\-diagnostic\-rendered\-ansi\fP: Ensure the \fBrendered\fP field of JSON messages
483contains embedded ANSI color codes for respecting rustc\(cqs default color
484scheme.
485.RE
486.sp
487.RS 4
488.ie n \{\
489\h'-04'\(bu\h'+03'\c
490.\}
491.el \{\
492. sp -1
493. IP \(bu 2.3
494.\}
495\fBjson\-render\-diagnostics\fP: Instruct Cargo to not include rustc diagnostics in
496in JSON messages printed, but instead Cargo itself should render the
497JSON diagnostics coming from rustc. Cargo\(cqs own JSON diagnostics and others
498coming from rustc are still emitted.
499.RE
500.RE
501.SS "Manifest Options"
502.sp
503\fB\-\-manifest\-path\fP \fIPATH\fP
504.RS 4
505Path to the \fBCargo.toml\fP file. By default, Cargo searches for the
506\fBCargo.toml\fP file in the current directory or any parent directory.
507.RE
508.sp
509\fB\-\-frozen\fP, \fB\-\-locked\fP
510.RS 4
511Either of these flags requires that the \fBCargo.lock\fP file is
512up\-to\-date. If the lock file is missing, or it needs to be updated, Cargo will
513exit with an error. The \fB\-\-frozen\fP flag also prevents Cargo from
514attempting to access the network to determine if it is out\-of\-date.
515.sp
516These may be used in environments where you want to assert that the
517\fBCargo.lock\fP file is up\-to\-date (such as a CI build) or want to avoid network
518access.
519.RE
520.sp
521\fB\-\-offline\fP
522.RS 4
523Prevents Cargo from accessing the network for any reason. Without this
524flag, Cargo will stop with an error if it needs to access the network and
525the network is not available. With this flag, Cargo will attempt to
526proceed without the network if possible.
527.sp
528Beware that this may result in different dependency resolution than online
529mode. Cargo will restrict itself to crates that are downloaded locally, even
530if there might be a newer version as indicated in the local copy of the index.
531See the \fBcargo\-fetch\fP(1) command to download dependencies before going
532offline.
533.sp
534May also be specified with the \fBnet.offline\fP \c
535.URL "https://doc.rust\-lang.org/cargo/reference/config.html" "config value" "."
536.RE
537.SS "Common Options"
538.sp
539\fB+TOOLCHAIN\fP
540.RS 4
541If Cargo has been installed with rustup, and the first argument to \fBcargo\fP
542begins with \fB+\fP, it will be interpreted as a rustup toolchain name (such
543as \fB+stable\fP or \fB+nightly\fP).
544See the \c
545.URL "https://github.com/rust\-lang/rustup/" "rustup documentation"
546for more information about how toolchain overrides work.
547.RE
548.sp
549\fB\-h\fP, \fB\-\-help\fP
550.RS 4
551Prints help information.
552.RE
553.sp
554\fB\-Z\fP \fIFLAG\fP...
555.RS 4
556Unstable (nightly\-only) flags to Cargo. Run \fBcargo \-Z help\fP for
557details.
558.RE
559.SS "Miscellaneous Options"
560.sp
561The \fB\-\-jobs\fP argument affects the building of the test executable but does not
562affect how many threads are used when running the tests. The Rust test harness
563includes an option to control the number of threads used:
564.sp
565.if n .RS 4
566.nf
567cargo test \-j 2 \-\- \-\-test\-threads=2
568.fi
569.if n .RE
570.sp
571\fB\-j\fP \fIN\fP, \fB\-\-jobs\fP \fIN\fP
572.RS 4
573Number of parallel jobs to run. May also be specified with the
574\fBbuild.jobs\fP \c
575.URL "https://doc.rust\-lang.org/cargo/reference/config.html" "config value" "."
576Defaults to
577the number of CPUs.
578.RE
579.SH "PROFILES"
580.sp
581Profiles may be used to configure compiler options such as optimization levels
582and debug settings. See
583\c
584.URL "https://doc.rust\-lang.org/cargo/reference/profiles.html" "the reference"
585for more details.
586.sp
587Profile selection depends on the target and crate being built. By default the
588\fBdev\fP or \fBtest\fP profiles are used. If the \fB\-\-release\fP flag is given, then the
589\fBrelease\fP or \fBbench\fP profiles are used.
590.TS
591allbox tab(:);
592lt lt lt.
593T{
594.sp
595Target
596T}:T{
597.sp
598Default Profile
599T}:T{
600.sp
601\fB\-\-release\fP Profile
602T}
603T{
604.sp
605lib, bin, example
606T}:T{
607.sp
608\fBdev\fP
609T}:T{
610.sp
611\fBrelease\fP
612T}
613T{
614.sp
615test, bench, or any target
616.br
617in "test" or "bench" mode
618T}:T{
619.sp
620\fBtest\fP
621T}:T{
622.sp
623\fBbench\fP
624T}
625.TE
626.sp
627.sp
628Dependencies use the \fBdev\fP/\fBrelease\fP profiles.
629.sp
630Unit tests are separate executable artifacts which use the \fBtest\fP/\fBbench\fP
631profiles. Example targets are built the same as with \fBcargo build\fP (using the
632\fBdev\fP/\fBrelease\fP profiles) unless you are building them with the test harness
633(by setting \fBtest = true\fP in the manifest or using the \fB\-\-example\fP flag) in
634which case they use the \fBtest\fP/\fBbench\fP profiles. Library targets are built
635with the \fBdev\fP/\fBrelease\fP profiles when linked to an integration test, binary,
636or doctest.
637.SH "ENVIRONMENT"
638.sp
639See \c
640.URL "https://doc.rust\-lang.org/cargo/reference/environment\-variables.html" "the reference" " "
641for
642details on environment variables that Cargo reads.
643.SH "EXIT STATUS"
644.sp
6450
646.RS 4
647Cargo succeeded.
648.RE
649.sp
650101
651.RS 4
652Cargo failed to complete.
653.RE
654.SH "EXAMPLES"
655.sp
656.RS 4
657.ie n \{\
658\h'-04' 1.\h'+01'\c
659.\}
660.el \{\
661. sp -1
662. IP " 1." 4.2
663.\}
664Execute all the unit and integration tests of the current package:
665.sp
666.if n .RS 4
667.nf
668cargo test
669.fi
670.if n .RE
671.RE
672.sp
673.RS 4
674.ie n \{\
675\h'-04' 2.\h'+01'\c
676.\}
677.el \{\
678. sp -1
679. IP " 2." 4.2
680.\}
681Run only a specific test within a specific integration test:
682.sp
683.if n .RS 4
684.nf
685cargo test \-\-test int_test_name \-\- modname::test_name
686.fi
687.if n .RE
688.RE
689.SH "SEE ALSO"
690.sp
691\fBcargo\fP(1), \fBcargo\-bench\fP(1)