You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The *`ignore` [attribute][attributes]* can be used with the [`test` attribute][attributes.testing.test] to tell the test harness to not execute that function as a test.
The *`should_panic` [attribute][attributes]* causes a test to pass only if the [test function][attributes.testing.test] to which the attribute is applied panics.
The `should_panic` attribute has one of the following forms:
139
+
140
+
- [MetaWord]
141
+
> [!EXAMPLE]
142
+
> ```rust,no_run
143
+
> #[test]
144
+
> #[should_panic]
145
+
> fn mytest() { panic!("error: some message, and more"); }
146
+
> ```
147
+
148
+
- [MetaNameValueStr] --- The given string must appear within the panic message for the test to pass.
149
+
> [!EXAMPLE]
150
+
> ```rust,no_run
151
+
> #[test]
152
+
> #[should_panic = "some message"]
153
+
> fn mytest() { panic!("error: some message, and more"); }
154
+
> ```
155
+
156
+
- [MetaListNameValueStr] --- As with the [MetaNameValueStr] syntax, the given string must appear within the panic message.
157
+
> [!EXAMPLE]
158
+
> ```rust,no_run
159
+
> #[test]
160
+
> #[should_panic(expected = "some message")]
161
+
> fn mytest() { panic!("error: some message, and more"); }
162
+
> ```
163
+
164
+
> [!NOTE]
165
+
> `rustc` currently accepts the [MetaListNameValueStr] form with invalid syntax between the parentheses and emits a future-compatibility warning. This may become a hard error in the future.
The `should_panic` attribute may only be applied to functions annotated with the `test` attribute.
169
+
170
+
> [!NOTE]
171
+
> `rustc` currently accepts this attribute in other positions with a warning. This may become a hard error in the future.
172
+
173
+
r[attributes.testing.should_panic.duplicates]
174
+
Only the first instance of `should_panic` on a function is honored.
175
+
176
+
> [!NOTE]
177
+
> `rustc` currently ignores subsequent `should_panic` attributes and emits a future-compatibility warning. This may become a hard error in the future.
178
+
179
+
r[attributes.testing.should_panic.expected]
180
+
When the [MetaNameValueStr] form or the [MetaListNameValueStr] form with the `expected` key is used, the given string must appear somewhere within the panic message for the test to pass.
181
+
182
+
r[attributes.testing.should_panic.return]
183
+
The return type of the test function must be `()`.
0 commit comments