]> git.proxmox.com Git - rustc.git/blob - src/doc/style/testing/unit.md
New upstream version 1.12.1+dfsg1
[rustc.git] / src / doc / style / testing / unit.md
1 % Unit testing
2
3 Unit tests should live in a `tests` submodule at the bottom of the module they
4 test. Mark the `tests` submodule with `#[cfg(test)]` so it is only compiled when
5 testing.
6
7 The `tests` module should contain:
8
9 * Imports needed only for testing.
10 * Functions marked with `#[test]` striving for full coverage of the parent module's
11 definitions.
12 * Auxiliary functions needed for writing the tests.
13
14 For example:
15
16 ``` rust
17 // Excerpt from std::str
18
19 #[cfg(test)]
20 mod tests {
21 #[test]
22 fn test_eq() {
23 assert!((eq(&"".to_owned(), &"".to_owned())));
24 assert!((eq(&"foo".to_owned(), &"foo".to_owned())));
25 assert!((!eq(&"foo".to_owned(), &"bar".to_owned())));
26 }
27 }
28 ```
29
30 > **[FIXME]** add details about useful macros for testing, e.g. `assert!`