Skip to content

Commit 66da9a2

Browse files
authored
Rollup merge of rust-lang#37368 - trotter:patch-1, r=steveklabnik
Update testing.md to reflect changes to cargo new `cargo new` now creates a `src/lib.rs` with a `tests` module by default. I've updated the earlier examples in this doc to reflect this. However, I don't know how we want to approach the "introduction" to idiomatic testing that follows in "the tests module" section. I _think_ it should be broken apart, with the module concept being introduced early on, and the `super` concept being addressed when we hit the `add_two` example. I'd like to get agreement on that being the right approach before I do it though. I _also_ removed the `#fn main() {}` hidden at the beginning of each example, as these cause Rust Playground to not treat the file as a set of tests that it can run. Removing it _should_ cause Rust Playground to display a "Test >" button in the top left when a user runs the code, which will allow them to see the test runner output.
2 parents 2db360e + 2a832a0 commit 66da9a2

File tree

1 file changed

+92
-31
lines changed

1 file changed

+92
-31
lines changed

src/doc/book/testing.md

+92-31
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ $ cd adder
2323
Cargo will automatically generate a simple test when you make a new project.
2424
Here's the contents of `src/lib.rs`:
2525

26-
```rust
26+
```rust,ignore
27+
# // The next line exists to trick play.rust-lang.org into running our code as a
28+
# // test:
29+
# // fn main
30+
#
2731
#[cfg(test)]
2832
mod tests {
2933
#[test]
@@ -32,17 +36,30 @@ mod tests {
3236
}
3337
```
3438

39+
For now, let's remove the `mod` bit, and focus on just the function:
40+
41+
```rust,ignore
42+
# // The next line exists to trick play.rust-lang.org into running our code as a
43+
# // test:
44+
# // fn main
45+
#
46+
#[test]
47+
fn it_works() {
48+
}
49+
```
50+
3551
Note the `#[test]`. This attribute indicates that this is a test function. It
3652
currently has no body. That's good enough to pass! We can run the tests with
3753
`cargo test`:
3854

3955
```bash
4056
$ cargo test
4157
Compiling adder v0.1.0 (file:///home/you/projects/adder)
42-
Running target/debug/deps/adder-91b3e234d4ed382a
58+
Finished debug [unoptimized + debuginfo] target(s) in 0.15 secs
59+
Running target/debug/deps/adder-941f01916ca4a642
4360

4461
running 1 test
45-
test tests::it_works ... ok
62+
test it_works ... ok
4663

4764
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
4865

@@ -58,13 +75,15 @@ for the test we wrote, and another for documentation tests. We'll talk about
5875
those later. For now, see this line:
5976

6077
```text
61-
test tests::it_works ... ok
78+
test it_works ... ok
6279
```
6380

6481
Note the `it_works`. This comes from the name of our function:
6582

6683
```rust
84+
# fn main() {
6785
fn it_works() {
86+
}
6887
# }
6988
```
7089

@@ -77,8 +96,11 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
7796
So why does our do-nothing test pass? Any test which doesn't `panic!` passes,
7897
and any test that does `panic!` fails. Let's make our test fail:
7998

80-
```rust
81-
# fn main() {}
99+
```rust,ignore
100+
# // The next line exists to trick play.rust-lang.org into running our code as a
101+
# // test:
102+
# // fn main
103+
#
82104
#[test]
83105
fn it_works() {
84106
assert!(false);
@@ -92,19 +114,21 @@ run our tests again:
92114
```bash
93115
$ cargo test
94116
Compiling adder v0.1.0 (file:///home/you/projects/adder)
95-
Running target/debug/deps/adder-91b3e234d4ed382a
117+
Finished debug [unoptimized + debuginfo] target(s) in 0.17 secs
118+
Running target/debug/deps/adder-941f01916ca4a642
96119

97120
running 1 test
98-
test tests::it_works ... FAILED
121+
test it_works ... FAILED
99122

100123
failures:
101124

102-
---- test::it_works stdout ----
103-
thread 'tests::it_works' panicked at 'assertion failed: false', src/lib.rs:5
125+
---- it_works stdout ----
126+
thread 'it_works' panicked at 'assertion failed: false', src/lib.rs:5
127+
note: Run with `RUST_BACKTRACE=1` for a backtrace.
104128

105129

106130
failures:
107-
tests::it_works
131+
it_works
108132

109133
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured
110134

@@ -114,7 +138,7 @@ error: test failed
114138
Rust indicates that our test failed:
115139

116140
```text
117-
test tests::it_works ... FAILED
141+
test it_works ... FAILED
118142
```
119143

120144
And that's reflected in the summary line:
@@ -147,8 +171,11 @@ This is useful if you want to integrate `cargo test` into other tooling.
147171

148172
We can invert our test's failure with another attribute: `should_panic`:
149173

150-
```rust
151-
# fn main() {}
174+
```rust,ignore
175+
# // The next line exists to trick play.rust-lang.org into running our code as a
176+
# // test:
177+
# // fn main
178+
#
152179
#[test]
153180
#[should_panic]
154181
fn it_works() {
@@ -161,10 +188,11 @@ This test will now succeed if we `panic!` and fail if we complete. Let's try it:
161188
```bash
162189
$ cargo test
163190
Compiling adder v0.1.0 (file:///home/you/projects/adder)
164-
Running target/debug/deps/adder-91b3e234d4ed382a
191+
Finished debug [unoptimized + debuginfo] target(s) in 0.17 secs
192+
Running target/debug/deps/adder-941f01916ca4a642
165193

166194
running 1 test
167-
test tests::it_works ... ok
195+
test it_works ... ok
168196

169197
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
170198

@@ -178,8 +206,11 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
178206
Rust provides another macro, `assert_eq!`, that compares two arguments for
179207
equality:
180208

181-
```rust
182-
# fn main() {}
209+
```rust,ignore
210+
# // The next line exists to trick play.rust-lang.org into running our code as a
211+
# // test:
212+
# // fn main
213+
#
183214
#[test]
184215
#[should_panic]
185216
fn it_works() {
@@ -193,10 +224,11 @@ passes:
193224
```bash
194225
$ cargo test
195226
Compiling adder v0.1.0 (file:///home/you/projects/adder)
196-
Running target/debug/deps/adder-91b3e234d4ed382a
227+
Finished debug [unoptimized + debuginfo] target(s) in 0.21 secs
228+
Running target/debug/deps/adder-941f01916ca4a642
197229

198230
running 1 test
199-
test tests::it_works ... ok
231+
test it_works ... ok
200232

201233
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
202234

@@ -213,8 +245,11 @@ parameter can be added to the `should_panic` attribute. The test harness will
213245
make sure that the failure message contains the provided text. A safer version
214246
of the example above would be:
215247

216-
```rust
217-
# fn main() {}
248+
```rust,ignore
249+
# // The next line exists to trick play.rust-lang.org into running our code as a
250+
# // test:
251+
# // fn main
252+
#
218253
#[test]
219254
#[should_panic(expected = "assertion failed")]
220255
fn it_works() {
@@ -225,7 +260,10 @@ fn it_works() {
225260
That's all there is to the basics! Let's write one 'real' test:
226261

227262
```rust,ignore
228-
# fn main() {}
263+
# // The next line exists to trick play.rust-lang.org into running our code as a
264+
# // test:
265+
# // fn main
266+
#
229267
pub fn add_two(a: i32) -> i32 {
230268
a + 2
231269
}
@@ -244,8 +282,15 @@ some known arguments and compare it to the expected output.
244282
Sometimes a few specific tests can be very time-consuming to execute. These
245283
can be disabled by default by using the `ignore` attribute:
246284

247-
```rust
248-
# fn main() {}
285+
```rust,ignore
286+
# // The next line exists to trick play.rust-lang.org into running our code as a
287+
# // test:
288+
# // fn main
289+
#
290+
pub fn add_two(a: i32) -> i32 {
291+
a + 2
292+
}
293+
249294
#[test]
250295
fn it_works() {
251296
assert_eq!(4, add_two(2));
@@ -264,7 +309,8 @@ not:
264309
```bash
265310
$ cargo test
266311
Compiling adder v0.1.0 (file:///home/you/projects/adder)
267-
Running target/debug/deps/adder-91b3e234d4ed382a
312+
Finished debug [unoptimized + debuginfo] target(s) in 0.20 secs
313+
Running target/debug/deps/adder-941f01916ca4a642
268314

269315
running 2 tests
270316
test expensive_test ... ignored
@@ -283,7 +329,8 @@ The expensive tests can be run explicitly using `cargo test -- --ignored`:
283329

284330
```bash
285331
$ cargo test -- --ignored
286-
Running target/debug/deps/adder-91b3e234d4ed382a
332+
Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
333+
Running target/debug/deps/adder-941f01916ca4a642
287334

288335
running 1 test
289336
test expensive_test ... ok
@@ -310,7 +357,10 @@ was missing from our last example. Let's explain what this does.
310357
The idiomatic way of writing our example looks like this:
311358

312359
```rust,ignore
313-
# fn main() {}
360+
# // The next line exists to trick play.rust-lang.org into running our code as a
361+
# // test:
362+
# // fn main
363+
#
314364
pub fn add_two(a: i32) -> i32 {
315365
a + 2
316366
}
@@ -339,7 +389,10 @@ a large module, and so this is a common use of globs. Let's change our
339389
`src/lib.rs` to make use of it:
340390

341391
```rust,ignore
342-
# fn main() {}
392+
# // The next line exists to trick play.rust-lang.org into running our code as a
393+
# // test:
394+
# // fn main
395+
#
343396
pub fn add_two(a: i32) -> i32 {
344397
a + 2
345398
}
@@ -389,9 +442,14 @@ To write an integration test, let's make a `tests` directory and
389442
put a `tests/integration_test.rs` file inside with this as its contents:
390443

391444
```rust,ignore
445+
# // The next line exists to trick play.rust-lang.org into running our code as a
446+
# // test:
447+
# // fn main
448+
#
449+
# // Sadly, this code will not work in play.rust-lang.org, because we have no
450+
# // crate adder to import. You'll need to try this part on your own machine.
392451
extern crate adder;
393452
394-
# fn main() {}
395453
#[test]
396454
fn it_works() {
397455
assert_eq!(4, adder::add_two(2));
@@ -452,7 +510,10 @@ running examples in your documentation (**note:** this only works in library
452510
crates, not binary crates). Here's a fleshed-out `src/lib.rs` with examples:
453511

454512
```rust,ignore
455-
# fn main() {}
513+
# // The next line exists to trick play.rust-lang.org into running our code as a
514+
# // test:
515+
# // fn main
516+
#
456517
//! The `adder` crate provides functions that add numbers to other numbers.
457518
//!
458519
//! # Examples

0 commit comments

Comments
 (0)