Skip to content

Instantly share code, notes, and snippets.

@Zer0S2m
Forked from serpro69/build.gradle.kts
Created November 13, 2024 16:26
Show Gist options
  • Select an option

  • Save Zer0S2m/7a8b87a1e95792a80bfb60cd144f041a to your computer and use it in GitHub Desktop.

Select an option

Save Zer0S2m/7a8b87a1e95792a80bfb60cd144f041a to your computer and use it in GitHub Desktop.
gradle junit test output
import org.gradle.api.tasks.testing.TestResult.ResultType
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent
//...
tasks.test {
useJUnitPlatform()
maxParallelForks = 1
testLogging { // credits: https://stackoverflow.com/a/36130467/5917497
// set options for log level LIFECYCLE
events = setOf(
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 = setOf(
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(KotlinClosure2({ desc: TestDescriptor, result: TestResult ->
if (desc.parent == null) { // will match the outermost suite
val pass = "${Color.GREEN}${result.successfulTestCount} passed${Color.NONE}"
val fail = "${Color.RED}${result.failedTestCount} failed${Color.NONE}"
val skip = "${Color.YELLOW}${result.skippedTestCount} skipped${Color.NONE}"
val type = when (val r = result.resultType) {
ResultType.SUCCESS -> "${Color.GREEN}$r${Color.NONE}"
ResultType.FAILURE -> "${Color.RED}$r${Color.NONE}"
ResultType.SKIPPED -> "${Color.YELLOW}$r${Color.NONE}"
}
val output = "Results: $type (${result.testCount} tests, $pass, $fail, $skip)"
val startItem = "| "
val endItem = " |"
val repeatLength = startItem.length + output.length + endItem.length - 36
println("")
println("\n" + ("-" * repeatLength) + "\n" + startItem + output + endItem + "\n" + ("-" * repeatLength))
}
}))
}
onOutput(KotlinClosure2({ _: TestDescriptor, event: TestOutputEvent ->
if (event.destination == TestOutputEvent.Destination.StdOut) {
logger.lifecycle(event.message.replace(Regex("""\s+$"""), ""))
}
}))
}
operator fun String.times(x: Int): String {
return List(x) { this }.joinToString("")
}
internal enum class Color(ansiCode: Int) {
NONE(0),
BLACK(30),
RED(31),
GREEN(32),
YELLOW(33),
BLUE(34),
PURPLE(35),
CYAN(36),
WHITE(37);
private val ansiString: String = "\u001B[${ansiCode}m"
override fun toString(): String {
return ansiString
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment