https://github.com/psfried/test-logger
Rust helper to initialize env_logger during unit and integration tests
https://github.com/psfried/test-logger
Last synced: over 1 year ago
JSON representation
Rust helper to initialize env_logger during unit and integration tests
- Host: GitHub
- URL: https://github.com/psfried/test-logger
- Owner: psFried
- License: other
- Created: 2016-07-14T00:01:28.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2019-01-14T15:52:55.000Z (over 7 years ago)
- Last Synced: 2025-02-28T07:11:58.580Z (over 1 year ago)
- Language: Rust
- Size: 3.91 KB
- Stars: 4
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
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:
```rust
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
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your option.