@@ -24,9 +24,11 @@ Cargo will automatically generate a simple test when you make a new project.
24
24
Here's the contents of ` src/lib.rs ` :
25
25
26
26
``` rust
27
- # fn main () {}
28
- #[test]
29
- fn it_works () {
27
+ #[cfg(test)]
28
+ mod tests {
29
+ #[test]
30
+ fn it_works () {
31
+ }
30
32
}
31
33
```
32
34
@@ -36,11 +38,11 @@ currently has no body. That's good enough to pass! We can run the tests with
36
38
37
39
``` bash
38
40
$ cargo test
39
- Compiling adder v0.0.1 (file:///home/you/projects/adder)
40
- Running target/adder-91b3e234d4ed382a
41
+ Compiling adder v0.1.0 (file:///home/you/projects/adder)
42
+ Running target/debug/deps/ adder-91b3e234d4ed382a
41
43
42
44
running 1 test
43
- test it_works ... ok
45
+ test tests:: it_works ... ok
44
46
45
47
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
46
48
@@ -56,7 +58,7 @@ for the test we wrote, and another for documentation tests. We'll talk about
56
58
those later. For now, see this line:
57
59
58
60
``` text
59
- test it_works ... ok
61
+ test tests:: it_works ... ok
60
62
```
61
63
62
64
Note the ` it_works ` . This comes from the name of our function:
@@ -89,31 +91,30 @@ run our tests again:
89
91
90
92
``` bash
91
93
$ cargo test
92
- Compiling adder v0.0.1 (file:///home/you/projects/adder)
93
- Running target/adder-91b3e234d4ed382a
94
+ Compiling adder v0.1.0 (file:///home/you/projects/adder)
95
+ Running target/debug/deps/ adder-91b3e234d4ed382a
94
96
95
97
running 1 test
96
- test it_works ... FAILED
98
+ test tests:: it_works ... FAILED
97
99
98
100
failures:
99
101
100
- ---- it_works stdout ----
101
- thread ' it_works' panicked at ' assertion failed: false' , /home/steve/tmp/adder/src/lib.rs:3
102
-
102
+ ---- test::it_works stdout ----
103
+ thread ' tests::it_works' panicked at ' assertion failed: false' , src/lib.rs:5
103
104
104
105
105
106
failures:
106
- it_works
107
+ tests:: it_works
107
108
108
109
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured
109
110
110
- thread ' main ' panicked at ' Some tests failed' , /home/steve/src/rust/src/libtest/lib.rs:247
111
+ error: test failed
111
112
```
112
113
113
114
Rust indicates that our test failed:
114
115
115
116
``` text
116
- test it_works ... FAILED
117
+ test tests:: it_works ... FAILED
117
118
```
118
119
119
120
And that's reflected in the summary line:
@@ -159,11 +160,11 @@ This test will now succeed if we `panic!` and fail if we complete. Let's try it:
159
160
160
161
``` bash
161
162
$ cargo test
162
- Compiling adder v0.0.1 (file:///home/you/projects/adder)
163
- Running target/adder-91b3e234d4ed382a
163
+ Compiling adder v0.1.0 (file:///home/you/projects/adder)
164
+ Running target/debug/deps/ adder-91b3e234d4ed382a
164
165
165
166
running 1 test
166
- test it_works ... ok
167
+ test tests:: it_works ... ok
167
168
168
169
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
169
170
@@ -191,11 +192,11 @@ passes:
191
192
192
193
``` bash
193
194
$ cargo test
194
- Compiling adder v0.0.1 (file:///home/you/projects/adder)
195
- Running target/adder-91b3e234d4ed382a
195
+ Compiling adder v0.1.0 (file:///home/you/projects/adder)
196
+ Running target/debug/deps/ adder-91b3e234d4ed382a
196
197
197
198
running 1 test
198
- test it_works ... ok
199
+ test tests:: it_works ... ok
199
200
200
201
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
201
202
262
263
263
264
``` bash
264
265
$ cargo test
265
- Compiling adder v0.0.1 (file:///home/you/projects/adder)
266
- Running target/adder-91b3e234d4ed382a
266
+ Compiling adder v0.1.0 (file:///home/you/projects/adder)
267
+ Running target/debug/deps/ adder-91b3e234d4ed382a
267
268
268
269
running 2 tests
269
270
test expensive_test ... ignored
@@ -282,7 +283,7 @@ The expensive tests can be run explicitly using `cargo test -- --ignored`:
282
283
283
284
``` bash
284
285
$ cargo test -- --ignored
285
- Running target/adder-91b3e234d4ed382a
286
+ Running target/debug/deps/ adder-91b3e234d4ed382a
286
287
287
288
running 1 test
288
289
test expensive_test ... ok
@@ -302,8 +303,11 @@ which is why the command is `cargo test -- --ignored`.
302
303
# The ` tests ` module
303
304
304
305
There is one way in which our existing example is not idiomatic: it's
305
- missing the ` tests ` module. The idiomatic way of writing our example
306
- looks like this:
306
+ missing the ` tests ` module. You might have noticed this test module was
307
+ present in the code that was initially generated with ` cargo new ` but
308
+ was missing from our last example. Let's explain what this does.
309
+
310
+ The idiomatic way of writing our example looks like this:
307
311
308
312
``` rust,ignore
309
313
# fn main() {}
@@ -356,8 +360,8 @@ Note the different `use` line. Now we run our tests:
356
360
``` bash
357
361
$ cargo test
358
362
Updating registry ` https://github.com/rust-lang/crates.io-index`
359
- Compiling adder v0.0.1 (file:///home/you/projects/adder)
360
- Running target/adder-91b3e234d4ed382a
363
+ Compiling adder v0.1.0 (file:///home/you/projects/adder)
364
+ Running target/debug/deps/ adder-91b3e234d4ed382a
361
365
362
366
running 1 test
363
367
test tests::it_works ... ok
@@ -404,15 +408,15 @@ Let's run them:
404
408
405
409
``` bash
406
410
$ cargo test
407
- Compiling adder v0.0.1 (file:///home/you/projects/adder)
408
- Running target/adder-91b3e234d4ed382a
411
+ Compiling adder v0.1.0 (file:///home/you/projects/adder)
412
+ Running target/debug/deps/ adder-91b3e234d4ed382a
409
413
410
414
running 1 test
411
415
test tests::it_works ... ok
412
416
413
417
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
414
418
415
- Running target/lib-c18e7d3494509e74
419
+ Running target/debug/integration_test-68064b69521c828a
416
420
417
421
running 1 test
418
422
test it_works ... ok
@@ -490,15 +494,15 @@ Let's run the tests again:
490
494
491
495
``` bash
492
496
$ cargo test
493
- Compiling adder v0.0.1 (file:///home/steve/tmp /adder)
494
- Running target/adder-91b3e234d4ed382a
497
+ Compiling adder v0.1.0. (file:///home/you/projects /adder)
498
+ Running target/debug/deps/ adder-91b3e234d4ed382a
495
499
496
500
running 1 test
497
501
test tests::it_works ... ok
498
502
499
503
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
500
504
501
- Running target/lib-c18e7d3494509e74
505
+ Running target/debug/integration_test-68064b69521c828a
502
506
503
507
running 1 test
504
508
test it_works ... ok
0 commit comments