Skip to content

Commit 6653329

Browse files
committed
Test attributes deserve their own section
1 parent 7e042cd commit 6653329

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

src/attributes.md

+41-6
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,6 @@ names have meaning.
121121

122122
## Function-only attributes
123123

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.
129124
- `cold` - The function is unlikely to be executed, so optimize it (and calls
130125
to it) differently.
131126

@@ -228,6 +223,42 @@ are transformed into `doc` attributes.
228223

229224
See [The Rustdoc Book] for reference material on this attribute.
230225

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+
231262
### Conditional compilation
232263

233264
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].
570601
[modules]: items/modules.html
571602
[statements]: statements.html
572603
[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

src/crates-and-source-files.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,20 @@ apply to the crate as a whole.
6868

6969
A crate that contains a `main` [function] can be compiled to an executable. If a
7070
`main` function is present, it must take no arguments, must not declare any
71-
[trait or lifetime bounds], must not have any [where clauses], and its return
71+
[trait or lifetime bounds], must not have any [where clauses], and its return
7272
type must be one of the following:
7373

7474
* `()`
7575
<!-- * `!` -->
76-
* `Result<T, E> where T: on this list, E: Error`
76+
* `Result<(), E> where E: Error`
77+
<!-- * Result<!, E> where E: Error` -->
7778

7879
> Note: The implementation of which return types are allowed is determined by
7980
> the unstable [`Termination`] trait.
8081
82+
<!-- If the previous section needs updating (from "must take no arguments"
83+
onwards, also update it in the attributes.md file, testing section -->
84+
8185
The optional [_UTF8 byte order mark_] (UTF8BOM production) indicates that the
8286
file is encoded in UTF8. It can only occur at the beginning of the file and
8387
is ignored by the compiler.

0 commit comments

Comments
 (0)