How should I go about having output that is only done during tests? The output would coming from the main code that is being tested, not from the tests themselves.
A #[cfg(test)]
attribute can be used to mark statements that should only be compiled when building for tests.
fn do_thing() -> i32 {
#[cfg(test)]
println!("planning to return 5");
5
}
fn main() {
do_thing();
}
#[test]
fn test_do_thing() {
assert_eq!(do_thing(), 5);
}
cargo run
# no output
cargo test -- --nocapture
# message is printed
8 Likes
Ah, thank you, I didn't realize you could put that on statements.
2 Likes