@@ -107,6 +107,10 @@ To check functions that should panic under certain circumstances, use attribute
107
107
the text of the panic message. If your function can panic in multiple ways, it helps
108
108
make sure your test is testing the correct panic.
109
109
110
+ ** Note** : Rust also allows a shorthand form ` #[should_panic = "message"] ` , which works
111
+ exactly like ` #[should_panic(expected = "message")] ` . Both are valid; the latter is more commonly
112
+ used and is considered more explicit.
113
+
110
114
``` rust,ignore
111
115
pub fn divide_non_zero_result(a: u32, b: u32) -> u32 {
112
116
if b == 0 {
@@ -137,6 +141,12 @@ mod tests {
137
141
fn test_specific_panic() {
138
142
divide_non_zero_result(1, 10);
139
143
}
144
+
145
+ #[test]
146
+ #[should_panic = "Divide result is zero"] // This also works
147
+ fn test_specific_panic_shorthand() {
148
+ divide_non_zero_result(1, 10);
149
+ }
140
150
}
141
151
```
142
152
@@ -149,8 +159,9 @@ running 3 tests
149
159
test tests::test_any_panic ... ok
150
160
test tests::test_divide ... ok
151
161
test tests::test_specific_panic ... ok
162
+ test tests::test_specific_panic_shorthand ... ok
152
163
153
- test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
164
+ test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
154
165
155
166
Doc-tests tmp-test-should-panic
156
167
0 commit comments