Skip to content

Commit a20926a

Browse files
committed
Mention expected in testing docs
1 parent 3246d4f commit a20926a

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/doc/guide-testing.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,21 @@ fn test_out_of_bounds_failure() {
8989
}
9090
~~~
9191

92+
`#[should_fail]` tests can be fragile as it's hard to guarantee that the test
93+
didn't fail for an unexpected reason. To help with this, an optional `expected`
94+
parameter can be added to the `should_fail` attribute. The test harness will
95+
make sure that the failure message contains the provided text. A safer version
96+
of the example above would be:
97+
98+
~~~test_harness
99+
#[test]
100+
#[should_fail(expected = "index out of bounds")]
101+
fn test_out_of_bounds_failure() {
102+
let v: &[int] = &[];
103+
v[0];
104+
}
105+
~~~
106+
92107
A test runner built with the `--test` flag supports a limited set of
93108
arguments to control which tests are run:
94109

0 commit comments

Comments
 (0)