Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
The reason to put tests in a separate module is so they are grouped. Another advantage is that you can put helper functions that are only needed for tests, but are not themselves tests in the modu...
Answer
#1: Initial revision
The reason to put tests in a separate module is so they are grouped. Another advantage is that you can put helper functions that are only needed for tests, but are not themselves tests in the module as well: ```rust #[cfg(test)] mod tests { fn setup() -> Vec<i32> { vec![1, 2, 3] } #[test] fn test_length() { let v = setup(); assert_eq!(v.len(), 3); } #[test] fn test_last() { let mut v = setup(); assert_eq!(v.pop(), Some(3)); } } ``` It's *not* used to exclude test functions from the final binary, a `#[test]` annotation on it's own is sufficient to ensure that, i.e. it includes a `#[cfg(test)]` annotation. As [specified in the reference](https://doc.rust-lang.org/reference/attributes/testing.html#the-test-attribute) > [`#[test]`] functions are only compiled when in test mode.