@@ -10,20 +10,43 @@ r[attributes.testing.test]
10
10
## The ` test ` attribute
11
11
12
12
r[ attributes.testing.test.intro]
13
- The * ` test ` attribute* marks a function to be executed as a test.
14
-
15
- r[ attributes.testing.test.enabled]
16
- These functions are only compiled when in test mode.
13
+ The * ` test ` [ attribute] [ attributes ] * marks a function to be executed as a test.
14
+
15
+ > [ !EXAMPLE]
16
+ > ``` rust
17
+ > # pub fn add (left : u64 , right : u64 ) -> u64 { left + right }
18
+ >
19
+ > #[test]
20
+ > fn it_works () {
21
+ > let result = add (2 , 2 );
22
+ > assert_eq! (result , 4 );
23
+ > }
24
+ > ```
25
+
26
+ r [attributes . testing. test. syntax]
27
+ The `test ` attribute uses the [MetaWord ] syntax and thus does not take any inputs .
17
28
18
29
r [attributes . testing. test. allowed- positions ]
19
- Test functions must be free, monomorphic functions that take no arguments, and the return type must implement the [ ` Termination ` ] trait, for example:
30
+ The ` test ` attribute may only be applied to [ free functions ] that are monomorphic , that take no arguments , and where the return type implements the [`Termination `] trait .
20
31
21
- * ` () `
22
- * ` Result<T, E> where T: Termination, E: Debug `
23
- * ` ! `
32
+ > [! NOTE ]
33
+ > Some of types that implement the [`Termination `] trait include :
34
+ > * `()`
35
+ > * `Result <T , E > where T : Termination , E : Debug `
24
36
25
- <!-- If the previous section needs updating (from "must take no arguments"
26
- onwards, also update it in the crates-and-source-files.md file -->
37
+ r [attributes . testing. test. duplicates]
38
+ Only the first instance of `test ` on a function is honored .
39
+
40
+ > [! NOTE ]
41
+ > Subsequent `test ` attributes are currently ignored and `rustc ` warns about these .
42
+
43
+ <! -- TODO : This is a minor lie . Currently rustc warns that duplicates are ignored , but it then generates multiple test entries with the same name . I would vote for rejecting this in the future . - ->
44
+
45
+ r [attributes . testing. test. stdlib]
46
+ The `test ` attribute is exported from the standard library prelude as [`std :: prelude :: v1 :: test `].
47
+
48
+ r [attributes . testing. test. enabled]
49
+ These functions are only compiled when in test mode .
27
50
28
51
> [! NOTE ]
29
52
> The test mode is enabled by passing the `-- test ` argument to `rustc ` or using `cargo test `.
@@ -36,17 +59,18 @@ In particular:
36
59
* Tests that return `ExitCode :: SUCCESS ` pass , and tests that return `ExitCode :: FAILURE ` fail .
37
60
* Tests that do not terminate neither pass nor fail .
38
61
39
- ``` rust
40
- # use std :: io;
41
- # fn setup_the_thing () -> io :: Result <i32 > { Ok (1 ) }
42
- # fn do_the_thing (s : & i32 ) -> io :: Result <()> { Ok (()) }
43
- #[test]
44
- fn test_the_thing () -> io :: Result <()> {
45
- let state = setup_the_thing ()? ; // expected to succeed
46
- do_the_thing (& state )? ; // expected to succeed
47
- Ok (())
48
- }
49
- ```
62
+ > [! EXAMPLE ]
63
+ > ```rust
64
+ > # use std :: io;
65
+ > # fn setup_the_thing () -> io :: Result <i32 > { Ok (1 ) }
66
+ > # fn do_the_thing (s : & i32 ) -> io :: Result <()> { Ok (()) }
67
+ > #[test]
68
+ > fn test_the_thing () -> io :: Result <()> {
69
+ > let state = setup_the_thing ()? ; // expected to succeed
70
+ > do_the_thing (& state )? ; // expected to succeed
71
+ > Ok (())
72
+ > }
73
+ > ```
50
74
51
75
r [attributes . testing. ignore]
52
76
## The `ignore ` attribute
@@ -102,3 +126,4 @@ fn mytest() {
102
126
[ `test` conditional compilation option ] : ../conditional-compilation.md#test
103
127
[ attributes ] : ../attributes.md
104
128
[ `ExitCode` ] : std::process::ExitCode
129
+ [ free functions ] : ../glossary.md#free-item
0 commit comments