Crate wraptest[][src]

A simple way to run code before and after every unit test.

Suppose you want to set up a tracing subscriber to display log and tracing events before some tests:

#[cfg(test)]
#[wraptest::wrap_tests(before = setup_logs)]
mod tests {
    use tracing::info;
    use tracing_subscriber::fmt::format::FmtSpan;

    fn setup_logs() {
        tracing_subscriber::fmt::fmt()
            .with_env_filter("debug")
            .with_span_events(FmtSpan::NEW | FmtSpan::CLOSE)
            .init();
    }

    #[test]    
    fn with_tracing() {
        info!("with tracing");
    }

    #[tokio::test]
    async fn with_tracing_async() {
        info!("with tracing -- but async");
    }
}

This translates to essentially:

#[test]
fn with_tracing() {
    fn with_tracing() {
        info!("with tracing");
    }
    setup_logs();
    with_tracing();
}

#[tokio::test]
async fn with_tracing_async() {
    async fn with_tracing_async() {
        info!("with tracing -- but async");
    }
    setup_logs();
    with_tracing_async().await;
}

You can also specify #[wraptest(after = after_fn)] to run code after each test.

Attribute Macros

wrap_tests