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

后端 未结 16 1003
隐瞒了意图╮
隐瞒了意图╮ 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:12

    As stefanglase answered:

    adding the following code to your build.gradle (since version 1.1) works fine for output on passed, skipped and failed tests.

    test {
        testLogging {
            events "passed", "skipped", "failed", "standardOut", "standardError"
        }
    }
    

    What I want to say additionally (I found out this is a problem for starters) is that the gradle test command executes the test only one time per change.

    So if you are running it the second time there will be no output on test results. You can also see this in the building output: gradle then says UP-TO-DATE on tests. So its not executed a n-th time.

    Smart gradle!

    If you want to force the test cases to run, use gradle cleanTest test.

    This is slightly off topic but I hope it will help some newbies.

    edit

    As sparc_spread stated in the comments:

    If you want to force gradle to always run fresh tests (which might not always be a good idea) you can add outputs.upToDateWhen {false} to testLogging { [...] }. Continue reading here.

    Peace.

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

    Add this to build.gradle to stop gradle from swallowing stdout and stderr.

    test {
        testLogging.showStandardStreams = true
    }
    

    It's documented here.

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

    My favourite minimalistic version based on Shubham Chaudhary answer.

    Put this in build.gradle file:

    test {
        afterSuite { desc, result ->
        if (!desc.parent)
            println("${result.resultType} " +
                "(${result.testCount} tests, " +
                "${result.successfulTestCount} successes, " +
                "${result.failedTestCount} failures, " +
                "${result.skippedTestCount} skipped)")
        }
    }
    
    0 讨论(0)
  • 2020-11-28 01:13

    In Gradle using Android plugin:

    gradle.projectsEvaluated {
        tasks.withType(Test) { task ->
            task.afterTest { desc, result ->
                println "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
            }
        }
    }
    

    Then the output will be:

    Executing test testConversionMinutes [org.example.app.test.DurationTest] with result: SUCCESS

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

    For those using Kotlin DSL, you can do:

    tasks {
      named<Test>("test") {
        testLogging.showStandardStreams = true
      }
    }
    
    0 讨论(0)
  • 2020-11-28 01:20

    If you are using jupiter and none of the answers work, consider verifying it is setup correctly:

    test {
        useJUnitPlatform()
        outputs.upToDateWhen { false }
    }
    
    dependencies {
        testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
        testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
    }
    

    And then try the accepted answers

    0 讨论(0)
提交回复
热议问题