#logging #log #env-logger #tests #testing

test-logger

Simple helper to initialize env_logger before unit and integration tests. Works on stable rust.

1 unstable release

Uses old Rust 2015

0.1.0 Jul 14, 2016

#8 in #env-logger

Download history 53/week @ 2022-06-11 63/week @ 2022-06-18 47/week @ 2022-06-25 40/week @ 2022-07-02 36/week @ 2022-07-09 34/week @ 2022-07-16 63/week @ 2022-07-23 29/week @ 2022-07-30 40/week @ 2022-08-06 45/week @ 2022-08-13 38/week @ 2022-08-20 20/week @ 2022-08-27 40/week @ 2022-09-03 33/week @ 2022-09-10 30/week @ 2022-09-17 37/week @ 2022-09-24

143 downloads per month
Used in 2 crates

MIT/Apache

4KB

Test Logger

Simple helper to initialize env_logger before unit and integration tests. Works on Stable Rust.

Running cargo test does not initialize a logging backend, so those of us who use the log crate do not get to see log output when a unit or integration test fails. This crate just provides a simple macro that provides a way to initialize an env_logger during unit and integration tests while still making tests somewhat ergonomic to write.

test!

Many applications use logging to aid in debugging a running application, but unfortunately there's no simple way to initialize the logger prior to running unit and integration tests. The test! macro is just a simple wrapper to take care of initializing an env_logger prior to running tests. This ensures that log statements are printed to stdout during the tests. The macro ensures that the logger is initialized exactly once.

use the test! macro:

test!(logs_are_printed_to_stdout_when_this_fails, {
    assert!(subject.do_stuff_that_fails());
});
// The above macro expands to:
#[test]
fn logs_are_printed_to_stdout_when_this_fails() {
    test_logger::ensure_env_logger_initialized();
    assert!(subject.do_stuff_that_fails());
}

test!(should_panic, simple_way_to_annotate_a_test_that_is_supposed_to_panic, {
    panic!("I'm supposed to panic.");
});
//expands to:
#[test]
#[should_panic]
fn simple_way_to_annotate_a_test_that_is_supposed_to_panic() {
    test_logger::ensure_env_logger_initialized();
    assert!(subject.do_stuff_that_fails());
}

// alternate format to support any arbitrary attributes on the test function
test!(#[allow(dead_code)], this_test_function_will_have_the_dead_code_lint_disabled, {
    ... 
    

License

Licensed under either of

at your option.

Dependencies

~3MB
~73K SLoC