]> git.proxmox.com Git - rustc.git/blob - src/doc/book/src/ch11-01-writing-tests.md
New upstream version 1.52.0~beta.3+dfsg1
[rustc.git] / src / doc / book / src / ch11-01-writing-tests.md
1 ## How to Write Tests
2
3 Tests are Rust functions that verify that the non-test code is functioning in
4 the expected manner. The bodies of test functions typically perform these three
5 actions:
6
7 1. Set up any needed data or state.
8 2. Run the code you want to test.
9 3. Assert the results are what you expect.
10
11 Let’s look at the features Rust provides specifically for writing tests that
12 take these actions, which include the `test` attribute, a few macros, and the
13 `should_panic` attribute.
14
15 ### The Anatomy of a Test Function
16
17 At its simplest, a test in Rust is a function that’s annotated with the `test`
18 attribute. Attributes are metadata about pieces of Rust code; one example is
19 the `derive` attribute we used with structs in Chapter 5. To change a function
20 into a test function, add `#[test]` on the line before `fn`. When you run your
21 tests with the `cargo test` command, Rust builds a test runner binary that runs
22 the functions annotated with the `test` attribute and reports on whether each
23 test function passes or fails.
24
25 When we make a new library project with Cargo, a test module with a test
26 function in it is automatically generated for us. This module helps you start
27 writing your tests so you don’t have to look up the exact structure and syntax
28 of test functions every time you start a new project. You can add as many
29 additional test functions and as many test modules as you want!
30
31 We’ll explore some aspects of how tests work by experimenting with the template
32 test generated for us without actually testing any code. Then we’ll write some
33 real-world tests that call some code that we’ve written and assert that its
34 behavior is correct.
35
36 Let’s create a new library project called `adder`:
37
38 ```console
39 $ cargo new adder --lib
40 Created library `adder` project
41 $ cd adder
42 ```
43
44 The contents of the *src/lib.rs* file in your `adder` library should look like
45 Listing 11-1.
46
47 <span class="filename">Filename: src/lib.rs</span>
48
49 ```rust,noplayground
50 {{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-01/src/lib.rs}}
51 ```
52
53 <span class="caption">Listing 11-1: The test module and function generated
54 automatically by `cargo new`</span>
55
56 For now, let’s ignore the top two lines and focus on the function to see how it
57 works. Note the `#[test]` annotation before the `fn` line: this attribute
58 indicates this is a test function, so the test runner knows to treat this
59 function as a test. We could also have non-test functions in the `tests` module
60 to help set up common scenarios or perform common operations, so we need to
61 indicate which functions are tests by using the `#[test]` attribute.
62
63 The function body uses the `assert_eq!` macro to assert that 2 + 2 equals 4.
64 This assertion serves as an example of the format for a typical test. Let’s run
65 it to see that this test passes.
66
67 The `cargo test` command runs all tests in our project, as shown in Listing
68 11-2.
69
70 ```console
71 {{#include ../listings/ch11-writing-automated-tests/listing-11-01/output.txt}}
72 ```
73
74 <span class="caption">Listing 11-2: The output from running the automatically
75 generated test</span>
76
77 Cargo compiled and ran the test. After the `Compiling`, `Finished`, and
78 `Running` lines is the line `running 1 test`. The next line shows the name
79 of the generated test function, called `it_works`, and the result of running
80 that test, `ok`. The overall summary of running the tests appears next. The
81 text `test result: ok.` means that all the tests passed, and the portion that
82 reads `1 passed; 0 failed` totals the number of tests that passed or failed.
83
84 Because we don’t have any tests we’ve marked as ignored, the summary shows `0
85 ignored`. We also haven’t filtered the tests being run, so the end of the
86 summary shows `0 filtered out`. We’ll talk about ignoring and filtering out
87 tests in the next section, [“Controlling How Tests Are
88 Run.”][controlling-how-tests-are-run]<!-- ignore -->
89
90 The `0 measured` statistic is for benchmark tests that measure performance.
91 Benchmark tests are, as of this writing, only available in nightly Rust. See
92 [the documentation about benchmark tests][bench] to learn more.
93
94 [bench]: ../unstable-book/library-features/test.html
95
96 The next part of the test output, which starts with `Doc-tests adder`, is for
97 the results of any documentation tests. We don’t have any documentation tests
98 yet, but Rust can compile any code examples that appear in our API
99 documentation. This feature helps us keep our docs and our code in sync! We’ll
100 discuss how to write documentation tests in the [“Documentation Comments as
101 Tests”][doc-comments]<!-- ignore --> section of Chapter 14. For now, we’ll
102 ignore the `Doc-tests` output.
103
104 Let’s change the name of our test to see how that changes the test output.
105 Change the `it_works` function to a different name, such as `exploration`, like
106 so:
107
108 <span class="filename">Filename: src/lib.rs</span>
109
110 ```rust,noplayground
111 {{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/src/lib.rs}}
112 ```
113
114 Then run `cargo test` again. The output now shows `exploration` instead of
115 `it_works`:
116
117 ```console
118 {{#include ../listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/output.txt}}
119 ```
120
121 Let’s add another test, but this time we’ll make a test that fails! Tests fail
122 when something in the test function panics. Each test is run in a new thread,
123 and when the main thread sees that a test thread has died, the test is marked
124 as failed. We talked about the simplest way to cause a panic in Chapter 9,
125 which is to call the `panic!` macro. Enter the new test, `another`, so your
126 *src/lib.rs* file looks like Listing 11-3.
127
128 <span class="filename">Filename: src/lib.rs</span>
129
130 ```rust,panics,noplayground
131 {{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-03/src/lib.rs:here}}
132 ```
133
134 <span class="caption">Listing 11-3: Adding a second test that will fail because
135 we call the `panic!` macro</span>
136
137 Run the tests again using `cargo test`. The output should look like Listing
138 11-4, which shows that our `exploration` test passed and `another` failed.
139
140 ```console
141 {{#include ../listings/ch11-writing-automated-tests/listing-11-03/output.txt}}
142 ```
143
144 <span class="caption">Listing 11-4: Test results when one test passes and one
145 test fails</span>
146
147 Instead of `ok`, the line `test tests::another` shows `FAILED`. Two new
148 sections appear between the individual results and the summary: the first
149 section displays the detailed reason for each test failure. In this case,
150 `another` failed because it `panicked at 'Make this test fail'`, which happened
151 on line 10 in the *src/lib.rs* file. The next section lists just the names of
152 all the failing tests, which is useful when there are lots of tests and lots of
153 detailed failing test output. We can use the name of a failing test to run just
154 that test to more easily debug it; we’ll talk more about ways to run tests in
155 the [“Controlling How Tests Are Run”][controlling-how-tests-are-run]<!-- ignore
156 --> section.
157
158 The summary line displays at the end: overall, our test result is `FAILED`.
159 We had one test pass and one test fail.
160
161 Now that you’ve seen what the test results look like in different scenarios,
162 let’s look at some macros other than `panic!` that are useful in tests.
163
164 ### Checking Results with the `assert!` Macro
165
166 The `assert!` macro, provided by the standard library, is useful when you want
167 to ensure that some condition in a test evaluates to `true`. We give the
168 `assert!` macro an argument that evaluates to a Boolean. If the value is
169 `true`, `assert!` does nothing and the test passes. If the value is `false`,
170 the `assert!` macro calls the `panic!` macro, which causes the test to fail.
171 Using the `assert!` macro helps us check that our code is functioning in the
172 way we intend.
173
174 In Chapter 5, Listing 5-15, we used a `Rectangle` struct and a `can_hold`
175 method, which are repeated here in Listing 11-5. Let’s put this code in the
176 *src/lib.rs* file and write some tests for it using the `assert!` macro.
177
178 <span class="filename">Filename: src/lib.rs</span>
179
180 ```rust,noplayground
181 {{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-05/src/lib.rs:here}}
182 ```
183
184 <span class="caption">Listing 11-5: Using the `Rectangle` struct and its
185 `can_hold` method from Chapter 5</span>
186
187 The `can_hold` method returns a Boolean, which means it’s a perfect use case
188 for the `assert!` macro. In Listing 11-6, we write a test that exercises the
189 `can_hold` method by creating a `Rectangle` instance that has a width of 8 and
190 a height of 7 and asserting that it can hold another `Rectangle` instance that
191 has a width of 5 and a height of 1.
192
193 <span class="filename">Filename: src/lib.rs</span>
194
195 ```rust,noplayground
196 {{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-06/src/lib.rs:here}}
197 ```
198
199 <span class="caption">Listing 11-6: A test for `can_hold` that checks whether a
200 larger rectangle can indeed hold a smaller rectangle</span>
201
202 Note that we’ve added a new line inside the `tests` module: `use super::*;`.
203 The `tests` module is a regular module that follows the usual visibility rules
204 we covered in Chapter 7 in the [“Paths for Referring to an Item in the Module
205 Tree”][paths-for-referring-to-an-item-in-the-module-tree]<!-- ignore -->
206 section. Because the `tests` module is an inner module, we need to bring the
207 code under test in the outer module into the scope of the inner module. We use
208 a glob here so anything we define in the outer module is available to this
209 `tests` module.
210
211 We’ve named our test `larger_can_hold_smaller`, and we’ve created the two
212 `Rectangle` instances that we need. Then we called the `assert!` macro and
213 passed it the result of calling `larger.can_hold(&smaller)`. This expression
214 is supposed to return `true`, so our test should pass. Let’s find out!
215
216 ```console
217 {{#include ../listings/ch11-writing-automated-tests/listing-11-06/output.txt}}
218 ```
219
220 It does pass! Let’s add another test, this time asserting that a smaller
221 rectangle cannot hold a larger rectangle:
222
223 <span class="filename">Filename: src/lib.rs</span>
224
225 ```rust,noplayground
226 {{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-02-adding-another-rectangle-test/src/lib.rs:here}}
227 ```
228
229 Because the correct result of the `can_hold` function in this case is `false`,
230 we need to negate that result before we pass it to the `assert!` macro. As a
231 result, our test will pass if `can_hold` returns `false`:
232
233 ```console
234 {{#include ../listings/ch11-writing-automated-tests/no-listing-02-adding-another-rectangle-test/output.txt}}
235 ```
236
237 Two tests that pass! Now let’s see what happens to our test results when we
238 introduce a bug in our code. Let’s change the implementation of the `can_hold`
239 method by replacing the greater than sign with a less than sign when it
240 compares the widths:
241
242 ```rust,not_desired_behavior,noplayground
243 {{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/src/lib.rs:here}}
244 ```
245
246 Running the tests now produces the following:
247
248 ```console
249 {{#include ../listings/ch11-writing-automated-tests/no-listing-03-introducing-a-bug/output.txt}}
250 ```
251
252 Our tests caught the bug! Because `larger.width` is 8 and `smaller.width` is
253 5, the comparison of the widths in `can_hold` now returns `false`: 8 is not
254 less than 5.
255
256 ### Testing Equality with the `assert_eq!` and `assert_ne!` Macros
257
258 A common way to test functionality is to compare the result of the code under
259 test to the value you expect the code to return to make sure they’re equal. You
260 could do this using the `assert!` macro and passing it an expression using the
261 `==` operator. However, this is such a common test that the standard library
262 provides a pair of macros—`assert_eq!` and `assert_ne!`—to perform this test
263 more conveniently. These macros compare two arguments for equality or
264 inequality, respectively. They’ll also print the two values if the assertion
265 fails, which makes it easier to see *why* the test failed; conversely, the
266 `assert!` macro only indicates that it got a `false` value for the `==`
267 expression, not the values that lead to the `false` value.
268
269 In Listing 11-7, we write a function named `add_two` that adds `2` to its
270 parameter and returns the result. Then we test this function using the
271 `assert_eq!` macro.
272
273 <span class="filename">Filename: src/lib.rs</span>
274
275 ```rust,noplayground
276 {{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-07/src/lib.rs}}
277 ```
278
279 <span class="caption">Listing 11-7: Testing the function `add_two` using the
280 `assert_eq!` macro</span>
281
282 Let’s check that it passes!
283
284 ```console
285 {{#include ../listings/ch11-writing-automated-tests/listing-11-07/output.txt}}
286 ```
287
288 The first argument we gave to the `assert_eq!` macro, `4`, is equal to the
289 result of calling `add_two(2)`. The line for this test is `test
290 tests::it_adds_two ... ok`, and the `ok` text indicates that our test passed!
291
292 Let’s introduce a bug into our code to see what it looks like when a test that
293 uses `assert_eq!` fails. Change the implementation of the `add_two` function to
294 instead add `3`:
295
296 ```rust,not_desired_behavior,noplayground
297 {{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-04-bug-in-add-two/src/lib.rs:here}}
298 ```
299
300 Run the tests again:
301
302 ```console
303 {{#include ../listings/ch11-writing-automated-tests/no-listing-04-bug-in-add-two/output.txt}}
304 ```
305
306 Our test caught the bug! The `it_adds_two` test failed, displaying the message
307 `` assertion failed: `(left == right)` `` and showing that `left` was `4` and
308 `right` was `5`. This message is useful and helps us start debugging: it means
309 the `left` argument to `assert_eq!` was `4` but the `right` argument, where we
310 had `add_two(2)`, was `5`.
311
312 Note that in some languages and test frameworks, the parameters to the
313 functions that assert two values are equal are called `expected` and `actual`,
314 and the order in which we specify the arguments matters. However, in Rust,
315 they’re called `left` and `right`, and the order in which we specify the value
316 we expect and the value that the code under test produces doesn’t matter. We
317 could write the assertion in this test as `assert_eq!(add_two(2), 4)`, which
318 would result in a failure message that displays `` assertion failed: `(left ==
319 right)` `` and that `left` was `5` and `right` was `4`.
320
321 The `assert_ne!` macro will pass if the two values we give it are not equal and
322 fail if they’re equal. This macro is most useful for cases when we’re not sure
323 what a value *will* be, but we know what the value definitely *won’t* be if our
324 code is functioning as we intend. For example, if we’re testing a function that
325 is guaranteed to change its input in some way, but the way in which the input
326 is changed depends on the day of the week that we run our tests, the best thing
327 to assert might be that the output of the function is not equal to the input.
328
329 Under the surface, the `assert_eq!` and `assert_ne!` macros use the operators
330 `==` and `!=`, respectively. When the assertions fail, these macros print their
331 arguments using debug formatting, which means the values being compared must
332 implement the `PartialEq` and `Debug` traits. All the primitive types and most
333 of the standard library types implement these traits. For structs and enums
334 that you define, you’ll need to implement `PartialEq` to assert that values of
335 those types are equal or not equal. You’ll need to implement `Debug` to print
336 the values when the assertion fails. Because both traits are derivable traits,
337 as mentioned in Listing 5-12 in Chapter 5, this is usually as straightforward
338 as adding the `#[derive(PartialEq, Debug)]` annotation to your struct or enum
339 definition. See Appendix C, [“Derivable Traits,”][derivable-traits]<!-- ignore
340 --> for more details about these and other derivable traits.
341
342 ### Adding Custom Failure Messages
343
344 You can also add a custom message to be printed with the failure message as
345 optional arguments to the `assert!`, `assert_eq!`, and `assert_ne!` macros. Any
346 arguments specified after the one required argument to `assert!` or the two
347 required arguments to `assert_eq!` and `assert_ne!` are passed along to the
348 `format!` macro (discussed in Chapter 8 in the [“Concatenation with the `+`
349 Operator or the `format!`
350 Macro”][concatenation-with-the--operator-or-the-format-macro]<!-- ignore -->
351 section), so you can pass a format string that contains `{}` placeholders and
352 values to go in those placeholders. Custom messages are useful to document
353 what an assertion means; when a test fails, you’ll have a better idea of what
354 the problem is with the code.
355
356 For example, let’s say we have a function that greets people by name and we
357 want to test that the name we pass into the function appears in the output:
358
359 <span class="filename">Filename: src/lib.rs</span>
360
361 ```rust,noplayground
362 {{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-05-greeter/src/lib.rs}}
363 ```
364
365 The requirements for this program haven’t been agreed upon yet, and we’re
366 pretty sure the `Hello` text at the beginning of the greeting will change. We
367 decided we don’t want to have to update the test when the requirements change,
368 so instead of checking for exact equality to the value returned from the
369 `greeting` function, we’ll just assert that the output contains the text of the
370 input parameter.
371
372 Let’s introduce a bug into this code by changing `greeting` to not include
373 `name` to see what this test failure looks like:
374
375 ```rust,not_desired_behavior,noplayground
376 {{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-06-greeter-with-bug/src/lib.rs:here}}
377 ```
378
379 Running this test produces the following:
380
381 ```console
382 {{#include ../listings/ch11-writing-automated-tests/no-listing-06-greeter-with-bug/output.txt}}
383 ```
384
385 This result just indicates that the assertion failed and which line the
386 assertion is on. A more useful failure message in this case would print the
387 value we got from the `greeting` function. Let’s change the test function,
388 giving it a custom failure message made from a format string with a placeholder
389 filled in with the actual value we got from the `greeting` function:
390
391 ```rust,ignore
392 {{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-07-custom-failure-message/src/lib.rs:here}}
393 ```
394
395 Now when we run the test, we’ll get a more informative error message:
396
397 ```console
398 {{#include ../listings/ch11-writing-automated-tests/no-listing-07-custom-failure-message/output.txt}}
399 ```
400
401 We can see the value we actually got in the test output, which would help us
402 debug what happened instead of what we were expecting to happen.
403
404 ### Checking for Panics with `should_panic`
405
406 In addition to checking that our code returns the correct values we expect,
407 it’s also important to check that our code handles error conditions as we
408 expect. For example, consider the `Guess` type that we created in Chapter 9,
409 Listing 9-10. Other code that uses `Guess` depends on the guarantee that `Guess`
410 instances will contain only values between 1 and 100. We can write a test that
411 ensures that attempting to create a `Guess` instance with a value outside that
412 range panics.
413
414 We do this by adding another attribute, `should_panic`, to our test function.
415 This attribute makes a test pass if the code inside the function panics; the
416 test will fail if the code inside the function doesn’t panic.
417
418 Listing 11-8 shows a test that checks that the error conditions of `Guess::new`
419 happen when we expect them to.
420
421 <span class="filename">Filename: src/lib.rs</span>
422
423 ```rust,noplayground
424 {{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-08/src/lib.rs}}
425 ```
426
427 <span class="caption">Listing 11-8: Testing that a condition will cause a
428 `panic!`</span>
429
430 We place the `#[should_panic]` attribute after the `#[test]` attribute and
431 before the test function it applies to. Let’s look at the result when this test
432 passes:
433
434 ```console
435 {{#include ../listings/ch11-writing-automated-tests/listing-11-08/output.txt}}
436 ```
437
438 Looks good! Now let’s introduce a bug in our code by removing the condition
439 that the `new` function will panic if the value is greater than 100:
440
441 ```rust,not_desired_behavior,noplayground
442 {{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-08-guess-with-bug/src/lib.rs:here}}
443 ```
444
445 When we run the test in Listing 11-8, it will fail:
446
447 ```console
448 {{#include ../listings/ch11-writing-automated-tests/no-listing-08-guess-with-bug/output.txt}}
449 ```
450
451 We don’t get a very helpful message in this case, but when we look at the test
452 function, we see that it’s annotated with `#[should_panic]`. The failure we got
453 means that the code in the test function did not cause a panic.
454
455 Tests that use `should_panic` can be imprecise because they only indicate that
456 the code has caused some panic. A `should_panic` test would pass even if the
457 test panics for a different reason from the one we were expecting to happen. To
458 make `should_panic` tests more precise, we can add an optional `expected`
459 parameter to the `should_panic` attribute. The test harness will make sure that
460 the failure message contains the provided text. For example, consider the
461 modified code for `Guess` in Listing 11-9 where the `new` function panics with
462 different messages depending on whether the value is too small or too large.
463
464 <span class="filename">Filename: src/lib.rs</span>
465
466 ```rust,noplayground
467 {{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-09/src/lib.rs:here}}
468 ```
469
470 <span class="caption">Listing 11-9: Testing that a condition will cause a
471 `panic!` with a particular panic message</span>
472
473 This test will pass because the value we put in the `should_panic` attribute’s
474 `expected` parameter is a substring of the message that the `Guess::new`
475 function panics with. We could have specified the entire panic message that we
476 expect, which in this case would be `Guess value must be less than or equal to
477 100, got 200.` What you choose to specify in the expected parameter for
478 `should_panic` depends on how much of the panic message is unique or dynamic
479 and how precise you want your test to be. In this case, a substring of the
480 panic message is enough to ensure that the code in the test function executes
481 the `else if value > 100` case.
482
483 To see what happens when a `should_panic` test with an `expected` message
484 fails, let’s again introduce a bug into our code by swapping the bodies of the
485 `if value < 1` and the `else if value > 100` blocks:
486
487 ```rust,ignore,not_desired_behavior
488 {{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-09-guess-with-panic-msg-bug/src/lib.rs:here}}
489 ```
490
491 This time when we run the `should_panic` test, it will fail:
492
493 ```console
494 {{#include ../listings/ch11-writing-automated-tests/no-listing-09-guess-with-panic-msg-bug/output.txt}}
495 ```
496
497 The failure message indicates that this test did indeed panic as we expected,
498 but the panic message did not include the expected string `'Guess value must be
499 less than or equal to 100'`. The panic message that we did get in this case was
500 `Guess value must be greater than or equal to 1, got 200.` Now we can start
501 figuring out where our bug is!
502
503 ### Using `Result<T, E>` in Tests
504
505 So far, we’ve written tests that panic when they fail. We can also write tests
506 that use `Result<T, E>`! Here’s the test from Listing 11-1, rewritten to use
507 `Result<T, E>` and return an `Err` instead of panicking:
508
509 ```rust,noplayground
510 {{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-10-result-in-tests/src/lib.rs}}
511 ```
512
513 The `it_works` function now has a return type, `Result<(), String>`. In the
514 body of the function, rather than calling the `assert_eq!` macro, we return
515 `Ok(())` when the test passes and an `Err` with a `String` inside when the test
516 fails.
517
518 Writing tests so they return a `Result<T, E>` enables you to use the question
519 mark operator in the body of tests, which can be a convenient way to write
520 tests that should fail if any operation within them returns an `Err` variant.
521
522 You can’t use the `#[should_panic]` annotation on tests that use `Result<T,
523 E>`. Instead, you should return an `Err` value directly when the test should
524 fail.
525
526 Now that you know several ways to write tests, let’s look at what is happening
527 when we run our tests and explore the different options we can use with `cargo
528 test`.
529
530 [concatenation-with-the--operator-or-the-format-macro]:
531 ch08-02-strings.html#concatenation-with-the--operator-or-the-format-macro
532 [controlling-how-tests-are-run]:
533 ch11-02-running-tests.html#controlling-how-tests-are-run
534 [derivable-traits]: appendix-03-derivable-traits.html
535 [doc-comments]: ch14-02-publishing-to-crates-io.html#documentation-comments-as-tests
536 [paths-for-referring-to-an-item-in-the-module-tree]: ch07-03-paths-for-referring-to-an-item-in-the-module-tree.html