@@ -121,11 +121,6 @@ names have meaning.
121
121
122
122
## Function-only attributes
123
123
124
- - ` test ` - indicates that this function is a test function, to only be compiled
125
- in case of ` --test ` .
126
- - ` ignore ` - indicates that this test function is disabled.
127
- - ` should_panic ` - indicates that this test function should panic, inverting the
128
- success condition.
129
124
- ` cold ` - The function is unlikely to be executed, so optimize it (and calls
130
125
to it) differently.
131
126
@@ -228,6 +223,42 @@ are transformed into `doc` attributes.
228
223
229
224
See [ The Rustdoc Book] for reference material on this attribute.
230
225
226
+ ### Testing
227
+
228
+ The compiler comes with a default test framework. It works by attributing
229
+ functions with the ` test ` attribute. These functions are only compiled when
230
+ compiling with the test harness. Like [ main] , functions annotated with this
231
+ attribute must take no arguments, must not declare any
232
+ [ trait or lifetime bounds] , must not have any [ where clauses] , and its return
233
+ type must be one of the following:
234
+
235
+ * ` () `
236
+ <!-- * `!` -->
237
+ * ` Result<(), E> where E: Error `
238
+ <!-- * Result<!, E> where E: Error` -->
239
+
240
+ > Note: The implementation of which return types are allowed is determined by
241
+ > the unstable [ ` Termination ` ] trait.
242
+
243
+ <!-- If the previous section needs updating (from "must take no arguments"
244
+ onwards, also update it in the crates-and-source-files.md file -->
245
+
246
+ > Note: The test harness is ran by passing the ` --test ` argument to ` rustc ` or
247
+ > using ` cargo test ` .
248
+
249
+ Tests that return ` () ` pass as long as they terminate do not panic. Tests that
250
+ return a ` Result ` pass as long as they return ` Ok(()) ` . Tests that do not
251
+ terminate neither pass nor fail.
252
+
253
+ A function annotated with the ` test ` attribute can also be annotated with the
254
+ ` ignore ` attribute. The * ` ignore ` attribute* tells the test harness to not
255
+ execute that function as a test. It will still only be compiled when compiling
256
+ with the test harness.
257
+
258
+ A function annotated with the ` test ` attribute that returns ` () ` can also be
259
+ annotated with the ` should_panic ` attribute. The * ` should_panic ` attribute*
260
+ makes the test only pass if it actually panics.
261
+
231
262
### Conditional compilation
232
263
233
264
Sometimes one wants to have different compiler outputs from the same code,
@@ -570,4 +601,8 @@ You can implement `derive` for your own type through [procedural macros].
570
601
[ modules ] : items/modules.html
571
602
[ statements ] : statements.html
572
603
[ match expressions ] : expressions/match-expr.html
573
- [ external blocks ] : items/external-blocks.html
604
+ [ external blocks ] : items/external-blocks.html
605
+ [ main ] : crates-and-source-files.html
606
+ [ `Termination` ] : ../std/process/trait.Termination.html
607
+ [ where clause ] : items/where-clauses.html
608
+ [ trait or lifetime bounds ] : trait-bounds.html
0 commit comments