Skip to content

Commit 65e9b3d

Browse files
Merge pull request #382 from Havvy/test-attr
Test attributes deserve their own section
2 parents 85df178 + f301002 commit 65e9b3d

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

src/attributes.md

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

121-
## Function-only attributes
122-
123-
- `test` - indicates that this function is a test function, to only be compiled
124-
in case of `--test`.
125-
- `ignore` - indicates that this test function is disabled.
126-
- `should_panic` - indicates that this test function should panic, inverting the
127-
success condition.
128-
129121
## FFI attributes
130122

131123
On an `extern` block, the following attributes are interpreted:
@@ -227,6 +219,42 @@ are transformed into `doc` attributes.
227219

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

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

232260
The `cfg` and `cfg_attr` attributes control conditional compilation of [items]
@@ -498,4 +526,8 @@ You can implement `derive` for your own traits through [procedural macros].
498526
[external blocks]: items/external-blocks.html
499527
[items]: items.html
500528
[conditional compilation]: conditional-compilation.html
501-
[trait]: items/traits.html
529+
[trait]: items/traits.html
530+
[main]: crates-and-source-files.html
531+
[`Termination`]: ../std/process/trait.Termination.html
532+
[where clause]: items/where-clauses.html
533+
[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
* `()`
75+
* `Result<(), E> where E: Error`
7576
<!-- * `!` -->
76-
* `Result<T, E> where T: on this list, 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)