Coder Social home page Coder Social logo

lachlanmckee / timber-junit-rule Goto Github PK

View Code? Open in Web Editor NEW
43.0 3.0 2.0 90 KB

A highly configurable JUnit Rule that outputs Timber logs to standard output

License: MIT License

Java 100.00%
java android timber logging testing timber-tree timber-logs timber-junit-rule junit junit-test

timber-junit-rule's Introduction

Timber JUnit-Rule

This library provides a JUnit TestRule that plants a temporary Timber tree that pipes any logs sent via Timber to the standard System.out. Once a unit test has completed, the Timber tree is removed to avoid logging unintended test cases.

The rule is extremely useful for Android JUnit tests, as the Timber logs do not show without planting a tree.

Usage

Using the library is very straight forward. An example is as follows:

public class TestExample {
    @Rule
    public TimberTestRule timberTestRule = TimberTestRule.builder()
            .minPriority(Log.ERROR)
            .showThread(true)
            .showTimestamp(false)
            .onlyLogWhenTestFails(true)
            .build();
}

There are also several factory methods which cover many common cases. These are:

public class TestExample {
    @Rule
    public TimberTestRule logAllAlwaysRule = TimberTestRule.logAllAlways();
    @Rule
    public TimberTestRule logAllOnFailuresRule = TimberTestRule.logAllWhenTestFails();
    @Rule
    public TimberTestRule logErrorsAlwaysRule = TimberTestRule.logErrorsAlways();
    @Rule
    public TimberTestRule logErrorsOnFailuresRule = TimberTestRule.logErrorsWhenTestFails();
}

Configuration

As seen in the example above, there are many ways to modify the output using the following behaviours:

  • The minimum log level to output.
  • Whether thread ids are shown.
  • Whether timestamps are shown.
  • Whether to always log, or only log when a unit test fails.

Download

This library is available on Maven, you can add it to your project using the following gradle dependencies:

testCompile 'net.lachlanmckee:timber-junit-rule:1.0.1'

timber-junit-rule's People

Contributors

lachlanmckee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

timber-junit-rule's Issues

Concurrency issue with flush

There appears to be a race condition in BufferedJUnitTimberTree.flushLogs. If mLogMessageBuffer is mutated by another thread while flushLogs is running, a ConcurrentModificationException is thrown.

Unfortunately I don't have a precise example to repro but the defect seems straight forward and should be quite easy to fix by adding some synchronization.

Cheers!

Add a feature to only log when a unit test fails

Currently the Timber JUnit rule will log for every single test, regardless of the outcome of the test.

This can be quite noisy, and unnecessary when unit tests are passing.

The enhancement to add will be to add a feature that buffer the logs and only outputs them if the unit test fails.

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.