Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Show STDOUT and STDERR during testing

  • nocapture

In this example there are print-statements both in the code and in the test function.

#![allow(unused)]
fn main() {
pub fn add(left: usize, right: usize) -> usize {
    println!("STDOUT In the application");
    eprintln!("STDERR In the application");
    left + right
}

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

    #[test]
    fn it_works() {
        println!("STDOUT in the test");
        eprintln!("STDERR in the test");
        let result = add(2, 2);
        assert_eq!(result, 4);
    }
}
}

If we run cargo test we don't see any of this as the tester captures them.

If we run cargo test -- --nocapture then we'll see the output of all the 4 print-statements.