How to convert testOptions.unitTests.all to gradle Kotlin dsl

2.1k views Asked by At

How this code should be translated from Groovy to Kotlin DSL in Gradle?

testOptions.unitTests.all {
    testLogging {
        exceptionFormat = "full"
        events "passed", "failed", "standardError"
        showCauses true
        showExceptions true
    }
}
2

There are 2 answers

3
Saurabh Thorat On BEST ANSWER

Use this:

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

testOptions.unitTests.apply {
    all(KotlinClosure1<Test, Test>({
        apply {
            testLogging.exceptionFormat = TestExceptionFormat.FULL
            testLogging.events = setOf(
                TestLogEvent.PASSED,
                TestLogEvent.FAILED,
                TestLogEvent.STANDARD_ERROR
            )
            testLogging.showCauses = true
            testLogging.showExceptions = true
        }
    }, this))
}
0
Chisko On

This is a cleaner version of the accepted answer:

testOptions {
    unitTests {
        isReturnDefaultValues = true
        isIncludeAndroidResources = true
      
        all {
            it.apply {
            testLogging {
                events("started", "passed", "skipped", "failed")
            }
        }
    }
}