@@ -43,7 +43,7 @@ $ cd adder
43
43
The contents of the * src/lib.rs* file in your ` adder ` library should look like
44
44
Listing 11-1.
45
45
46
- <span class = " filename " >Filename: src/lib.rs</ span >
46
+ <Listing number = " 11-1 " file-name = " src/lib.rs " caption = " The test module and function generated automatically by `cargo new` " >
47
47
48
48
<!-- manual-regeneration
49
49
cd listings/ch11-writing-automated-tests
@@ -59,8 +59,7 @@ cd ../../..
59
59
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-01/src/lib.rs}}
60
60
```
61
61
62
- <span class =" caption " >Listing 11-1: The test module and function generated
63
- automatically by ` cargo new ` </span >
62
+ </Listing >
64
63
65
64
For now, let’s focus solely on the ` it_works() ` function. Note the
66
65
` #[test] ` annotation: this attribute indicates this is a test function, so the
@@ -76,12 +75,13 @@ passes.
76
75
The ` cargo test ` command runs all tests in our project, as shown in Listing
77
76
11-2.
78
77
78
+ <Listing number =" 11-2 " caption =" The output from running the automatically generated test " >
79
+
79
80
``` console
80
81
{{#include ../listings/ch11-writing-automated-tests/listing-11-01/output.txt}}
81
82
```
82
83
83
- <span class =" caption " >Listing 11-2: The output from running the automatically
84
- generated test</span >
84
+ </Listing >
85
85
86
86
Cargo compiled and ran the test. We see the line ` running 1 test ` . The next
87
87
line shows the name of the generated test function, called ` it_works ` , and that
@@ -113,12 +113,14 @@ ignore the `Doc-tests` output.
113
113
Let’s start to customize the test to our own needs. First change the name of
114
114
the ` it_works ` function to a different name, such as ` exploration ` , like so:
115
115
116
- <span class = " filename " >Filename: src/lib.rs</ span >
116
+ <Listing file-name = " src/lib.rs " >
117
117
118
118
``` rust,noplayground
119
119
{{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/src/lib.rs}}
120
120
```
121
121
122
+ </Listing >
123
+
122
124
Then run ` cargo test ` again. The output now shows ` exploration ` instead of
123
125
` it_works ` :
124
126
@@ -133,24 +135,24 @@ marked as failed. In Chapter 9, we talked about how the simplest way to panic
133
135
is to call the ` panic! ` macro. Enter the new test as a function named
134
136
` another ` , so your * src/lib.rs* file looks like Listing 11-3.
135
137
136
- <span class = " filename " >Filename: src/lib .rs</ span >
138
+ <Listing number = " 11-3 " file-name = " src/libs .rs " caption = " Adding a second test that will fail because we call the `panic!` macro " >
137
139
138
140
``` rust,panics,noplayground
139
141
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-03/src/lib.rs:here}}
140
142
```
141
143
142
- <span class =" caption " >Listing 11-3: Adding a second test that will fail because
143
- we call the ` panic! ` macro</span >
144
+ </Listing >
144
145
145
146
Run the tests again using ` cargo test ` . The output should look like Listing
146
147
11-4, which shows that our ` exploration ` test passed and ` another ` failed.
147
148
149
+ <Listing number =" 11-4 " caption =" Test results when one test passes and one test fails " >
150
+
148
151
``` console
149
152
{{#include ../listings/ch11-writing-automated-tests/listing-11-03/output.txt}}
150
153
```
151
154
152
- <span class =" caption " >Listing 11-4: Test results when one test passes and one
153
- test fails</span >
155
+ </Listing >
154
156
155
157
Instead of ` ok ` , the line ` test tests::another ` shows ` FAILED ` . Two new
156
158
sections appear between the individual results and the summary: the first
@@ -182,29 +184,27 @@ In Chapter 5, Listing 5-15, we used a `Rectangle` struct and a `can_hold`
182
184
method, which are repeated here in Listing 11-5. Let’s put this code in the
183
185
* src/lib.rs* file, then write some tests for it using the ` assert! ` macro.
184
186
185
- <span class = " filename " >Filename: src/lib.rs</ span >
187
+ <Listing number = " 11-5 " file-name = " src/lib.rs " caption = " The `Rectangle` struct and its `can_hold` method from Chapter 5 " >
186
188
187
189
``` rust,noplayground
188
190
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-05/src/lib.rs:here}}
189
191
```
190
192
191
- <span class =" caption " >Listing 11-5: Using the ` Rectangle ` struct and its
192
- ` can_hold ` method from Chapter 5</span >
193
+ </Listing >
193
194
194
195
The ` can_hold ` method returns a Boolean, which means it’s a perfect use case
195
196
for the ` assert! ` macro. In Listing 11-6, we write a test that exercises the
196
197
` can_hold ` method by creating a ` Rectangle ` instance that has a width of 8 and
197
198
a height of 7 and asserting that it can hold another ` Rectangle ` instance that
198
199
has a width of 5 and a height of 1.
199
200
200
- <span class = " filename " >Filename: src/lib.rs</ span >
201
+ <Listing number = " 11-6 " file-name = " src/lib.rs " caption = " A test for `can_hold` that checks whether a larger rectangle can indeed hold a smaller rectangle " >
201
202
202
203
``` rust,noplayground
203
204
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-06/src/lib.rs:here}}
204
205
```
205
206
206
- <span class =" caption " >Listing 11-6: A test for ` can_hold ` that checks whether a
207
- larger rectangle can indeed hold a smaller rectangle</span >
207
+ </Listing >
208
208
209
209
Note that we’ve added a new line inside the ` tests ` module: ` use super::*; ` .
210
210
The ` tests ` module is a regular module that follows the usual visibility rules
@@ -227,12 +227,14 @@ supposed to return `true`, so our test should pass. Let’s find out!
227
227
It does pass! Let’s add another test, this time asserting that a smaller
228
228
rectangle cannot hold a larger rectangle:
229
229
230
- <span class = " filename " >Filename: src/lib.rs</ span >
230
+ <Listing file-name = " src/lib.rs " >
231
231
232
232
``` rust,noplayground
233
233
{{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-02-adding-another-rectangle-test/src/lib.rs:here}}
234
234
```
235
235
236
+ </Listing >
237
+
236
238
Because the correct result of the ` can_hold ` function in this case is ` false ` ,
237
239
we need to negate that result before we pass it to the ` assert! ` macro. As a
238
240
result, our test will pass if ` can_hold ` returns ` false ` :
@@ -276,14 +278,13 @@ expression, without printing the values that led to the `false` value.
276
278
In Listing 11-7, we write a function named ` add_two ` that adds ` 2 ` to its
277
279
parameter, then we test this function using the ` assert_eq! ` macro.
278
280
279
- <span class = " filename " >Filename: src/lib.rs</ span >
281
+ <Listing number = " 11-7 " file-name = " src/lib.rs " caption = " Testing the function `add_two` using the `assert_eq!` macro " >
280
282
281
283
``` rust,noplayground
282
284
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-07/src/lib.rs}}
283
285
```
284
286
285
- <span class =" caption " >Listing 11-7: Testing the function ` add_two ` using the
286
- ` assert_eq! ` macro</span >
287
+ </Listing >
287
288
288
289
Let’s check that it passes!
289
290
@@ -360,12 +361,14 @@ the problem is with the code.
360
361
For example, let’s say we have a function that greets people by name and we
361
362
want to test that the name we pass into the function appears in the output:
362
363
363
- <span class = " filename " >Filename: src/lib.rs</ span >
364
+ <Listing file-name = " src/lib.rs " >
364
365
365
366
``` rust,noplayground
366
367
{{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-05-greeter/src/lib.rs}}
367
368
```
368
369
370
+ </Listing >
371
+
369
372
The requirements for this program haven’t been agreed upon yet, and we’re
370
373
pretty sure the ` Hello ` text at the beginning of the greeting will change. We
371
374
decided we don’t want to have to update the test when the requirements change,
@@ -421,14 +424,13 @@ inside the function doesn’t panic.
421
424
Listing 11-8 shows a test that checks that the error conditions of ` Guess::new `
422
425
happen when we expect them to.
423
426
424
- <span class = " filename " >Filename: src/lib.rs</ span >
427
+ <Listing number = " 11-8 " file-name = " src/lib.rs " caption = " Testing that a condition will cause a `panic!` " >
425
428
426
429
``` rust,noplayground
427
430
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-08/src/lib.rs}}
428
431
```
429
432
430
- <span class =" caption " >Listing 11-8: Testing that a condition will cause a
431
- ` panic! ` </span >
433
+ </Listing >
432
434
433
435
We place the ` #[should_panic] ` attribute after the ` #[test] ` attribute and
434
436
before the test function it applies to. Let’s look at the result when this test
@@ -464,14 +466,13 @@ consider the modified code for `Guess` in Listing 11-9 where the `new` function
464
466
panics with different messages depending on whether the value is too small or
465
467
too large.
466
468
467
- <span class = " filename " >Filename: src/lib.rs</ span >
469
+ <Listing number = " 11-9 " caption = " Testing for a `panic!` with a panic message containing a specified substring " >
468
470
469
471
``` rust,noplayground
470
472
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-09/src/lib.rs:here}}
471
473
```
472
474
473
- <span class =" caption " >Listing 11-9: Testing for a ` panic! ` with a panic message
474
- containing a specified substring</span >
475
+ </Listing >
475
476
476
477
This test will pass because the value we put in the ` should_panic ` attribute’s
477
478
` expected ` parameter is a substring of the message that the ` Guess::new `
0 commit comments