rust-cli / env_logger

A logging implementation for `log` which is configured via an environment variable.
https://docs.rs/env_logger
Apache License 2.0
803 stars 125 forks source link

Output for tests doesn't work #182

Closed aDotInTheVoid closed 3 years ago

aDotInTheVoid commented 3 years ago

Cargo.toml

[package]
name = "xm"
version = "0.1.0"
authors = ["Nixon Enraght-Moony <nixon.emoony@gmail.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
log = "0.4.11"

[dev-dependencies]
env_logger = "0.8.1"

src/lib.rs

#[macro_use]
extern crate log;

fn add_one(num: i32) -> i32 {
    info!("add_one called with {}", num);
    num + 1
}

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

    fn init() {
        let _ = env_logger::builder().is_test(true).try_init();
    }

    #[test]
    fn it_adds_one() {
        init();

        info!("can log from the test too");
        assert_eq!(3, add_one(2));
    }

    #[test]
    fn it_handles_negative_numbers() {
        init();

        info!("logging from another test");
        assert_eq!(-7, add_one(-8));
    }
}
RUST_LOG=xm=info cargo test
warning: function is never used: `add_one`
 --> src/lib.rs:4:4
  |
4 | fn add_one(num: i32) -> i32 {
  |    ^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

    Finished test [unoptimized + debuginfo] target(s) in 0.13s
     Running target/debug/deps/xm-5a8d55a20d74cd8b

running 2 tests
test tests::it_handles_negative_numbers ... ok
test tests::it_adds_one ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests xm

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

rustc 1.47.0 (18bf6b4f0 2020-10-07)

aDotInTheVoid commented 3 years ago

Running RUST_LOG=xm=info cargo test -- --nocapture works, as stderr is hidden for sucesses. This is fine, except when the test fails by going into an infinate loop.