Gradle: How to Display Test Results in the Console in Real Time?

后端 未结 16 989
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 00:39

I 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


        
相关标签:
16条回答
  • 2020-11-28 01:33

    Just add the following closure to your build.gradle. the output will be printed after the execution of every test.

    test{
        useJUnitPlatform()
        afterTest { desc, result ->
            def output = "Class name: ${desc.className}, Test name: ${desc.name},  (Test status: ${result.resultType})"
            println( '\n' + output)
        }
    }
    
    0 讨论(0)
  • 2020-11-28 01:34

    You can add a Groovy closure inside your build.gradle file that does the logging for you:

    test {
        afterTest { desc, result -> 
            logger.quiet "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
        }
    }
    

    On your console it then reads like this:

    :compileJava UP-TO-DATE
    :compileGroovy
    :processResources
    :classes
    :jar
    :assemble
    :compileTestJava
    :compileTestGroovy
    :processTestResources
    :testClasses
    :test
    Executing test maturesShouldBeCharged11DollarsForDefaultMovie [movietickets.MovieTicketsTests] with result: SUCCESS
    Executing test studentsShouldBeCharged8DollarsForDefaultMovie [movietickets.MovieTicketsTests] with result: SUCCESS
    Executing test seniorsShouldBeCharged6DollarsForDefaultMovie [movietickets.MovieTicketsTests] with result: SUCCESS
    Executing test childrenShouldBeCharged5DollarsAnd50CentForDefaultMovie [movietickets.MovieTicketsTests] with result: SUCCESS
    :check
    :build
    

    Since version 1.1 Gradle supports much more options to log test output. With those options at hand you can achieve a similar output with the following configuration:

    test {
        testLogging {
            events "passed", "skipped", "failed"
        }
    }
    
    0 讨论(0)
  • 2020-11-28 01:34

    Disclaimer: I am the developer of the Gradle Test Logger Plugin.

    You can simply use the Gradle Test Logger Plugin to print beautiful logs on the console. The plugin uses sensible defaults to satisfy most users with little or no configuration but also offers a number of themes and configuration options to suit everyone.

    Examples

    Standard theme

    Mocha theme

    Usage

    plugins {
        id 'com.adarshr.test-logger' version '<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 {
        // pick a theme - mocha, standard, plain, mocha-parallel, standard-parallel or plain-parallel
        theme 'standard'
    
        // set to false to disable detailed failure logs
        showExceptions true
    
        // set to false to hide stack traces
        showStackTraces true
    
        // set to true to remove any filtering applied to stack traces
        showFullStackTraces false
    
        // set to false to hide exception causes
        showCauses true
    
        // set threshold in milliseconds to highlight slow tests
        slowThreshold 2000
    
        // displays a breakdown of passes, failures and skips along with total duration
        showSummary true
    
        // set to true to see simple class names
        showSimpleNames false
    
        // set to false to hide passed tests
        showPassed true
    
        // set to false to hide skipped tests
        showSkipped true
    
        // set to false to hide failed tests
        showFailed true
    
        // enable to see standard out and error streams inline with the test results
        showStandardStreams false
    
        // set to false to hide passed standard out and error streams
        showPassedStandardStreams true
    
        // set to false to hide skipped standard out and error streams
        showSkippedStandardStreams true
    
        // set to false to hide failed standard out and error streams
        showFailedStandardStreams true
    }
    

    I hope you will enjoy using it.

    0 讨论(0)
  • 2020-11-28 01:35

    Following on from Benjamin Muschko's answer (19 March 2011), you can use the -i flag along with grep, to filter out 1000s of unwanted lines. Examples:

    Strong filter - Only display each unit test name and test result and the overall build status. Setup errors or exceptions are not displayed.

    ./gradlew test -i | grep -E " > |BUILD"
    

    Soft filter - Display each unit test name and test result, as well as setup errors/exceptions. But it will also include some irrelevant info:

    ./gradlew test -i | grep -E -v "^Executing |^Creating |^Parsing |^Using |^Merging |^Download |^title=Compiling|^AAPT|^future=|^task=|:app:|V/InstrumentationResultParser:"
    

    Soft filter, Alternative syntax: (search tokens are split into individual strings)

    ./gradlew test -i | grep -v -e "^Executing " -e "^Creating " -e "^Parsing " -e "^Using " -e "^Merging " -e "^Download " -e "^title=Compiling" -e "^AAPT" -e "^future=" -e "^task=" -e ":app:" -e "V/InstrumentationResultParser:"
    

    Explanation of how it works:

    The first command is ./gradlew test -i and "-i" means "Info/Verbose" mode, which prints the result of each test in real-time, but also displays large amounts of unwanted debug lines.

    So the output of the first command, ./gradlew test -i, is piped to a second command grep, which will filter out many unwanted lines, based on a regular expression. "-E" enables the regular expression mode for a single string; "-e" enables regular expressions for multiple strings; and "|" in the regex string means "or".

    In the strong filter, a unit test name and test result is allowed to display using " > ", and the overall status is allowed with "BUILD".

    In the soft filter, the "-v" flag means "not containing" and "^" means "start of line". So it strips out all lines that start with "Executing " or start with "Creating ", etc.


    Example for Android instrumentation unit tests, with gradle 5.1:

    ./gradlew connectedDebugAndroidTest --continue -i | grep -v -e \
        "^Transforming " -e "^Skipping " -e "^Cache " -e "^Performance " -e "^Creating " -e \
        "^Parsing " -e "^file " -e "ddms: " -e ":app:" -e "V/InstrumentationResultParser:"
    

    Example for Jacoco unit test coverage, with gradle 4.10:

    ./gradlew createDebugCoverageReport --continue -i | grep -E -v "^Executing |^Creating |^Parsing |^Using |^Merging |^Download |^title=Compiling|^AAPT|^future=|^task=|:app:|V/InstrumentationResultParser:"
    
    0 讨论(0)
提交回复
热议问题