Test Modules
Unit tests are often put in a nested module (run tests on the Playground):
fn helper(a: &str, b: &str) -> String {format!("{a} {b}")}pub fn main() {println!("{}", helper("Hello", "World"));}#[cfg(test)]mod tests {use super::*;#[test]fn test_helper() {assert_eq!(helper("foo", "bar"), "foo bar");}}
- This lets you unit test private helpers.
- The
#[cfg(test)]attribute is only active when you runcargo test.