summary refs log tree commit diff
path: root/src/doc/style/testing/unit.md
blob: dbbe9fc3ac6da92602304f64de31b07afa01e80a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
% Unit testing

Unit tests should live in a `tests` submodule at the bottom of the module they
test. Mark the `tests` submodule with `#[cfg(test)]` so it is only compiled when
testing.

The `tests` module should contain:

* Imports needed only for testing.
* Functions marked with `#[test]` striving for full coverage of the parent module's
  definitions.
* Auxiliary functions needed for writing the tests.

For example:

``` rust
// Excerpt from std::str

#[cfg(test)]
mod tests {
    #[test]
    fn test_eq() {
        assert!((eq(&"".to_owned(), &"".to_owned())));
        assert!((eq(&"foo".to_owned(), &"foo".to_owned())));
        assert!((!eq(&"foo".to_owned(), &"bar".to_owned())));
    }
}
```

> **[FIXME]** add details about useful macros for testing, e.g. `assert!`