44
55To create test functions, add a ` #[test] ` attribute like this:
66
7- ``` rust
7+ ~~~
88fn return_two() -> int {
99 2
1010}
@@ -14,17 +14,17 @@ fn return_two_test() {
1414 let x = return_two();
1515 assert!(x == 2);
1616}
17- ```
17+ ~~~
1818
1919To run these tests, use ` rustc --test ` :
2020
21- ```
21+ ~~~ {.notrust}
2222$ rustc --test foo.rs; ./foo
2323running 1 test
2424test return_two_test ... ok
2525
2626test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
27- ```
27+ ~~~
2828
2929` rustc foo.rs ` will * not* compile the tests, since ` #[test] ` implies
3030` #[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
3535Rust has built in support for simple unit testing. Functions can be
3636marked as unit tests using the 'test' attribute.
3737
38- ``` rust
38+ ~~~
3939#[test]
4040fn return_none_if_empty() {
4141 // ... test code ...
4242}
43- ```
43+ ~~~
4444
4545A test function's signature must have no arguments and no return
4646value. To run the tests in a crate, it must be compiled with the
@@ -54,15 +54,15 @@ then the test fails.
5454When compiling a crate with the '--test' flag '--cfg test' is also
5555implied, so that tests can be conditionally compiled.
5656
57- ``` rust
57+ ~~~
5858#[cfg(test)]
5959mod tests {
6060 #[test]
6161 fn return_none_if_empty() {
6262 // ... test code ...
6363 }
6464}
65- ```
65+ ~~~
6666
6767Additionally #[ test] items behave as if they also have the
6868#[ 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
7979task to fail then the test will be counted as successful; otherwise it
8080will be counted as a failure. For example:
8181
82- ``` rust
82+ ~~~
8383#[test]
8484#[should_fail]
8585fn test_out_of_bounds_failure() {
8686 let v: [int] = [];
8787 v[0];
8888}
89- ```
89+ ~~~
9090
9191A test runner built with the '--test' flag supports a limited set of
9292arguments to control which tests are run: the first free argument
@@ -126,7 +126,7 @@ amount.
126126
127127For example:
128128
129- ``` rust
129+ ~~~
130130extern mod extra;
131131use std::vec;
132132
@@ -141,7 +141,7 @@ fn initialise_a_vector(b: &mut extra::test::BenchHarness) {
141141 b.iter(|| {vec::from_elem(1024, 0u64);} );
142142 b.bytes = 1024 * 8;
143143}
144- ```
144+ ~~~
145145
146146The benchmark runner will calibrate measurement of the benchmark
147147function 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.
168168
169169### Typical test run
170170
171- ```
171+ ~~~ {.notrust}
172172> mytests
173173
174174running 30 tests
@@ -178,11 +178,11 @@ running driver::tests::mytest2 ... ignored
178178running driver::tests::mytest30 ... ok
179179
180180result: ok. 28 passed; 0 failed; 2 ignored
181- ```
181+ ~~~ {.notrust}
182182
183183### Test run with failures
184184
185- ```
185+ ~~~ {.notrust}
186186> mytests
187187
188188running 30 tests
@@ -192,23 +192,23 @@ running driver::tests::mytest2 ... ignored
192192running driver::tests::mytest30 ... FAILED
193193
194194result: FAILED. 27 passed; 1 failed; 2 ignored
195- ```
195+ ~~~
196196
197197### Running ignored tests
198198
199- ```
199+ ~~~ {.notrust}
200200> mytests --ignored
201201
202202running 2 tests
203203running driver::tests::mytest2 ... failed
204204running driver::tests::mytest10 ... ok
205205
206206result: FAILED. 1 passed; 1 failed; 0 ignored
207- ```
207+ ~~~
208208
209209### Running a subset of tests
210210
211- ```
211+ ~~~ {.notrust}
212212> mytests mytest1
213213
214214running 11 tests
@@ -218,19 +218,19 @@ running driver::tests::mytest10 ... ignored
218218running driver::tests::mytest19 ... ok
219219
220220result: ok. 11 passed; 0 failed; 1 ignored
221- ```
221+ ~~~
222222
223223### Running benchmarks
224224
225- ```
225+ ~~~ {.notrust}
226226> mytests --bench
227227
228228running 2 tests
229229test bench_sum_1024_ints ... bench: 709 ns/iter (+/- 82)
230230test initialise_a_vector ... bench: 424 ns/iter (+/- 99) = 19320 MB/s
231231
232232test result: ok. 0 passed; 0 failed; 0 ignored; 2 measured
233- ```
233+ ~~~
234234
235235## Saving and ratcheting metrics
236236
0 commit comments