testlog

Crates.iotestlog
lib.rstestlog
version0.1.3
created_at2025-06-05 18:42:29.902107+00
updated_at2025-06-10 22:44:12.69742+00
descriptionA tiny macro for test-only stderr logging that activates only for the current crate
homepage
repositoryhttps://github.com/jodavaho/testlog
max_upload_size
id1702112
size6,817
Josh Vander Hook (jodavaho)

documentation

README

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. But, 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 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:

[dependencies]
testlog = "0.1.3"

Use in your code:

use testlog::test_log;

fn complex_function(data: &[i32]) -> Vec<i32> {
    test_log!("Processing {} items", data.len());
    
    let result: Vec<i32> = data.iter()
        .map(|&x| {
            test_log!("Processing item: {}", x);
            x * 2
        })
        .collect();
        
    test_log!("Result: {:?}", result);
    result
}

#[test]
fn test_complex_function() {
    let input = vec![1, 2, 3];
    let output = complex_function(&input);
    assert_eq!(output, vec![2, 4, 6]);
    // Debug output appears here when running `cargo test`
}

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 false should be compiled out in release builds.
  • Crate-local: Only activates when the current crate is in test mode

That's it

Commit count: 6

cargo fmt