@@ -23,7 +23,11 @@ $ cd adder
23
23
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
- ``` 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
+ #
27
31
#[cfg(test)]
28
32
mod tests {
29
33
#[test]
@@ -32,17 +36,30 @@ mod tests {
32
36
}
33
37
```
34
38
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
+
35
51
Note the ` #[test] ` . This attribute indicates that this is a test function. It
36
52
currently has no body. That's good enough to pass! We can run the tests with
37
53
` cargo test ` :
38
54
39
55
``` bash
40
56
$ cargo test
41
57
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
43
60
44
61
running 1 test
45
- test tests:: it_works ... ok
62
+ test it_works ... ok
46
63
47
64
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
48
65
@@ -58,13 +75,15 @@ for the test we wrote, and another for documentation tests. We'll talk about
58
75
those later. For now, see this line:
59
76
60
77
``` text
61
- test tests:: it_works ... ok
78
+ test it_works ... ok
62
79
```
63
80
64
81
Note the ` it_works ` . This comes from the name of our function:
65
82
66
83
``` rust
84
+ # fn main () {
67
85
fn it_works () {
86
+ }
68
87
# }
69
88
```
70
89
@@ -77,8 +96,11 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
77
96
So why does our do-nothing test pass? Any test which doesn't ` panic! ` passes,
78
97
and any test that does ` panic! ` fails. Let's make our test fail:
79
98
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
+ #
82
104
#[test]
83
105
fn it_works() {
84
106
assert!(false);
@@ -92,19 +114,21 @@ run our tests again:
92
114
``` bash
93
115
$ cargo test
94
116
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
96
119
97
120
running 1 test
98
- test tests:: it_works ... FAILED
121
+ test it_works ... FAILED
99
122
100
123
failures:
101
124
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.
104
128
105
129
106
130
failures:
107
- tests:: it_works
131
+ it_works
108
132
109
133
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured
110
134
@@ -114,7 +138,7 @@ error: test failed
114
138
Rust indicates that our test failed:
115
139
116
140
``` text
117
- test tests:: it_works ... FAILED
141
+ test it_works ... FAILED
118
142
```
119
143
120
144
And that's reflected in the summary line:
@@ -147,8 +171,11 @@ This is useful if you want to integrate `cargo test` into other tooling.
147
171
148
172
We can invert our test's failure with another attribute: ` should_panic ` :
149
173
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
+ #
152
179
#[test]
153
180
#[should_panic]
154
181
fn it_works() {
@@ -161,10 +188,11 @@ This test will now succeed if we `panic!` and fail if we complete. Let's try it:
161
188
``` bash
162
189
$ cargo test
163
190
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
165
193
166
194
running 1 test
167
- test tests:: it_works ... ok
195
+ test it_works ... ok
168
196
169
197
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
170
198
@@ -178,8 +206,11 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
178
206
Rust provides another macro, ` assert_eq! ` , that compares two arguments for
179
207
equality:
180
208
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
+ #
183
214
#[test]
184
215
#[should_panic]
185
216
fn it_works() {
@@ -193,10 +224,11 @@ passes:
193
224
``` bash
194
225
$ cargo test
195
226
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
197
229
198
230
running 1 test
199
- test tests:: it_works ... ok
231
+ test it_works ... ok
200
232
201
233
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
202
234
@@ -213,8 +245,11 @@ parameter can be added to the `should_panic` attribute. The test harness will
213
245
make sure that the failure message contains the provided text. A safer version
214
246
of the example above would be:
215
247
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
+ #
218
253
#[test]
219
254
#[should_panic(expected = "assertion failed")]
220
255
fn it_works() {
@@ -225,7 +260,10 @@ fn it_works() {
225
260
That's all there is to the basics! Let's write one 'real' test:
226
261
227
262
``` 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
+ #
229
267
pub fn add_two(a: i32) -> i32 {
230
268
a + 2
231
269
}
@@ -244,8 +282,15 @@ some known arguments and compare it to the expected output.
244
282
Sometimes a few specific tests can be very time-consuming to execute. These
245
283
can be disabled by default by using the ` ignore ` attribute:
246
284
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
+
249
294
#[test]
250
295
fn it_works() {
251
296
assert_eq!(4, add_two(2));
264
309
``` bash
265
310
$ cargo test
266
311
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
268
314
269
315
running 2 tests
270
316
test expensive_test ... ignored
@@ -283,7 +329,8 @@ The expensive tests can be run explicitly using `cargo test -- --ignored`:
283
329
284
330
``` bash
285
331
$ 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
287
334
288
335
running 1 test
289
336
test expensive_test ... ok
@@ -310,7 +357,10 @@ was missing from our last example. Let's explain what this does.
310
357
The idiomatic way of writing our example looks like this:
311
358
312
359
``` 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
+ #
314
364
pub fn add_two(a: i32) -> i32 {
315
365
a + 2
316
366
}
@@ -339,7 +389,10 @@ a large module, and so this is a common use of globs. Let's change our
339
389
` src/lib.rs ` to make use of it:
340
390
341
391
``` 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
+ #
343
396
pub fn add_two(a: i32) -> i32 {
344
397
a + 2
345
398
}
@@ -389,9 +442,14 @@ To write an integration test, let's make a `tests` directory and
389
442
put a ` tests/integration_test.rs ` file inside with this as its contents:
390
443
391
444
``` 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.
392
451
extern crate adder;
393
452
394
- # fn main() {}
395
453
#[test]
396
454
fn it_works() {
397
455
assert_eq!(4, adder::add_two(2));
@@ -452,7 +510,10 @@ running examples in your documentation (**note:** this only works in library
452
510
crates, not binary crates). Here's a fleshed-out ` src/lib.rs ` with examples:
453
511
454
512
``` 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
+ #
456
517
//! The `adder` crate provides functions that add numbers to other numbers.
457
518
//!
458
519
//! # Examples
0 commit comments