|
14 | 14 | ├── Cargo.toml
|
15 | 15 | ├── src
|
16 | 16 | │ └── main.rs
|
| 17 | +│ └── lib.rs |
17 | 18 | └── tests
|
18 | 19 | ├── my_test.rs
|
19 | 20 | └── my_other_test.rs
|
20 | 21 | ```
|
21 | 22 |
|
22 |
| -Each file in `tests` is a separate integration test. |
| 23 | +Each file in `tests` is a separate |
| 24 | +[integration test](https://doc.rust-lang.org/book/ch11-03-test-organization.html#integration-tests), |
| 25 | +i.e. a test that is meant to test your library as if it were being called from a dependent |
| 26 | +crate. |
| 27 | + |
| 28 | +The [Testing][testing] chapter elaborates on the three different testing styles: |
| 29 | +[Unit][unit_testing], [Doc][doc_testing], and [Integration][integration_testing]. |
23 | 30 |
|
24 | 31 | `cargo` naturally provides an easy way to run all of your tests!
|
25 | 32 |
|
@@ -64,5 +71,84 @@ test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 2 filtered out
|
64 | 71 | ```
|
65 | 72 |
|
66 | 73 | One word of caution: Cargo may run multiple tests concurrently, so make sure
|
67 |
| -that they don't race with each other. For example, if they all output to a |
68 |
| -file, you should make them write to different files. |
| 74 | +that they don't race with each other. |
| 75 | + |
| 76 | +One example of this concurrency causing issues is if two tests output to a |
| 77 | +file, such as below: |
| 78 | + |
| 79 | +```rust |
| 80 | +#[cfg(test)] |
| 81 | +mod tests { |
| 82 | + // Import the necessary modules |
| 83 | + use std::fs::OpenOptions; |
| 84 | + use std::io::Write; |
| 85 | + |
| 86 | + // This test writes to a file |
| 87 | + #[test] |
| 88 | + fn test_file() { |
| 89 | + // Opens the file ferris.txt or creates one if it doesn't exist. |
| 90 | + let mut file = OpenOptions::new() |
| 91 | + .append(true) |
| 92 | + .create(true) |
| 93 | + .open("ferris.txt") |
| 94 | + .expect("Failed to open ferris.txt"); |
| 95 | + |
| 96 | + // Print "Ferris" 5 times. |
| 97 | + for _ in 0..5 { |
| 98 | + file.write_all("Ferris\n".as_bytes()) |
| 99 | + .expect("Could not write to ferris.txt"); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // This test tries to write to the same file |
| 104 | + #[test] |
| 105 | + fn test_file_also() { |
| 106 | + // Opens the file ferris.txt or creates one if it doesn't exist. |
| 107 | + let mut file = OpenOptions::new() |
| 108 | + .append(true) |
| 109 | + .create(true) |
| 110 | + .open("ferris.txt") |
| 111 | + .expect("Failed to open ferris.txt"); |
| 112 | + |
| 113 | + // Print "Corro" 5 times. |
| 114 | + for _ in 0..5 { |
| 115 | + file.write_all("Corro\n".as_bytes()) |
| 116 | + .expect("Could not write to ferris.txt"); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +Although the intent is to get the following: |
| 123 | +```shell |
| 124 | +$ cat ferris.txt |
| 125 | +Ferris |
| 126 | +Ferris |
| 127 | +Ferris |
| 128 | +Ferris |
| 129 | +Ferris |
| 130 | +Corro |
| 131 | +Corro |
| 132 | +Corro |
| 133 | +Corro |
| 134 | +Corro |
| 135 | +``` |
| 136 | +What actually gets put into `ferris.txt` is this: |
| 137 | +```shell |
| 138 | +$ cargo test test_foo |
| 139 | +Corro |
| 140 | +Ferris |
| 141 | +Corro |
| 142 | +Ferris |
| 143 | +Corro |
| 144 | +Ferris |
| 145 | +Corro |
| 146 | +Ferris |
| 147 | +Corro |
| 148 | +Ferris |
| 149 | +``` |
| 150 | + |
| 151 | +[testing]: ../testing.md |
| 152 | +[unit_testing]: ../testing/unit_testing.md |
| 153 | +[integration_testing]: ../testing/unit_testing.md |
| 154 | +[doc_testing]: ../testing/doc_testing.md |
0 commit comments