test-env-log 0.1.1

A replacement of the #[test] attribute that initializes the env_logger before running tests.
test-env-log-0.1.1 doesn't have any documentation.

pipeline crates.io Docs rustc

test-env-log

test-env-log is a crate that takes care of automatically initializing env_logger for Rust tests.

When running Rust tests it can often be helpful to have easy access to the verbose log messages emitted by the code under test. Assuming said code uses the log backend for its logging purposes that may not be straight forward, however. The problem is that all crates making use of log require some form of initialization to be usable.

The commonly used env_logger, for example, needs to be initialized like this:

let _ = env_logger::builder().is_test(true).try_init();

in each and every test.

This crate takes care of this per-test initialization in an intuitive way.

Usage

The crate provides a custom #[test] attribute that, when used for running a particular test, takes care of initializing env_logger beforehand. As such, usage is as simple as importing and using said attribute:

use test_env_log::test;

#[test]
fn it_works() {
  info!("Checking whether it still works...");
  assert_eq!(2 + 2, 4);
  info!("Looks good!");
}

It is of course also possible to initialize logging for a chosen set of tests, by only annotating these with the custom attribute:

#[test_env_log::test]
fn it_still_works() {
  // ...
}

As usual when running cargo test, the output is captured by the framework by default and only shown on test failure. The --nocapture argument can be supplied in order to overwrite this setting. E.g.,

$ cargo test -- --nocapture

Furthermore, by virtue of using env_logger, the RUST_LOG environment variable is honored and can be used to influence the log level to work with. Please refer to the env_logger docs for more information.