Skip to content

Instantly share code, notes, and snippets.

@serpro69
Last active November 13, 2024 16:26
Show Gist options
  • Select an option

  • Save serpro69/c987a4016fb59f6ca2ff9f8d8464561d to your computer and use it in GitHub Desktop.

Select an option

Save serpro69/c987a4016fb59f6ca2ff9f8d8464561d to your computer and use it in GitHub Desktop.

Revisions

  1. serpro69 revised this gist Nov 4, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion build.gradle.kts
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ import org.gradle.api.tasks.testing.logging.TestLogEvent
    tasks.test {
    useJUnitPlatform()
    maxParallelForks = 1
    testLogging {
    testLogging { // credits: https://stackoverflow.com/a/36130467/5917497
    // set options for log level LIFECYCLE
    events = setOf(
    TestLogEvent.FAILED,
  2. serpro69 created this gist Nov 4, 2022.
    82 changes: 82 additions & 0 deletions build.gradle.kts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    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 {
    // 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
    }
    }