[Solved] Println! during tests only?


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 {
    println!("planning to return 5");

fn main() {

fn test_do_thing() {
    assert_eq!(do_thing(), 5);

cargo run   # no output

cargo test -- --nocapture   # message is printed


Ah, thank you, I didn’t realize you could put that on statements.