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

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

    Merge of Shubham's great answer and JJD use enum instead of string

    tasks.withType(Test) {
       testLogging {
           // set options for log level LIFECYCLE
           events TestLogEvent.PASSED,
                TestLogEvent.SKIPPED, TestLogEvent.FAILED, TestLogEvent.STANDARD_OUT
           showExceptions true
           exceptionFormat TestExceptionFormat.FULL
           showCauses true
           showStackTraces true
    
        // set options for log level DEBUG and INFO
           debug {
            events TestLogEvent.STARTED, TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED, TestLogEvent.STANDARD_OUT, TestLogEvent.STANDARD_ERROR
            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))
               }
           }
       }
    }
    
    0 讨论(0)
  • 2020-11-28 01:23

    If you have a build.gradle.kts written in Kotlin DSL you can print test results with (I was developing a kotlin multi-platform project, with no "java" plugin applied):

    tasks.withType<AbstractTestTask> {
        afterSuite(KotlinClosure2({ desc: TestDescriptor, result: TestResult ->
            if (desc.parent == null) { // will match the outermost suite
                println("Results: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} successes, ${result.failedTestCount} failures, ${result.skippedTestCount} skipped)")
            }
        }))
    }
    
    0 讨论(0)
  • 2020-11-28 01:29

    You could run Gradle with INFO logging level on the command line. It'll show you the result of each test while they are running. Downside is that you will get far more output for other tasks also.

    gradle test -i
    
    0 讨论(0)
  • 2020-11-28 01:30

    Here is my fancy version:

    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} passed, ${result.failedTestCount} failed, ${result.skippedTestCount} skipped)"
                    def startItem = '|  ', endItem = '  |'
                    def repeatLength = startItem.length() + output.length() + endItem.length()
                    println('\n' + ('-' * repeatLength) + '\n' + startItem + output + endItem + '\n' + ('-' * repeatLength))
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-28 01:31

    'test' task does not work for Android plugin, for Android plugin use the following:

    // Test Logging
    tasks.withType(Test) {
        testLogging {
            events "started", "passed", "skipped", "failed"
        }
    }
    

    See the following: https://stackoverflow.com/a/31665341/3521637

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

    As a follow up to Shubham's great answer I like to suggest using enum values instead of strings. Please take a look at the documentation of the TestLogging class.

    import org.gradle.api.tasks.testing.logging.TestExceptionFormat
    import org.gradle.api.tasks.testing.logging.TestLogEvent
    
    tasks.withType(Test) {
        testLogging {
            events TestLogEvent.FAILED,
                   TestLogEvent.PASSED,
                   TestLogEvent.SKIPPED,
                   TestLogEvent.STANDARD_ERROR,
                   TestLogEvent.STANDARD_OUT
            exceptionFormat TestExceptionFormat.FULL
            showCauses true
            showExceptions true
            showStackTraces true
        }
    }
    
    0 讨论(0)
提交回复
热议问题