@@ -23,7 +23,11 @@ $ cd adder
2323Cargo will automatically generate a simple test when you make a new project.
2424Here's the contents of ` src/lib.rs ` :
2525
26- ``` rust
26+ ``` rust,ignore
27+ # // The next line exists to trick play.rust-lang.org into running our code as a
28+ # // test:
29+ # // fn main
30+ #
2731#[cfg(test)]
2832mod tests {
2933 #[test]
@@ -32,17 +36,30 @@ mod tests {
3236}
3337```
3438
39+ For now, let's remove the ` mod ` bit, and focus on just the function:
40+
41+ ``` rust,ignore
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+
3551Note the ` #[test] ` . This attribute indicates that this is a test function. It
3652currently has no body. That's good enough to pass! We can run the tests with
3753` cargo test ` :
3854
3955``` bash
4056$ cargo test
4157 Compiling adder v0.1.0 (file:///home/you/projects/adder)
42- Running target/debug/deps/adder-91b3e234d4ed382a
58+ Finished debug [unoptimized + debuginfo] target(s) in 0.15 secs
59+ Running target/debug/deps/adder-941f01916ca4a642
4360
4461running 1 test
45- test tests:: it_works ... ok
62+ test it_works ... ok
4663
4764test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
4865
@@ -58,13 +75,15 @@ for the test we wrote, and another for documentation tests. We'll talk about
5875those later. For now, see this line:
5976
6077``` text
61- test tests:: it_works ... ok
78+ test it_works ... ok
6279```
6380
6481Note the ` it_works ` . This comes from the name of our function:
6582
6683``` rust
84+ # fn main () {
6785fn it_works () {
86+ }
6887# }
6988```
7089
@@ -77,8 +96,11 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
7796So why does our do-nothing test pass? Any test which doesn't ` panic! ` passes,
7897and any test that does ` panic! ` fails. Let's make our test fail:
7998
80- ``` rust
81- # fn main () {}
99+ ``` rust,ignore
100+ # // The next line exists to trick play.rust-lang.org into running our code as a
101+ # // test:
102+ # // fn main
103+ #
82104#[test]
83105fn it_works() {
84106 assert!(false);
@@ -92,19 +114,21 @@ run our tests again:
92114``` bash
93115$ cargo test
94116 Compiling adder v0.1.0 (file:///home/you/projects/adder)
95- Running target/debug/deps/adder-91b3e234d4ed382a
117+ Finished debug [unoptimized + debuginfo] target(s) in 0.17 secs
118+ Running target/debug/deps/adder-941f01916ca4a642
96119
97120running 1 test
98- test tests:: it_works ... FAILED
121+ test it_works ... FAILED
99122
100123failures:
101124
102- ---- test::it_works stdout ----
103- thread ' tests::it_works' panicked at ' assertion failed: false' , src/lib.rs:5
125+ ---- it_works stdout ----
126+ thread ' it_works' panicked at ' assertion failed: false' , src/lib.rs:5
127+ note: Run with ` RUST_BACKTRACE=1` for a backtrace.
104128
105129
106130failures:
107- tests:: it_works
131+ it_works
108132
109133test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured
110134
@@ -114,7 +138,7 @@ error: test failed
114138Rust indicates that our test failed:
115139
116140``` text
117- test tests:: it_works ... FAILED
141+ test it_works ... FAILED
118142```
119143
120144And that's reflected in the summary line:
@@ -147,8 +171,11 @@ This is useful if you want to integrate `cargo test` into other tooling.
147171
148172We can invert our test's failure with another attribute: ` should_panic ` :
149173
150- ``` rust
151- # fn main () {}
174+ ``` rust,ignore
175+ # // The next line exists to trick play.rust-lang.org into running our code as a
176+ # // test:
177+ # // fn main
178+ #
152179#[test]
153180#[should_panic]
154181fn it_works() {
@@ -161,10 +188,11 @@ This test will now succeed if we `panic!` and fail if we complete. Let's try it:
161188``` bash
162189$ cargo test
163190 Compiling adder v0.1.0 (file:///home/you/projects/adder)
164- Running target/debug/deps/adder-91b3e234d4ed382a
191+ Finished debug [unoptimized + debuginfo] target(s) in 0.17 secs
192+ Running target/debug/deps/adder-941f01916ca4a642
165193
166194running 1 test
167- test tests:: it_works ... ok
195+ test it_works ... ok
168196
169197test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
170198
@@ -178,8 +206,11 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
178206Rust provides another macro, ` assert_eq! ` , that compares two arguments for
179207equality:
180208
181- ``` rust
182- # fn main () {}
209+ ``` rust,ignore
210+ # // The next line exists to trick play.rust-lang.org into running our code as a
211+ # // test:
212+ # // fn main
213+ #
183214#[test]
184215#[should_panic]
185216fn it_works() {
@@ -193,10 +224,11 @@ passes:
193224``` bash
194225$ cargo test
195226 Compiling adder v0.1.0 (file:///home/you/projects/adder)
196- Running target/debug/deps/adder-91b3e234d4ed382a
227+ Finished debug [unoptimized + debuginfo] target(s) in 0.21 secs
228+ Running target/debug/deps/adder-941f01916ca4a642
197229
198230running 1 test
199- test tests:: it_works ... ok
231+ test it_works ... ok
200232
201233test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
202234
@@ -213,8 +245,11 @@ parameter can be added to the `should_panic` attribute. The test harness will
213245make sure that the failure message contains the provided text. A safer version
214246of the example above would be:
215247
216- ``` rust
217- # fn main () {}
248+ ``` rust,ignore
249+ # // The next line exists to trick play.rust-lang.org into running our code as a
250+ # // test:
251+ # // fn main
252+ #
218253#[test]
219254#[should_panic(expected = "assertion failed")]
220255fn it_works() {
@@ -225,7 +260,10 @@ fn it_works() {
225260That's all there is to the basics! Let's write one 'real' test:
226261
227262``` rust,ignore
228- # fn main() {}
263+ # // The next line exists to trick play.rust-lang.org into running our code as a
264+ # // test:
265+ # // fn main
266+ #
229267pub fn add_two(a: i32) -> i32 {
230268 a + 2
231269}
@@ -244,8 +282,15 @@ some known arguments and compare it to the expected output.
244282Sometimes a few specific tests can be very time-consuming to execute. These
245283can be disabled by default by using the ` ignore ` attribute:
246284
247- ``` rust
248- # fn main () {}
285+ ``` rust,ignore
286+ # // The next line exists to trick play.rust-lang.org into running our code as a
287+ # // test:
288+ # // fn main
289+ #
290+ pub fn add_two(a: i32) -> i32 {
291+ a + 2
292+ }
293+
249294#[test]
250295fn it_works() {
251296 assert_eq!(4, add_two(2));
264309``` bash
265310$ cargo test
266311 Compiling adder v0.1.0 (file:///home/you/projects/adder)
267- Running target/debug/deps/adder-91b3e234d4ed382a
312+ Finished debug [unoptimized + debuginfo] target(s) in 0.20 secs
313+ Running target/debug/deps/adder-941f01916ca4a642
268314
269315running 2 tests
270316test expensive_test ... ignored
@@ -283,7 +329,8 @@ The expensive tests can be run explicitly using `cargo test -- --ignored`:
283329
284330``` bash
285331$ cargo test -- --ignored
286- Running target/debug/deps/adder-91b3e234d4ed382a
332+ Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
333+ Running target/debug/deps/adder-941f01916ca4a642
287334
288335running 1 test
289336test expensive_test ... ok
@@ -310,7 +357,10 @@ was missing from our last example. Let's explain what this does.
310357The idiomatic way of writing our example looks like this:
311358
312359``` rust,ignore
313- # fn main() {}
360+ # // The next line exists to trick play.rust-lang.org into running our code as a
361+ # // test:
362+ # // fn main
363+ #
314364pub fn add_two(a: i32) -> i32 {
315365 a + 2
316366}
@@ -339,7 +389,10 @@ a large module, and so this is a common use of globs. Let's change our
339389` src/lib.rs ` to make use of it:
340390
341391``` rust,ignore
342- # fn main() {}
392+ # // The next line exists to trick play.rust-lang.org into running our code as a
393+ # // test:
394+ # // fn main
395+ #
343396pub fn add_two(a: i32) -> i32 {
344397 a + 2
345398}
@@ -389,9 +442,14 @@ To write an integration test, let's make a `tests` directory and
389442put a ` tests/integration_test.rs ` file inside with this as its contents:
390443
391444``` rust,ignore
445+ # // The next line exists to trick play.rust-lang.org into running our code as a
446+ # // test:
447+ # // fn main
448+ #
449+ # // Sadly, this code will not work in play.rust-lang.org, because we have no
450+ # // crate adder to import. You'll need to try this part on your own machine.
392451extern crate adder;
393452
394- # fn main() {}
395453#[test]
396454fn it_works() {
397455 assert_eq!(4, adder::add_two(2));
@@ -452,7 +510,10 @@ running examples in your documentation (**note:** this only works in library
452510crates, not binary crates). Here's a fleshed-out ` src/lib.rs ` with examples:
453511
454512``` rust,ignore
455- # fn main() {}
513+ # // The next line exists to trick play.rust-lang.org into running our code as a
514+ # // test:
515+ # // fn main
516+ #
456517//! The `adder` crate provides functions that add numbers to other numbers.
457518//!
458519//! # Examples
0 commit comments