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 _ = builder.is_test.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;
It is of course also possible to initialize logging for a chosen set of tests, by only annotating these with the custom attribute:
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.,
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.