lundi 8 juin 2020

How to print rust cargo test output to console stdout?

What are ways to run Rust cargo test and have the test runner display any log output to the console stdout or stderr?

What I've learned so far is trying this:

  • cargo test -- --nocapture which tells cargo to show output instead of hiding output.
  • env_logger crate which enables using an environment variable to set the log level.

Cargo.toml dependency:

env_logger = "*"

Demo source code:

use log::*;

pub fn foo() -> bool {
    info!("hello world");
    true
}

#[cfg(test)]
mod tests {
    use super::*;
    use env_logger;
    #[test]
    fn test_foo() {
        env_logger::init();
        assert!(foo());
    }
}

Command:

RUST_LOG=info cargo test -- --nocapture   

I'm seeking any similar ways, and ideally any official reference guide that shows good ways to accomplish this.

Aucun commentaire:

Enregistrer un commentaire