5

I'm trying to print more logs to my console by following this post: https://stackoverflow.com/a/36130467, but I keep getting these errors:

  1. Cannot resolve symbol testLogging
  2. Cannot resolve symbol events
  3. Cannot resolve symbol exceptionFormat
  4. Cannot resolve symbol debug etc.

Here's my code

build.gradle file:

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

    apply plugin: 'groovy'
    apply plugin: 'idea'
    apply plugin: 'java'

    repositories {
        jcenter()
        mavenCentral()
    }


    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))
                }
            }
        }
    }

    dependencies {
        testImplementation group: 'org.codehaus.groovy', name: 'groovy-all', version:'2.4.10'
        testImplementation group: 'org.spockframework', name: 'spock-unitils', version:'1.1-groovy-2.4-rc-4'
    }
tim_yates
  • 167,322
  • 27
  • 342
  • 338
Jay F.
  • 51
  • 5
  • yes I have. What if I'm not allowed to use a library, also I still want to know what is wrong with my script. – Jay F. Mar 17 '18 at 16:02

1 Answers1

1

With JUnit 5 and Gradle 6, following test task definition prints the names and results of the tests:

test {
    useJUnitPlatform()
    testLogging.events.addAll([TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED])
}

If you want to have aggregated information about the test results, you can use the following version of the test task. This will add the the following report after all the tests are completed.

----------------------------------------------------------------------
|  Results: SUCCESS (95 tests, 95 successes, 0 failures, 0 skipped)  |
----------------------------------------------------------------------
test {
    useJUnitPlatform()
    testLogging.events.addAll([TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED])
    testLogging {
        afterSuite { desc, result ->
            if (!desc.parent) { // group the results in one
                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))
            }
        }
    }
}

I would recommend to have a look at these documentations: