@@ -38,8 +38,9 @@ currently has no body. That's good enough to pass! We can run the tests with
38
38
39
39
``` bash
40
40
$ cargo test
41
- Compiling adder v0.1.0 (file:///home/you/projects/adder)
42
- Running target/debug/deps/adder-91b3e234d4ed382a
41
+ Compiling adder v0.1.0 (file:///private/tmp/adder)
42
+ Finished debug [unoptimized + debuginfo] target(s) in 0.15 secs
43
+ Running target/debug/deps/adder-941f01916ca4a642
43
44
44
45
running 1 test
45
46
test tests::it_works ... ok
@@ -61,11 +62,11 @@ those later. For now, see this line:
61
62
test tests::it_works ... ok
62
63
```
63
64
64
- Note the ` it_works ` . This comes from the name of our function:
65
+ Note the ` tests:: it_works` . This comes from the name of our module and function:
65
66
66
67
``` rust
67
68
fn it_works () {
68
- # }
69
+ }
69
70
```
70
71
71
72
We also get a summary line:
@@ -78,10 +79,12 @@ So why does our do-nothing test pass? Any test which doesn't `panic!` passes,
78
79
and any test that does ` panic! ` fails. Let's make our test fail:
79
80
80
81
``` rust
81
- # fn main () {}
82
- #[test]
83
- fn it_works () {
84
- assert! (false );
82
+ #[cfg(test)]
83
+ mod tests {
84
+ #[test]
85
+ fn it_works () {
86
+ assert! (false );
87
+ }
85
88
}
86
89
```
87
90
@@ -91,16 +94,18 @@ run our tests again:
91
94
92
95
``` bash
93
96
$ cargo test
94
- Compiling adder v0.1.0 (file:///home/you/projects/adder)
95
- Running target/debug/deps/adder-91b3e234d4ed382a
97
+ Compiling adder v0.1.0 (file:///private/tmp/adder)
98
+ Finished debug [unoptimized + debuginfo] target(s) in 0.17 secs
99
+ Running target/debug/deps/adder-941f01916ca4a642
96
100
97
101
running 1 test
98
102
test tests::it_works ... FAILED
99
103
100
104
failures:
101
105
102
- ---- test ::it_works stdout ----
106
+ ---- tests ::it_works stdout ----
103
107
thread ' tests::it_works' panicked at ' assertion failed: false' , src/lib.rs:5
108
+ note: Run with ` RUST_BACKTRACE=1` for a backtrace.
104
109
105
110
106
111
failures:
@@ -148,20 +153,24 @@ This is useful if you want to integrate `cargo test` into other tooling.
148
153
We can invert our test's failure with another attribute: ` should_panic ` :
149
154
150
155
``` rust
151
- # fn main () {}
152
- #[test]
153
- #[should_panic]
154
- fn it_works () {
155
- assert! (false );
156
+ #[cfg(test)]
157
+ mod tests {
158
+ #[test]
159
+ #[should_panic]
160
+ fn it_works () {
161
+ assert! (false );
162
+ }
156
163
}
164
+
157
165
```
158
166
159
167
This test will now succeed if we ` panic! ` and fail if we complete. Let's try it:
160
168
161
169
``` bash
162
170
$ cargo test
163
- Compiling adder v0.1.0 (file:///home/you/projects/adder)
164
- Running target/debug/deps/adder-91b3e234d4ed382a
171
+ Compiling adder v0.1.0 (file:///private/tmp/adder)
172
+ Finished debug [unoptimized + debuginfo] target(s) in 0.17 secs
173
+ Running target/debug/deps/adder-941f01916ca4a642
165
174
166
175
running 1 test
167
176
test tests::it_works ... ok
@@ -179,11 +188,13 @@ Rust provides another macro, `assert_eq!`, that compares two arguments for
179
188
equality:
180
189
181
190
``` rust
182
- # fn main () {}
183
- #[test]
184
- #[should_panic]
185
- fn it_works () {
186
- assert_eq! (" Hello" , " world" );
191
+ #[cfg(test)]
192
+ mod tests {
193
+ #[test]
194
+ #[should_panic]
195
+ fn it_works () {
196
+ assert_eq! (" Hello" , " world" );
197
+ }
187
198
}
188
199
```
189
200
@@ -192,8 +203,9 @@ passes:
192
203
193
204
``` bash
194
205
$ cargo test
195
- Compiling adder v0.1.0 (file:///home/you/projects/adder)
196
- Running target/debug/deps/adder-91b3e234d4ed382a
206
+ Compiling adder v0.1.0 (file:///private/tmp/adder)
207
+ Finished debug [unoptimized + debuginfo] target(s) in 0.21 secs
208
+ Running target/debug/deps/adder-941f01916ca4a642
197
209
198
210
running 1 test
199
211
test tests::it_works ... ok
@@ -214,25 +226,31 @@ make sure that the failure message contains the provided text. A safer version
214
226
of the example above would be:
215
227
216
228
``` rust
217
- # fn main () {}
218
- #[test]
219
- #[should_panic(expected = " assertion failed" )]
220
- fn it_works () {
221
- assert_eq! (" Hello" , " world" );
229
+ #[cfg(test)]
230
+ mod tests {
231
+ #[test]
232
+ #[should_panic(expected = " assertion failed" )]
233
+ fn it_works () {
234
+ assert_eq! (" Hello" , " world" );
235
+ }
222
236
}
223
237
```
224
238
225
239
That's all there is to the basics! Let's write one 'real' test:
226
240
227
241
``` rust,ignore
228
- # fn main() {}
229
242
pub fn add_two(a: i32) -> i32 {
230
243
a + 2
231
244
}
232
245
233
- #[test]
234
- fn it_works() {
235
- assert_eq!(4, add_two(2));
246
+ #[cfg(test)]
247
+ mod tests {
248
+ use super::add_two;
249
+
250
+ #[test]
251
+ fn it_works() {
252
+ assert_eq!(4, add_two(2));
253
+ }
236
254
}
237
255
```
238
256
@@ -245,16 +263,24 @@ Sometimes a few specific tests can be very time-consuming to execute. These
245
263
can be disabled by default by using the ` ignore ` attribute:
246
264
247
265
``` rust
248
- # fn main () {}
249
- #[test]
250
- fn it_works () {
251
- assert_eq! (4 , add_two (2 ));
266
+ pub fn add_two (a : i32 ) -> i32 {
267
+ a + 2
252
268
}
253
269
254
- #[test]
255
- #[ignore]
256
- fn expensive_test () {
257
- // code that takes an hour to run
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
+ }
278
+
279
+ #[test]
280
+ #[ignore]
281
+ fn expensive_test () {
282
+ // code that takes an hour to run
283
+ }
258
284
}
259
285
```
260
286
@@ -263,12 +289,13 @@ not:
263
289
264
290
``` bash
265
291
$ cargo test
266
- Compiling adder v0.1.0 (file:///home/you/projects/adder)
267
- Running target/debug/deps/adder-91b3e234d4ed382a
292
+ Compiling adder v0.1.0 (file:///private/tmp/adder)
293
+ Finished debug [unoptimized + debuginfo] target(s) in 0.20 secs
294
+ Running target/debug/deps/adder-941f01916ca4a642
268
295
269
296
running 2 tests
270
- test expensive_test ... ignored
271
- test it_works ... ok
297
+ test tests:: expensive_test ... ignored
298
+ test tests:: it_works ... ok
272
299
273
300
test result: ok. 1 passed; 0 failed; 1 ignored; 0 measured
274
301
@@ -283,10 +310,11 @@ The expensive tests can be run explicitly using `cargo test -- --ignored`:
283
310
284
311
``` bash
285
312
$ cargo test -- --ignored
286
- Running target/debug/deps/adder-91b3e234d4ed382a
313
+ Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
314
+ Running target/debug/deps/adder-941f01916ca4a642
287
315
288
316
running 1 test
289
- test expensive_test ... ok
317
+ test tests:: expensive_test ... ok
290
318
291
319
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
292
320
@@ -310,7 +338,6 @@ was missing from our last example. Let's explain what this does.
310
338
The idiomatic way of writing our example looks like this:
311
339
312
340
``` rust,ignore
313
- # fn main() {}
314
341
pub fn add_two(a: i32) -> i32 {
315
342
a + 2
316
343
}
@@ -339,7 +366,6 @@ a large module, and so this is a common use of globs. Let's change our
339
366
` src/lib.rs ` to make use of it:
340
367
341
368
``` rust,ignore
342
- # fn main() {}
343
369
pub fn add_two(a: i32) -> i32 {
344
370
a + 2
345
371
}
0 commit comments