@@ -36,6 +36,18 @@ mod tests {
36
36
}
37
37
```
38
38
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
+
39
51
Note the ` #[test] ` . This attribute indicates that this is a test function. It
40
52
currently has no body. That's good enough to pass! We can run the tests with
41
53
` cargo test ` :
@@ -47,7 +59,7 @@ $ cargo test
47
59
Running target/debug/deps/adder-941f01916ca4a642
48
60
49
61
running 1 test
50
- test tests:: it_works ... ok
62
+ test it_works ... ok
51
63
52
64
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
53
65
@@ -63,10 +75,10 @@ for the test we wrote, and another for documentation tests. We'll talk about
63
75
those later. For now, see this line:
64
76
65
77
``` text
66
- test tests:: it_works ... ok
78
+ test it_works ... ok
67
79
```
68
80
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:
70
82
71
83
``` rust
72
84
fn it_works () {
@@ -87,12 +99,9 @@ and any test that does `panic!` fails. Let's make our test fail:
87
99
# // test:
88
100
# // fn main
89
101
#
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 );
96
105
}
97
106
```
98
107
@@ -107,17 +116,17 @@ $ cargo test
107
116
Running target/debug/deps/adder-941f01916ca4a642
108
117
109
118
running 1 test
110
- test tests:: it_works ... FAILED
119
+ test it_works ... FAILED
111
120
112
121
failures:
113
122
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
116
125
note: Run with ` RUST_BACKTRACE=1` for a backtrace.
117
126
118
127
119
128
failures:
120
- tests:: it_works
129
+ it_works
121
130
122
131
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured
123
132
@@ -127,7 +136,7 @@ error: test failed
127
136
Rust indicates that our test failed:
128
137
129
138
``` text
130
- test tests:: it_works ... FAILED
139
+ test it_works ... FAILED
131
140
```
132
141
133
142
And that's reflected in the summary line:
@@ -165,15 +174,11 @@ We can invert our test's failure with another attribute: `should_panic`:
165
174
# // test:
166
175
# // fn main
167
176
#
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 );
175
181
}
176
-
177
182
```
178
183
179
184
This test will now succeed if we ` panic! ` and fail if we complete. Let's try it:
@@ -185,7 +190,7 @@ $ cargo test
185
190
Running target/debug/deps/adder-941f01916ca4a642
186
191
187
192
running 1 test
188
- test tests:: it_works ... ok
193
+ test it_works ... ok
189
194
190
195
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
191
196
@@ -204,13 +209,10 @@ equality:
204
209
# // test:
205
210
# // fn main
206
211
#
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" );
214
216
}
215
217
```
216
218
@@ -224,7 +226,7 @@ $ cargo test
224
226
Running target/debug/deps/adder-941f01916ca4a642
225
227
226
228
running 1 test
227
- test tests:: it_works ... ok
229
+ test it_works ... ok
228
230
229
231
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
230
232
@@ -246,13 +248,10 @@ of the example above would be:
246
248
# // test:
247
249
# // fn main
248
250
#
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" );
256
255
}
257
256
```
258
257
@@ -267,14 +266,9 @@ pub fn add_two(a: i32) -> i32 {
267
266
a + 2
268
267
}
269
268
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));
278
272
}
279
273
```
280
274
@@ -295,20 +289,15 @@ pub fn add_two(a: i32) -> i32 {
295
289
a + 2
296
290
}
297
291
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
+ }
306
296
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
312
301
}
313
302
```
314
303
@@ -322,8 +311,8 @@ $ cargo test
322
311
Running target/debug/deps/adder-941f01916ca4a642
323
312
324
313
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
327
316
328
317
test result: ok. 1 passed; 0 failed; 1 ignored; 0 measured
329
318
@@ -342,7 +331,7 @@ $ cargo test -- --ignored
342
331
Running target/debug/deps/adder-941f01916ca4a642
343
332
344
333
running 1 test
345
- test tests:: expensive_test ... ok
334
+ test expensive_test ... ok
346
335
347
336
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
348
337
0 commit comments