Skip to content

Commit 68891e5

Browse files
authored
Merge pull request #1438 from mautamu/testing-add-example
Add some tests to cargo/test.md. Partially addresses #1304
2 parents e0a721f + fb8b522 commit 68891e5

File tree

1 file changed

+89
-3
lines changed

1 file changed

+89
-3
lines changed

src/cargo/test.md

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,19 @@ foo
1414
├── Cargo.toml
1515
├── src
1616
│ └── main.rs
17+
│ └── lib.rs
1718
└── tests
1819
├── my_test.rs
1920
└── my_other_test.rs
2021
```
2122

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].
2330

2431
`cargo` naturally provides an easy way to run all of your tests!
2532

@@ -64,5 +71,84 @@ test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 2 filtered out
6471
```
6572

6673
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

Comments
 (0)