Skip to content

Commit 381201f

Browse files
committed
Test attributes deserve their own section
1 parent 845d1ed commit 381201f

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

src/attributes.md

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,6 @@ names have meaning.
119119
bar;` is equivalent to `mod bar { /* contents of foo.rs */ }`. The path is
120120
taken relative to the directory that the current module is in.
121121

122-
## Function-only attributes
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-
130122
## FFI attributes
131123

132124
On an `extern` block, the following attributes are interpreted:
@@ -228,6 +220,42 @@ are transformed into `doc` attributes.
228220

229221
See [The Rustdoc Book] for reference material on this attribute.
230222

223+
### Testing
224+
225+
The compiler comes with a default test framework. It works by attributing
226+
functions with the `test` attribute. These functions are only compiled when
227+
compiling with the test harness. Like [main], functions annotated with this
228+
attribute must take no arguments, must not declare any
229+
[trait or lifetime bounds], must not have any [where clauses], and its return
230+
type must be one of the following:
231+
232+
* `()`
233+
<!-- * `!` -->
234+
* `Result<(), E> where E: Error`
235+
<!-- * Result<!, E> where E: Error` -->
236+
237+
> Note: The implementation of which return types are allowed is determined by
238+
> the unstable [`Termination`] trait.
239+
240+
<!-- If the previous section needs updating (from "must take no arguments"
241+
onwards, also update it in the crates-and-source-files.md file -->
242+
243+
> Note: The test harness is ran by passing the `--test` argument to `rustc` or
244+
> using `cargo test`.
245+
246+
Tests that return `()` pass as long as they terminate do not panic. Tests that
247+
return a `Result` pass as long as they return `Ok(())`. Tests that do not
248+
terminate neither pass nor fail.
249+
250+
A function annotated with the `test` attribute can also be annotated with the
251+
`ignore` attribute. The *`ignore` attribute* tells the test harness to not
252+
execute that function as a test. It will still only be compiled when compiling
253+
with the test harness.
254+
255+
A function annotated with the `test` attribute that returns `()` can also be
256+
annotated with the `should_panic` attribute. The *`should_panic` attribute*
257+
makes the test only pass if it actually panics.
258+
231259
### Conditional compilation
232260

233261
The `cfg` and `cfg_attr` attributes control conditional compilation of [items]
@@ -499,4 +527,8 @@ You can implement `derive` for your own traits through [procedural macros].
499527
[external blocks]: items/external-blocks.html
500528
[items]: items.html
501529
[conditional compilation]: conditional-compilation.html
502-
[trait]: items/traits.html
530+
[trait]: items/traits.html
531+
[main]: crates-and-source-files.html
532+
[`Termination`]: ../std/process/trait.Termination.html
533+
[where clause]: items/where-clauses.html
534+
[trait or lifetime bounds]: trait-bounds.html

src/crates-and-source-files.md

Lines changed: 6 additions & 2 deletions
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)