Skip to content

Logging from tests appears not to work as described in the docs #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wykurz opened this issue Nov 23, 2020 · 7 comments
Closed

Logging from tests appears not to work as described in the docs #186

wykurz opened this issue Nov 23, 2020 · 7 comments
Labels

Comments

@wykurz
Copy link

wykurz commented Nov 23, 2020

Calling the init() defined as:

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

in my tests appears not to produce any log output:

RUST_LOG=debug cargo test
    Finished test [unoptimized + debuginfo] target(s) in 0.02s
     Running target/debug/deps/log_fail-ea6852f038016c7e

running 1 test
test tests::test1 ... ok

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

However running env_logger::init() does seem to work:

#[cfg(test)]
mod tests {
    #[test]
    fn test1() {
	env_logger::init();
        debug!("hello!");
	info!("hello!");
	error!("hello!");
	assert!(true);
    }
}

RUST_LOG=debug cargo test
Finished test [unoptimized + debuginfo] target(s) in 0.01s
Running target/debug/deps/log_fail-ea6852f038016c7e

running 1 test
[2020-11-23T05:39:32Z DEBUG log_fail::tests] hello!
[2020-11-23T05:39:32Z INFO log_fail::tests] hello!
[2020-11-23T05:39:32Z ERROR log_fail::tests] hello!
test tests::test1 ... ok

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

This is with env_logger 0.8.2 and log 0.4.11.
@KodrAus
Copy link
Collaborator

KodrAus commented Jan 11, 2021

Thanks for the report! It's possible something changed about the way the test framework captures output so I'll take a look. If we don't need is_test anymore that would be great.

@KodrAus KodrAus added the bug label Jan 12, 2021
@detly
Copy link

detly commented Mar 11, 2021

I just hit a similar problem that seems like it's related. If not, I'll open a separate issue.

Cargo.toml:

[package]
name = "logtest"
version = "0.1.0"
edition = "2018"

[dependencies]
log = "0.4"
env_logger = "0.8"

tests/test1.rs:

use env_logger;

#[test]
fn one() {
    env_logger::builder()
        .is_test(true)
        .format_timestamp(None)
        .try_init()
        .expect("Could not initialise test logging");
}

#[test]
fn two() {
    env_logger::builder()
        .is_test(true)
        .format_timestamp(None)
        .try_init()
        .expect("Could not initialise test logging");
}

Running cargo test gives:

running 2 tests
test two ... ok
test one ... FAILED

failures:

---- one stdout ----
thread 'one' panicked at 'Could not initialise test logging: SetLoggerError(())', tests/test1.rs:9:10
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    one

test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

error: test failed, to rerun pass '--test test1'

@ditsing
Copy link

ditsing commented Jul 10, 2021

Running cargo test gives:

running 2 tests
test two ... ok
test one ... FAILED

failures:

---- one stdout ----
thread 'one' panicked at 'Could not initialise test logging: SetLoggerError(())', tests/test1.rs:9:10
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    one

test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

error: test failed, to rerun pass '--test test1'

Logger can only be setup once per process. The two tests share the same process so you cannot set up logger in both. You can either make sure try_init() is only called once (e.g. by using std::sync::Once), or wait for subprocess testing.

@mpizenberg
Copy link

mpizenberg commented Aug 6, 2021

I also thought that logging from tests did not work, but in my case, it was actually that I did not set the level filter correctly. Now all is working as it should when initializing with:

fn init_log() {
    let _ = env_logger::builder()
        .filter_level(LevelFilter::Trace)
        .is_test(true)
        .try_init();
}

And as expected from the doc, if I do not set is_test(true), the logs go to stderr and are mixed with the output from cargo test instead of being captured and output for the failing tests. So for me, all is working well.

@wykurz could this be that you just need to add filter_level to your env logger builder?

By the way, it is normal that your logs do not appear when the test is passing. is_test(true) is meant so that test logs are captured for each test and only reported for failing tests, colocated with the test output.

@wykurz
Copy link
Author

wykurz commented Aug 15, 2021 via email

@ditsing
Copy link

ditsing commented Aug 15, 2021

@wykurz Have you tried cargo test -- --nocapture? Test outputs are captured by default. They will be silently swallowed if the test succeeds.

@wykurz
Copy link
Author

wykurz commented Aug 15, 2021 via email

@wykurz wykurz closed this as completed Aug 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants