Skip to content

Commit 35903bb

Browse files
committed
Remove mod tests from earlier sections
The narrative flows better if we follow what @steveklabnik is doing in rust-lang/book#288. Therefore, I completely copied it.
1 parent d9c60ca commit 35903bb

File tree

1 file changed

+51
-62
lines changed

1 file changed

+51
-62
lines changed

src/doc/book/testing.md

+51-62
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ mod tests {
3636
}
3737
```
3838

39+
For now, let's remove the `mod` bit, and focus on just the function:
40+
41+
```rust
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+
3951
Note the `#[test]`. This attribute indicates that this is a test function. It
4052
currently has no body. That's good enough to pass! We can run the tests with
4153
`cargo test`:
@@ -47,7 +59,7 @@ $ cargo test
4759
Running target/debug/deps/adder-941f01916ca4a642
4860

4961
running 1 test
50-
test tests::it_works ... ok
62+
test it_works ... ok
5163

5264
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
5365

@@ -63,10 +75,10 @@ for the test we wrote, and another for documentation tests. We'll talk about
6375
those later. For now, see this line:
6476

6577
```text
66-
test tests::it_works ... ok
78+
test it_works ... ok
6779
```
6880

69-
Note the `tests::it_works`. This comes from the name of our module and function:
81+
Note the `it_works`. This comes from the name of our module and function:
7082

7183
```rust
7284
fn it_works() {
@@ -87,12 +99,9 @@ and any test that does `panic!` fails. Let's make our test fail:
8799
# // test:
88100
# // fn main
89101
#
90-
#[cfg(test)]
91-
mod tests {
92-
#[test]
93-
fn it_works() {
94-
assert!(false);
95-
}
102+
#[test]
103+
fn it_works() {
104+
assert!(false);
96105
}
97106
```
98107

@@ -107,17 +116,17 @@ $ cargo test
107116
Running target/debug/deps/adder-941f01916ca4a642
108117

109118
running 1 test
110-
test tests::it_works ... FAILED
119+
test it_works ... FAILED
111120

112121
failures:
113122

114-
---- tests::it_works stdout ----
115-
thread 'tests::it_works' panicked at 'assertion failed: false', src/lib.rs:5
123+
---- it_works stdout ----
124+
thread 'it_works' panicked at 'assertion failed: false', src/lib.rs:5
116125
note: Run with `RUST_BACKTRACE=1` for a backtrace.
117126

118127

119128
failures:
120-
tests::it_works
129+
it_works
121130

122131
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured
123132

@@ -127,7 +136,7 @@ error: test failed
127136
Rust indicates that our test failed:
128137

129138
```text
130-
test tests::it_works ... FAILED
139+
test it_works ... FAILED
131140
```
132141

133142
And that's reflected in the summary line:
@@ -165,15 +174,11 @@ We can invert our test's failure with another attribute: `should_panic`:
165174
# // test:
166175
# // fn main
167176
#
168-
#[cfg(test)]
169-
mod tests {
170-
#[test]
171-
#[should_panic]
172-
fn it_works() {
173-
assert!(false);
174-
}
177+
#[test]
178+
#[should_panic]
179+
fn it_works() {
180+
assert!(false);
175181
}
176-
177182
```
178183

179184
This test will now succeed if we `panic!` and fail if we complete. Let's try it:
@@ -185,7 +190,7 @@ $ cargo test
185190
Running target/debug/deps/adder-941f01916ca4a642
186191

187192
running 1 test
188-
test tests::it_works ... ok
193+
test it_works ... ok
189194

190195
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
191196

@@ -204,13 +209,10 @@ equality:
204209
# // test:
205210
# // fn main
206211
#
207-
#[cfg(test)]
208-
mod tests {
209-
#[test]
210-
#[should_panic]
211-
fn it_works() {
212-
assert_eq!("Hello", "world");
213-
}
212+
#[test]
213+
#[should_panic]
214+
fn it_works() {
215+
assert_eq!("Hello", "world");
214216
}
215217
```
216218

@@ -224,7 +226,7 @@ $ cargo test
224226
Running target/debug/deps/adder-941f01916ca4a642
225227

226228
running 1 test
227-
test tests::it_works ... ok
229+
test it_works ... ok
228230

229231
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
230232

@@ -246,13 +248,10 @@ of the example above would be:
246248
# // test:
247249
# // fn main
248250
#
249-
#[cfg(test)]
250-
mod tests {
251-
#[test]
252-
#[should_panic(expected = "assertion failed")]
253-
fn it_works() {
254-
assert_eq!("Hello", "world");
255-
}
251+
#[test]
252+
#[should_panic(expected = "assertion failed")]
253+
fn it_works() {
254+
assert_eq!("Hello", "world");
256255
}
257256
```
258257

@@ -267,14 +266,9 @@ pub fn add_two(a: i32) -> i32 {
267266
a + 2
268267
}
269268
270-
#[cfg(test)]
271-
mod tests {
272-
use super::add_two;
273-
274-
#[test]
275-
fn it_works() {
276-
assert_eq!(4, add_two(2));
277-
}
269+
#[test]
270+
fn it_works() {
271+
assert_eq!(4, add_two(2));
278272
}
279273
```
280274

@@ -295,20 +289,15 @@ pub fn add_two(a: i32) -> i32 {
295289
a + 2
296290
}
297291

298-
#[cfg(test)]
299-
mod tests {
300-
use super::add_two;
301-
302-
#[test]
303-
fn it_works() {
304-
assert_eq!(4, add_two(2));
305-
}
292+
#[test]
293+
fn it_works() {
294+
assert_eq!(4, add_two(2));
295+
}
306296

307-
#[test]
308-
#[ignore]
309-
fn expensive_test() {
310-
// code that takes an hour to run
311-
}
297+
#[test]
298+
#[ignore]
299+
fn expensive_test() {
300+
// code that takes an hour to run
312301
}
313302
```
314303

@@ -322,8 +311,8 @@ $ cargo test
322311
Running target/debug/deps/adder-941f01916ca4a642
323312

324313
running 2 tests
325-
test tests::expensive_test ... ignored
326-
test tests::it_works ... ok
314+
test expensive_test ... ignored
315+
test it_works ... ok
327316

328317
test result: ok. 1 passed; 0 failed; 1 ignored; 0 measured
329318

@@ -342,7 +331,7 @@ $ cargo test -- --ignored
342331
Running target/debug/deps/adder-941f01916ca4a642
343332

344333
running 1 test
345-
test tests::expensive_test ... ok
334+
test expensive_test ... ok
346335

347336
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
348337

0 commit comments

Comments
 (0)