Skip to content

Commit cce487e

Browse files
committed
Auto merge of #10726 - weihanglo:test-doc-enhance, r=epage
Enhance documentation of testing
2 parents 03e2235 + 5d33609 commit cce487e

19 files changed

+237
-74
lines changed

src/doc/man/cargo-bench.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ similarly named benchmarks like `foobar`):
2828

2929
cargo bench -- foo --exact
3030

31-
Benchmarks are built with the `--test` option to `rustc` which creates an
32-
executable with a `main` function that automatically runs all functions
33-
annotated with the `#[bench]` attribute. Cargo passes the `--bench` flag to
34-
the test harness to tell it to run only benchmarks.
31+
Benchmarks are built with the `--test` option to `rustc` which creates a
32+
special executable by linking your code with libtest. The executable
33+
automatically runs all functions annotated with the `#[bench]` attribute.
34+
Cargo passes the `--bench` flag to the test harness to tell it to run
35+
only benchmarks.
3536

3637
The libtest harness may be disabled by setting `harness = false` in the target
3738
manifest settings, in which case your code will need to provide its own `main`
@@ -81,6 +82,8 @@ them from being benchmarked by default. Target selection options that take a
8182
target by name ignore the `bench` flag and will always benchmark the given
8283
target.
8384

85+
{{> options-targets-bin-auto-built }}
86+
8487
{{> options-targets }}
8588

8689
{{> section-features }}

src/doc/man/cargo-build.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ When no target selection options are given, `cargo build` will build all
2323
binary and library targets of the selected packages. Binaries are skipped if
2424
they have `required-features` that are missing.
2525

26+
{{> options-targets-bin-auto-built }}
27+
2628
{{> options-targets }}
2729

2830
{{> section-features }}

src/doc/man/cargo-rustc.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ To pass flags to all compiler processes spawned by Cargo, use the `RUSTFLAGS`
3535
When no target selection options are given, `cargo rustc` will build all
3636
binary and library targets of the selected package.
3737

38+
{{> options-targets-bin-auto-built }}
39+
3840
{{> options-targets }}
3941

4042
{{> section-features }}

src/doc/man/cargo-test.md

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ cargo-test - Execute unit and integration tests of a package
1212

1313
## DESCRIPTION
1414

15-
Compile and execute unit and integration tests.
15+
Compile and execute unit, integration, and documentation tests.
1616

