@@ -18,13 +18,15 @@ title: "11. Writing Automated Tests"
18
18
19
19
## Testing in Rust
20
20
21
- - Ensures code functions as expected
22
- - Rust provides support for writing automated tests
23
- - Types of tests in Rust:
21
+ - Tests ensure code functions as expected
22
+ - Rust ( ` cargo ` ) provides native support for automating tests
23
+ - Types of test in Rust include :
24
24
25
25
::: nonincremental
26
26
- Unit tests
27
27
- Integration tests
28
+ - Documentation tests
29
+ - Benchmarks
28
30
:::
29
31
30
32
::: notes
@@ -33,6 +35,11 @@ In this chapter, we'll explore how to write tests in Rust to ensure our code wor
33
35
34
36
# How to Write Tests
35
37
38
+ ## Typical Flow of a Test
39
+ - Set up any needed data or state.
40
+ - Run the code you want to test.
41
+ - Assert that the results are what you expect.
42
+
36
43
## Anatomy of a Test Function
37
44
38
45
- Annotate functions with ` #[test] ` to make them tests
@@ -244,6 +251,7 @@ Ignored tests can be useful for tests that take a long time or require special s
244
251
### The Tests Module and ` #[cfg(test)] `
245
252
246
253
- Place tests in a ` tests ` module annotated with ` #[cfg(test)] `
254
+ - This module is only compiled when testing
247
255
248
256
``` rust
249
257
#[cfg(test)]
@@ -261,7 +269,7 @@ Ignored tests can be useful for tests that take a long time or require special s
261
269
The ` #[cfg(test)] ` attribute ensures the tests are only compiled and run when testing.
262
270
:::
263
271
264
- ### Testing Private Functions
272
+ ## Testing Private Functions
265
273
266
274
- You can test private functions in Rust
267
275
- Example:
@@ -290,10 +298,7 @@ Because the tests module is inside the same file, it has access to private funct
290
298
291
299
- Test the public API as an external user would
292
300
- Placed in the ` tests ` directory
293
-
294
- ### The ` tests ` Directory
295
-
296
- - Create a ` tests ` directory at the project root
301
+ - Each file in ` tests ` is a separate crate
297
302
298
303
```
299
304
my_project
@@ -303,8 +308,6 @@ Because the tests module is inside the same file, it has access to private funct
303
308
└── tests
304
309
└── integration_test.rs
305
310
```
306
-
307
- - Each file in ` tests ` is a separate crate
308
311
- Example:
309
312
310
313
``` rust
@@ -320,7 +323,7 @@ Because the tests module is inside the same file, it has access to private funct
320
323
Integration tests verify that your library works as expected when used by others.
321
324
:::
322
325
323
- ### Submodules in Integration Tests
326
+ ## Submodules in Integration Tests
324
327
325
328
- Share code between integration tests using modules
326
329
- Create ` tests/common/mod.rs ` for shared code
@@ -345,7 +348,7 @@ Integration tests verify that your library works as expected when used by others
345
348
This avoids code duplication in your tests.
346
349
:::
347
350
348
- ### Integration Tests for Binary Crates
351
+ ## Integration Tests for Binary Crates
349
352
350
353
- Binary crates (with only ` main.rs ` ) can't be tested directly via integration tests
351
354
- Solution: Extract logic into a library crate (` lib.rs ` )
0 commit comments