testlog
Yes, One macro
A micro-crate that provides test_log! – a macro that prints to stderr only when tests are running, and only for the crate where it's used.
Why?
You need to debug test failures. It's tempting to add print statements to the library you're testing. Bug, adding print statements of any kind pollute the logs of the consumers of your library. What you want is something in your library that only prints for your tests, and ideally is only shown during testing on failure.
This does exactly what you want: debug output that only appears during testing of your crate, and since output from a test is captured on success, you only see the log when the test fails. Perfect!.
Usage
Add to Cargo.toml:
[]
= "0.1"
Use in your code:
use test_log;
How it works
The test_log! macro checks cfg!(test) at compile time:
- In test builds: Expands to
eprintln!(...), even still, the output is captured on successful tests, so it doesn't pollute. - In production builds:
if falseshould be compiled out in release builds. - Crate-local: Only activates when the current crate is in test mode
That's it