rust/src/doc/style/testing/unit.md
Johannes Oertel 07cc7d9960 Change name of unit test sub-module to "tests".
Changes the style guidelines regarding unit tests to recommend using a
sub-module named "tests" instead of "test" for unit tests as "test"
might clash with imports of libtest.
2015-04-24 23:06:41 +02:00

779 B

% 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:

// 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!