1717
The test filtering argument `TESTNAME` and all the arguments following the two
1818
dashes (`--`) are passed to the test binaries and thus to _libtest_ (rustc's
@@ -27,20 +27,34 @@ on 3 threads in parallel:
2727

2828
cargo test foo -- --test-threads 3
2929

30-
Tests are built with the `--test` option to `rustc` which creates an
31-
executable with a `main` function that automatically runs all functions
32-
annotated with the `#[test]` attribute in multiple threads. `#[bench]`
33-
annotated functions will also be run with one iteration to verify that they
34-
are functional.
30+
Tests are built with the `--test` option to `rustc` which creates a special
31+
executable by linking your code with libtest. The executable automatically
32+
runs all functions annotated with the `#[test]` attribute in multiple threads.
33+
`#[bench]` annotated functions will also be run with one iteration to verify
34+
that they are functional.
35+
36+
If the package contains multiple test targets, each target compiles to a
37+
special executable as aforementioned, and then is run serially.
3538

3639
The libtest harness may be disabled by setting `harness = false` in the target
3740
manifest settings, in which case your code will need to provide its own `main`
3841
function to handle running tests.
3942

43+
### Documentation tests
44+
4045
Documentation tests are also run by default, which is handled by `rustdoc`. It
41-
extracts code samples from documentation comments and executes them. See the
42-
[rustdoc book](https://doc.rust-lang.org/rustdoc/) for more information on
43-
writing doc tests.
46+
extracts code samples from documentation comments of the library target, and
47+
then executes them.
48+
49+
Different from normal test targets, each code block compiles to a doctest
50+
executable on the fly with `rustc`. These executables run in parallel in
51+
separate processes. The compilation of a code block is in fact a part of test
52+
function controlled by libtest, so some options such as `--jobs` might not
53+
take effect. Note that this execution model of doctests is not guaranteed
54+
and may change in the future; beware of depending on it.
55+
56+
See the [rustdoc book](https://doc.rust-lang.org/rustdoc/) for more information
57+
on writing doc tests.
4458

4559
## OPTIONS
4660

@@ -73,13 +87,7 @@ ignore the `test` flag and will always test the given target.
7387
Doc tests for libraries may be disabled by setting `doctest = false` for the
7488
library in the manifest.
7589

76-
Binary targets are automatically built if there is an integration test or
77-
benchmark. This allows an integration test to execute the binary to exercise
78-
and test its behavior. The `CARGO_BIN_EXE_<name>`
79-
[environment variable](../reference/environment-variables.html#environment-variables-cargo-sets-for-crates)
80-
is set when the integration test is built so that it can use the
81-
[`env` macro](https://doc.rust-lang.org/std/macro.env.html) to locate the
82-
executable.
90+
{{> options-targets-bin-auto-built }}
8391

8492
{{> options-targets }}
8593

src/doc/man/generated_txt/cargo-bench.txt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ DESCRIPTION
2323

2424
cargo bench -- foo --exact
2525

26-
Benchmarks are built with the --test option to rustc which creates an
27-
executable with a main function that automatically runs all functions
28-
annotated with the #[bench] attribute. Cargo passes the --bench flag to
29-
the test harness to tell it to run only benchmarks.
26+
Benchmarks are built with the --test option to rustc which creates a
27+
special executable by linking your code with libtest. The executable
28+
automatically runs all functions annotated with the #[bench] attribute.
29+
Cargo passes the --bench flag to the test harness to tell it to run only
30+
benchmarks.
3031

3132
The libtest harness may be disabled by setting harness = false in the
3233
target manifest settings, in which case your code will need to provide
@@ -117,6 +118,15 @@ OPTIONS
117118
options that take a target by name ignore the bench flag and will always
118119
benchmark the given target.
119120

121+
Binary targets are automatically built if there is an integration test
122+
or benchmark being selected to benchmark. This allows an integration
123+
test to execute the binary to exercise and test its behavior. The
124+
CARGO_BIN_EXE_<name> environment variable
125+
<https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates>
126+
is set when the integration test is built so that it can use the env
127+
macro <https://doc.rust-lang.org/std/macro.env.html> to locate the
128+
executable.
129+
120130
Passing target selection flags will benchmark only the specified
121131
targets.
122132

src/doc/man/generated_txt/cargo-build.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ OPTIONS
5151
binary and library targets of the selected packages. Binaries are
5252
skipped if they have required-features that are missing.
5353

54+
Binary targets are automatically built if there is an integration test
55+
or benchmark being selected to build. This allows an integration test to
56+
execute the binary to exercise and test its behavior. The
57+
CARGO_BIN_EXE_<name> environment variable
58+
<https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates>
59+
is set when the integration test is built so that it can use the env
60+
macro <https://doc.rust-lang.org/std/macro.env.html> to locate the
61+
executable.
62+
5463
Passing target selection flags will build only the specified targets.
5564

5665
Note that --bin, --example, --test and --bench flags also support common

src/doc/man/generated_txt/cargo-rustc.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ OPTIONS
4242
When no target selection options are given, cargo rustc will build all
4343
binary and library targets of the selected package.
4444

45+
Binary targets are automatically built if there is an integration test
46+
or benchmark being selected to build. This allows an integration test to
47+
execute the binary to exercise and test its behavior. The
48+
CARGO_BIN_EXE_<name> environment variable
49+
<https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates>
50+
is set when the integration test is built so that it can use the env
51+
macro <https://doc.rust-lang.org/std/macro.env.html> to locate the
52+
executable.
53+
4554
Passing target selection flags will build only the specified targets.
4655

4756
Note that --bin, --example, --test and --bench flags also support common

src/doc/man/generated_txt/cargo-test.txt

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ SYNOPSIS
77
cargo test [options] [testname] [-- test-options]
88

99
DESCRIPTION
10-
Compile and execute unit and integration tests.
10+
Compile and execute unit, integration, and documentation tests.
1111

1212
The test filtering argument TESTNAME and all the arguments following the
1313
two dashes (--) are passed to the test binaries and thus to libtest
@@ -23,20 +23,34 @@ DESCRIPTION
2323

2424
cargo test foo -- --test-threads 3
2525

26-
Tests are built with the --test option to rustc which creates an
27-
executable with a main function that automatically runs all functions
28-
annotated with the #[test] attribute in multiple threads. #[bench]
29-
annotated functions will also be run with one iteration to verify that
30-
they are functional.
26+
Tests are built with the --test option to rustc which creates a special
27+
executable by linking your code with libtest. The executable
28+
automatically runs all functions annotated with the #[test] attribute in
29+
multiple threads. #[bench] annotated functions will also be run with one
30+
iteration to verify that they are functional.
31+
32+
If the package contains multiple test targets, each target compiles to a
33+
special executable as aforementioned, and then is run serially.
3134

3235
The libtest harness may be disabled by setting harness = false in the
3336
target manifest settings, in which case your code will need to provide
3437
its own main function to handle running tests.
3538

39+
Documentation tests
3640
Documentation tests are also run by default, which is handled by
37-
rustdoc. It extracts code samples from documentation comments and
38-
executes them. See the rustdoc book <https://doc.rust-lang.org/rustdoc/>
39-
for more information on writing doc tests.
41+
rustdoc. It extracts code samples from documentation comments of the
42+
library target, and then executes them.
43+
44+
Different from normal test targets, each code block compiles to a
45+
doctest executable on the fly with rustc. These executables run in
46+
parallel in separate processes. The compilation of a code block is in
47+
fact a part of test function controlled by libtest, so some options such
48+
as --jobs might not take effect. Note that this execution model of
49+
doctests is not guaranteed and may change in the future; beware of
50+
depending on it.
51+
52+
See the rustdoc book <https://doc.rust-lang.org/rustdoc/> for more
53+
information on writing doc tests.
4054

4155
OPTIONS
4256
Test Options
@@ -116,9 +130,9 @@ OPTIONS
116130
the library in the manifest.
117131

118132
Binary targets are automatically built if there is an integration test
119-
or benchmark. This allows an integration test to execute the binary to
120-
exercise and test its behavior. The CARGO_BIN_EXE_<name> environment
121-
variable
133+
or benchmark being selected to test. This allows an integration test to
134+
execute the binary to exercise and test its behavior. The
135+
CARGO_BIN_EXE_<name> environment variable
122136
<https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates>
123137
is set when the integration test is built so that it can use the env
124138
macro <https://doc.rust-lang.org/std/macro.env.html> to locate the
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Binary targets are automatically built if there is an integration test or
2+
benchmark being selected to {{lower actionverb}}. This allows an integration
3+
test to execute the binary to exercise and test its behavior.
4+
The `CARGO_BIN_EXE_<name>`
5+
[environment variable](../reference/environment-variables.html#environment-variables-cargo-sets-for-crates)
6+
is set when the integration test is built so that it can use the
7+
[`env` macro](https://doc.rust-lang.org/std/macro.env.html) to locate the
8+
executable.

src/doc/src/commands/cargo-bench.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ similarly named benchmarks like `foobar`):
2828

2929
cargo bench -- foo --exact
3030

31-
Benchmarks are built with the `--test` option to `rustc` which creates an
32-
executable with a `main` function that automatically runs all functions
33-
annotated with the `#[bench]` attribute. Cargo passes the `--bench` flag to
34-
the test harness to tell it to run only benchmarks.
31+
Benchmarks are built with the `--test` option to `rustc` which creates a
32+
special executable by linking your code with libtest. The executable
33+
automatically runs all functions annotated with the `#[bench]` attribute.
34+
Cargo passes the `--bench` flag to the test harness to tell it to run
35+
only benchmarks.
3536

3637
The libtest harness may be disabled by setting `harness = false` in the target
3738
manifest settings, in which case your code will need to provide its own `main`
@@ -138,6 +139,16 @@ them from being benchmarked by default. Target selection options that take a
138139
target by name ignore the `bench` flag and will always benchmark the given
139140
target.
140141

142+
Binary targets are automatically built if there is an integration test or
143+
benchmark being selected to benchmark. This allows an integration
144+
test to execute the binary to exercise and test its behavior.
145+
The `CARGO_BIN_EXE_<name>`
146+
[environment variable](../reference/environment-variables.html#environment-variables-cargo-sets-for-crates)
147+
is set when the integration test is built so that it can use the
148+
[`env` macro](https://doc.rust-lang.org/std/macro.env.html) to locate the
149+
executable.
150+
151+
141152
Passing target selection flags will benchmark only the specified
142153
targets.
143154

0 commit comments

Comments
 (0)