Use the #[cfg(test)] attribute to include test modules only during test execution.
Use the #[cfg(test)] attribute above a module to include it only when running tests.
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
Run the tests with cargo test to compile and execute the conditional code.
The #[cfg] attribute lets you include specific code blocks only when you are running tests, keeping your production code clean. Think of it like a secret door that only opens when you are in testing mode, allowing you to verify your logic without affecting the final product.