@@ -24,9 +24,11 @@ Cargo will automatically generate a simple test when you make a new project.
2424Here's the contents of ` src/lib.rs ` :
2525
2626``` rust
27- # fn main () {}
28- #[test]
29- fn it_works () {
27+ #[cfg(test)]
28+ mod tests {
29+ #[test]
30+ fn it_works () {
31+ }
3032}
3133```
3234
@@ -36,11 +38,11 @@ currently has no body. That's good enough to pass! We can run the tests with
3638
3739``` bash
3840$ 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
4143
4244running 1 test
43- test it_works ... ok
45+ test tests:: it_works ... ok
4446
4547test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
4648
@@ -56,7 +58,7 @@ for the test we wrote, and another for documentation tests. We'll talk about
5658those later. For now, see this line:
5759
5860``` text
59- test it_works ... ok
61+ test tests:: it_works ... ok
6062```
6163
6264Note the ` it_works ` . This comes from the name of our function:
@@ -89,31 +91,30 @@ run our tests again:
8991
9092``` bash
9193$ 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
9496
9597running 1 test
96- test it_works ... FAILED
98+ test tests:: it_works ... FAILED
9799
98100failures:
99101
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
103104
104105
105106failures:
106- it_works
107+ tests:: it_works
107108
108109test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured
109110
110- thread ' main ' panicked at ' Some tests failed' , /home/steve/src/rust/src/libtest/lib.rs:247
111+ error: test failed
111112```
112113
113114Rust indicates that our test failed:
114115
115116``` text
116- test it_works ... FAILED
117+ test tests:: it_works ... FAILED
117118```
118119
119120And 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:
159160
160161``` bash
161162$ 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
164165
165166running 1 test
166- test it_works ... ok
167+ test tests:: it_works ... ok
167168
168169test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
169170
@@ -191,11 +192,11 @@ passes:
191192
192193``` bash
193194$ 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
196197
197198running 1 test
198- test it_works ... ok
199+ test tests:: it_works ... ok
199200
200201test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
201202
262263
263264``` bash
264265$ 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
267268
268269running 2 tests
269270test expensive_test ... ignored
@@ -282,7 +283,7 @@ The expensive tests can be run explicitly using `cargo test -- --ignored`:
282283
283284``` bash
284285$ cargo test -- --ignored
285- Running target/adder-91b3e234d4ed382a
286+ Running target/debug/deps/ adder-91b3e234d4ed382a
286287
287288running 1 test
288289test expensive_test ... ok
@@ -302,8 +303,11 @@ which is why the command is `cargo test -- --ignored`.
302303# The ` tests ` module
303304
304305There 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:
307311
308312``` rust,ignore
309313# fn main() {}
@@ -356,8 +360,8 @@ Note the different `use` line. Now we run our tests:
356360``` bash
357361$ cargo test
358362 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
361365
362366running 1 test
363367test tests::it_works ... ok
@@ -404,15 +408,15 @@ Let's run them:
404408
405409``` bash
406410$ 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
409413
410414running 1 test
411415test tests::it_works ... ok
412416
413417test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
414418
415- Running target/lib-c18e7d3494509e74
419+ Running target/debug/integration_test-68064b69521c828a
416420
417421running 1 test
418422test it_works ... ok
@@ -490,15 +494,15 @@ Let's run the tests again:
490494
491495``` bash
492496$ 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
495499
496500running 1 test
497501test tests::it_works ... ok
498502
499503test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
500504
501- Running target/lib-c18e7d3494509e74
505+ Running target/debug/integration_test-68064b69521c828a
502506
503507running 1 test
504508test it_works ... ok
0 commit comments