Gradle – How to Display Test Results in the Console in Real Time

Problem Statement:

Most of the times we would like to see test results ( system.out/err, log messages from components being tested ) as they run in the same console I run:

gradle test

And not wait until tests are done to look at the test reports.

Solution :


import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent

tasks.withType(Test) {
    testLogging {
        // set options for log level LIFECYCLE
        events TestLogEvent.FAILED,
               TestLogEvent.PASSED,
               TestLogEvent.SKIPPED,
               TestLogEvent.STANDARD_OUT
        exceptionFormat TestExceptionFormat.FULL
        showExceptions true
        showCauses true
        showStackTraces true

        // set options for log level DEBUG and INFO
        debug {
            events TestLogEvent.STARTED,
                   TestLogEvent.FAILED,
                   TestLogEvent.PASSED,
                   TestLogEvent.SKIPPED,
                   TestLogEvent.STANDARD_ERROR,
                   TestLogEvent.STANDARD_OUT
            exceptionFormat TestExceptionFormat.FULL
        }
        info.events = debug.events
        info.exceptionFormat = debug.exceptionFormat

        afterSuite { desc, result ->
            if (!desc.parent) { // will match the outermost suite
                def output = "Results: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} successes, ${result.failedTestCount} failures, ${result.skippedTestCount} skipped)"
                def startItem = '|  ', endItem = '  |'
                def repeatLength = startItem.length() + output.length() + endItem.length()
                println('\n' + ('-' * repeatLength) + '\n' + startItem + output + endItem + '\n' + ('-' * repeatLength))
            }
        }
    }
}

 OR

We can use Gradle Test Logger Plugin to print beautiful logs on the console.

Usage

 plugins {
 id 'com.adarshr.test-logger' version ''
 }

Make sure you always get the latest version from Gradle Central.

Configuration

You don’t need any configuration at all. However, the plugin offers a few options. This can be done as follows (default values shown):


testlogger {
theme 'standard' // pick a theme - mocha, standard or plain
showExceptions true // set to false to disable detailed failure logs
slowThreshold 1000 // set threshold in milliseconds to highlight slow tests
showSummary true // displays a breakdown of passes, failures and skips along with total duration
}

Source : https://github.com/radarsh/gradle-test-logger-plugin

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s