test-env-log 0.1.0

A replacement of the #[test] attribute that initializes the env_logger before running tests.
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() {
  // ...
}