How to test private functions

Test private Rust functions by defining tests within the same module using the #[cfg(test)] attribute to access internal logic directly.

You test private functions by placing the test code in the same module as the function or by using the #[cfg(test)] attribute to expose the function only during testing. In Rust, tests defined within the same module can access private items, while tests in separate files cannot. Use the #[cfg(test)] attribute to wrap your test module, ensuring it only compiles when running cargo test.

pub fn public_function() {
    private_helper();
}

fn private_helper() {
    // private logic
}

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

    #[test]
    fn test_private_helper() {
        // Directly call the private function here
        private_helper();
        assert!(true);
    }
}