Skip to content

Commit

Permalink
add gradle script that helps showing better output when running tests…
Browse files Browse the repository at this point in the history
… using the cli
  • Loading branch information
Souleymane Sidibe committed Aug 19, 2021
1 parent 502c375 commit f047a63
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ plugins {
id("kotlin-android")
}

apply {
from(file("$rootDir/gradle_tests_report.gradle.kts"))
}

android {
compileSdk = 30
buildToolsVersion = "30.0.3"
Expand Down
4 changes: 4 additions & 0 deletions data/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ plugins {
id("kotlin")
}

apply {
from(file("$rootDir/gradle_tests_report.gradle.kts"))
}

java {
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
Expand Down
3 changes: 3 additions & 0 deletions device/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ plugins {
id 'kotlin-android'
id 'kotlin-kapt'
}
apply {
from(file("$rootDir/gradle_tests_report.gradle.kts"))
}

android {
compileSdk 30
Expand Down
3 changes: 3 additions & 0 deletions domain/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ plugins {
id 'java-library'
id 'kotlin'
}
apply {
from(file("$rootDir/gradle_tests_report.gradle.kts"))
}

java {
sourceCompatibility = JavaVersion.VERSION_1_7
Expand Down
91 changes: 91 additions & 0 deletions gradle_tests_report.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent
import groovy.time.TimeCategory
import java.util.Date

/**
* based on the groovy code by lwasyl:
* https://gist.github.com/lwasyl/f5b2b4ebe9e348ebbd8ee4cb995f8362
* https://medium.com/@wasyl/pretty-tests-summary-in-gradle-744804dd676c
*/
var testResults by extra(mutableListOf<TestOutcome>()) // Container for tests summaries

tasks.withType<Test>().configureEach {
val testTask = this

testLogging {
events = setOf(
TestLogEvent.FAILED,
TestLogEvent.SKIPPED,
TestLogEvent.STANDARD_OUT,
TestLogEvent.STANDARD_ERROR
)

exceptionFormat = TestExceptionFormat.FULL
showExceptions = true
showCauses = true
showStackTraces = true
}

ignoreFailures = true // Always try to run all tests for all modules

//addTestListener is a workaround https://github.com/gradle/kotlin-dsl-samples/issues/836
addTestListener(object : TestListener {
override fun beforeSuite(suite: TestDescriptor) {}
override fun beforeTest(testDescriptor: TestDescriptor) {}
override fun afterTest(testDescriptor: TestDescriptor, result: TestResult) {}
override fun afterSuite(desc: TestDescriptor, result: TestResult) {
if (desc.parent != null) return // Only summarize results for whole modules

val summary = TestOutcome().apply {
add( "${testTask.project.name}:${testTask.name} results: ${result.resultType} " +
"(" +
"${result.testCount} tests, " +
"${result.successfulTestCount} successes, " +
"${result.failedTestCount} failures, " +
"${result.skippedTestCount} skipped" +
") " +
"in ${TimeCategory.minus(Date(result.endTime), Date(result.startTime))}")
add("Report file: ${testTask.reports.html.entryPoint}")
}

// Add reports in `testsResults`, keep failed suites at the end
if (result.resultType == TestResult.ResultType.SUCCESS) {
testResults.add(0, summary)
} else {
testResults.add(summary)
}
}
})
}

gradle.buildFinished {
if (testResults.isNotEmpty()) {
printResults(testResults)
}
}

fun printResults(allResults:List<TestOutcome>) {
val maxLength = allResults.map{ it.maxWidth() }
.maxOrNull() ?: 0

println("${"".repeat(maxLength)}")

println(allResults.joinToString("${"".repeat(maxLength)}\n") { testOutcome ->
testOutcome.lines.joinToString("\n", "", "") {
it + " ".repeat(maxLength - it.length)
}
})

println("${"".repeat(maxLength)}")
}

data class TestOutcome(val lines:MutableList<String> = mutableListOf()) {
fun add(line:String) {
lines.add(line)
}

fun maxWidth(): Int {
return lines.maxByOrNull { it.length }?.length ?: 0
}
}

0 comments on commit f047a63

Please sign in to comment.