Why doesn’t println! work in Rust unit tests?

I’ve implemented the following method and unit test:

use std::fs::File;
use std::path::Path;
use std::io::prelude::*;

fn read_file(path: &Path) {
    let mut file = File::open(path).unwrap();
    let mut contents = String::new();
    file.read_to_string(&mut contents).unwrap();
    println!("{}", contents);
}

#[test]
fn test_read_file() {
    let path = &Path::new("/etc/hosts");
    println!("{:?}", path);
    read_file(path);
}

I run the unit test this way:

rustc --test app.rs; ./app

I could also run this with

cargo test

I get a message back saying the test passed but the println! is never displayed on screen. Why not?

7 Answers
7

Leave a Comment