4
4
5
5
To create test functions, add a ` #[test] ` attribute like this:
6
6
7
- ``` rust
7
+ ~~~
8
8
fn return_two() -> int {
9
9
2
10
10
}
@@ -14,17 +14,17 @@ fn return_two_test() {
14
14
let x = return_two();
15
15
assert!(x == 2);
16
16
}
17
- ```
17
+ ~~~
18
18
19
19
To run these tests, use ` rustc --test ` :
20
20
21
- ```
21
+ ~~~ {.notrust}
22
22
$ rustc --test foo.rs; ./foo
23
23
running 1 test
24
24
test return_two_test ... ok
25
25
26
26
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
27
- ```
27
+ ~~~
28
28
29
29
` rustc foo.rs ` will * not* compile the tests, since ` #[test] ` implies
30
30
` #[cfg(test)] ` . The ` --test ` flag to ` rustc ` implies ` --cfg test ` .
@@ -35,12 +35,12 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
35
35
Rust has built in support for simple unit testing. Functions can be
36
36
marked as unit tests using the 'test' attribute.
37
37
38
- ``` rust
38
+ ~~~
39
39
#[test]
40
40
fn return_none_if_empty() {
41
41
// ... test code ...
42
42
}
43
- ```
43
+ ~~~
44
44
45
45
A test function's signature must have no arguments and no return
46
46
value. To run the tests in a crate, it must be compiled with the
@@ -54,15 +54,15 @@ then the test fails.
54
54
When compiling a crate with the '--test' flag '--cfg test' is also
55
55
implied, so that tests can be conditionally compiled.
56
56
57
- ``` rust
57
+ ~~~
58
58
#[cfg(test)]
59
59
mod tests {
60
60
#[test]
61
61
fn return_none_if_empty() {
62
62
// ... test code ...
63
63
}
64
64
}
65
- ```
65
+ ~~~
66
66
67
67
Additionally #[ test] items behave as if they also have the
68
68
#[ cfg(test)] attribute, and will not be compiled when the --test flag
@@ -79,14 +79,14 @@ Tests that are intended to fail can be annotated with the
79
79
task to fail then the test will be counted as successful; otherwise it
80
80
will be counted as a failure. For example:
81
81
82
- ``` rust
82
+ ~~~
83
83
#[test]
84
84
#[should_fail]
85
85
fn test_out_of_bounds_failure() {
86
86
let v: [int] = [];
87
87
v[0];
88
88
}
89
- ```
89
+ ~~~
90
90
91
91
A test runner built with the '--test' flag supports a limited set of
92
92
arguments to control which tests are run: the first free argument
@@ -126,7 +126,7 @@ amount.
126
126
127
127
For example:
128
128
129
- ``` rust
129
+ ~~~
130
130
extern mod extra;
131
131
use std::vec;
132
132
@@ -141,7 +141,7 @@ fn initialise_a_vector(b: &mut extra::test::BenchHarness) {
141
141
b.iter(|| {vec::from_elem(1024, 0u64);} );
142
142
b.bytes = 1024 * 8;
143
143
}
144
- ```
144
+ ~~~
145
145
146
146
The benchmark runner will calibrate measurement of the benchmark
147
147
function to run the ` iter ` block "enough" times to get a reliable
@@ -168,7 +168,7 @@ test-runner. Benchmarks are compiled-in but not executed by default.
168
168
169
169
### Typical test run
170
170
171
- ```
171
+ ~~~ {.notrust}
172
172
> mytests
173
173
174
174
running 30 tests
@@ -178,11 +178,11 @@ running driver::tests::mytest2 ... ignored
178
178
running driver::tests::mytest30 ... ok
179
179
180
180
result: ok. 28 passed; 0 failed; 2 ignored
181
- ```
181
+ ~~~ {.notrust}
182
182
183
183
### Test run with failures
184
184
185
- ```
185
+ ~~~ {.notrust}
186
186
> mytests
187
187
188
188
running 30 tests
@@ -192,23 +192,23 @@ running driver::tests::mytest2 ... ignored
192
192
running driver::tests::mytest30 ... FAILED
193
193
194
194
result: FAILED. 27 passed; 1 failed; 2 ignored
195
- ```
195
+ ~~~
196
196
197
197
### Running ignored tests
198
198
199
- ```
199
+ ~~~ {.notrust}
200
200
> mytests --ignored
201
201
202
202
running 2 tests
203
203
running driver::tests::mytest2 ... failed
204
204
running driver::tests::mytest10 ... ok
205
205
206
206
result: FAILED. 1 passed; 1 failed; 0 ignored
207
- ```
207
+ ~~~
208
208
209
209
### Running a subset of tests
210
210
211
- ```
211
+ ~~~ {.notrust}
212
212
> mytests mytest1
213
213
214
214
running 11 tests
@@ -218,19 +218,19 @@ running driver::tests::mytest10 ... ignored
218
218
running driver::tests::mytest19 ... ok
219
219
220
220
result: ok. 11 passed; 0 failed; 1 ignored
221
- ```
221
+ ~~~
222
222
223
223
### Running benchmarks
224
224
225
- ```
225
+ ~~~ {.notrust}
226
226
> mytests --bench
227
227
228
228
running 2 tests
229
229
test bench_sum_1024_ints ... bench: 709 ns/iter (+/- 82)
230
230
test initialise_a_vector ... bench: 424 ns/iter (+/- 99) = 19320 MB/s
231
231
232
232
test result: ok. 0 passed; 0 failed; 0 ignored; 2 measured
233
- ```
233
+ ~~~
234
234
235
235
## Saving and ratcheting metrics
236
236
0 commit comments