Coder Social home page Coder Social logo

jstarks / test-env-log Goto Github PK

View Code? Open in Web Editor NEW

This project forked from d-e-s-o/test-log

0.0 1.0 0.0 93 KB

A replacement of the #[test] attribute that initializes logging and/or tracing infrastructure before running tests.

License: Apache License 2.0

Rust 100.00%

test-env-log's Introduction

pipeline crates.io Docs rustc

test-env-log

test-env-log is a crate that takes care of automatically initializing logging and/or tracing for Rust tests.

When running Rust tests it can often be helpful to have easy access to the verbose log messages emitted by the code under test. Commonly, these log messages may be coming from the log crate or being emitted through the tracing infrastructure.

The problem with either -- in the context of testing -- is that some form of initialization is required in order to make these crate's messages appear on a standard output stream.

The commonly used env_logger (which provides an easy way to configure log based logging), for example, needs to be initialized like this:

let _ = env_logger::builder().is_test(true).try_init();

in each and every test.

Similarly, tracing based solutions require a subscriber to be registered that writes events/spans to the console.

This crate takes care of this per-test initialization in an intuitive way.

Usage

The crate provides a custom #[test] attribute that, when used for running a particular test, takes care of initializing log and/or tracing beforehand.

Example

As such, usage is as simple as importing and using said attribute:

use test_env_log::test;

#[test]
fn it_works() {
  info!("Checking whether it still works...");
  assert_eq!(2 + 2, 4);
  info!("Looks good!");
}

It is of course also possible to initialize logging for a chosen set of tests, by only annotating these with the custom attribute:

#[test_env_log::test]
fn it_still_works() {
  // ...
}

You can also wrap another attribute. For example, suppose you use #[tokio::test] to run async tests:

use test_env_log::test;

#[test(tokio::test)]
async fn it_still_works() {
  // ...
}

Logging Configuration

As usual when running cargo test, the output is captured by the framework by default and only shown on test failure. The --nocapture argument can be supplied in order to overwrite this setting. E.g.,

$ cargo test -- --nocapture

Furthermore, the RUST_LOG environment variable is honored and can be used to influence the log level to work with (among other things). Please refer to the env_logger docs for more information.

If the trace feature is enabled, the RUST_LOG_SPAN_EVENTS environment variable can be used to configure the tracing subscriber to log synthesized events at points in the span lifecycle. Set the variable to a comma-separated list of events you want to see. For example, RUST_LOG_SPAN_EVENTS=full or RUST_LOG_SPAN_EVENTS=new,close.

Valid events are new, enter, exit, close, active, and full. See the tracing_subscriber docs for details on what the events mean.

Features

The crate comes with two features:

  • log, enabled by default, controls initialization for the log crate.
  • trace, disabled by default, controls initialization for the tracing crate.

Depending on what backend the crate-under-test (and its dependencies) use, the respective feature should be enabled to make messages that are emitted by the test manifest on the console.

Note that as a user you are required to explicitly add env_logger or tracing-subscriber as a dependency to your project-under-test (when enabling the log or trace feature, respectively). E.g.,

[dev-dependencies]
env_logger = "*"
tracing = {version = "0.1", default-features = false}
tracing-subscriber = {version = "0.2.17", default-features = false, features = ["env-filter", "fmt"]}

test-env-log's People

Contributors

d-e-s-o avatar dzfranklin avatar johnchildren avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.