Best Practices for Organizing Tests in Rust Projects

Organize Rust tests by placing unit tests in the same file as the code they test using `#[cfg(test)]` modules, and integration tests in a separate `tests` directory.

Best Practices for Organizing Tests in Rust Projects

Organize Rust tests by placing unit tests in the same file as the code they test using #[cfg(test)] modules, and integration tests in a separate tests directory.

pub trait Summary {
    fn summarize(&self) -> String;
}

pub struct NewsArticle {
    pub headline: String,
    pub location: String,
    pub author: String,
    pub content: String,
}

impl Summary for NewsArticle {
    fn summarize(&self) -> String {
        format!("{}, by {} ({})", self.headline, self.author, self.location)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_summarize() {
        let article = NewsArticle {
            headline: String::from("Penguins win"),
            location: String::from("Pittsburgh"),
            author: String::from("Iceburgh"),
            content: String::from("Hockey wins"),
        };
        assert_eq!(article.summarize(), "Penguins win, by Iceburgh (Pittsburgh)");
    }
}