Skip to content

Commit a71c6a4

Browse files
committed
Adding Github summary action for Java test
1 parent 0bbc619 commit a71c6a4

File tree

1 file changed

+66
-12
lines changed

1 file changed

+66
-12
lines changed

backend/build.gradle.kts

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
import org.gradle.api.tasks.testing.TestResult.ResultType
2+
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
3+
import org.gradle.api.tasks.testing.logging.TestLogEvent
4+
15
import java.text.SimpleDateFormat
26
import java.time.Instant
7+
import java.io.FileWriter
38
import java.util.*
49

10+
511
group = "ai.giskard"
612
description = "Giskard main java backend"
713

@@ -267,9 +273,12 @@ tasks {
267273
test {
268274
exclude("**/*IT*", "**/*IntTest*")
269275
testLogging {
270-
events.add(org.gradle.api.tasks.testing.logging.TestLogEvent.FAILED)
271-
events.add(org.gradle.api.tasks.testing.logging.TestLogEvent.SKIPPED)
276+
events = setOf(
277+
TestLogEvent.FAILED,
278+
TestLogEvent.SKIPPED
279+
)
272280
}
281+
273282
jvmArgs?.add("-Djava.security.egd=file:/dev/./urandom -Xmx256m")
274283
reports.html.required.set(false)
275284
}
@@ -282,17 +291,70 @@ tasks {
282291
description = "Execute integration tests."
283292
group = "verification"
284293
include("**/*IT*", "**/*IntTest*")
294+
285295
testLogging {
286296
events = setOf(
287-
org.gradle.api.tasks.testing.logging.TestLogEvent.FAILED,
288-
org.gradle.api.tasks.testing.logging.TestLogEvent.SKIPPED
297+
TestLogEvent.FAILED,
298+
TestLogEvent.SKIPPED
289299
)
290300
}
301+
// Improvement ?
302+
// https://stackoverflow.com/questions/3963708/gradle-how-to-display-test-results-in-the-console-in-real-time#answer-36130467
303+
// https://gist.github.com/serpro69/c987a4016fb59f6ca2ff9f8d8464561d
304+
291305
jvmArgs?.add("-Djava.security.egd=file:/dev/./urandom -Xmx256m")
292306
if (project.hasProperty("testcontainers")) {
293307
environment = mapOf("spring.profiles.active" to "testcontainers")
294308
}
295309
reports.html.required.set(false)
310+
311+
beforeSuite(KotlinClosure0({ ->
312+
val report_path = System.getenv("GITHUB_STEP_SUMMARY") ?: "local_report.md"
313+
val file = File(report_path)
314+
if(file.exists()) {
315+
file.delete();
316+
}
317+
}))
318+
319+
afterTest(KotlinClosure2({ testDescriptor: TestDescriptor, result: TestResult ->
320+
// https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/testing/TestResult.html$
321+
// https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/testing/TestFailure.html
322+
if (result.getResultType() == ResultType.FAILURE) {
323+
val report_path = System.getenv("GITHUB_STEP_SUMMARY") ?: "local_report.md"
324+
var report = File(report_path)
325+
var test_name = testDescriptor.getName()
326+
report.appendText("\n\n#### ${testDescriptor}\n\n")
327+
328+
for (failure in result.getFailures()) {
329+
report.appendText("<details>\n")
330+
report.appendText("<summary>${failure.getDetails().getMessage() ?: ""}</summary>\n\n")
331+
report.appendText("\n\n```java\n")
332+
report.appendText(failure.getDetails().getStacktrace() ?: "")
333+
report.appendText("\n```\n")
334+
report.appendText("\n</details>\n\n")
335+
336+
}
337+
}
338+
}))
339+
340+
afterSuite(KotlinClosure2({ desc: TestDescriptor, result: TestResult ->
341+
if (desc.parent == null) { // will match the outermost suite
342+
val report_path = System.getenv("GITHUB_STEP_SUMMARY") ?: "local_report.md"
343+
val report_content = if (File(report_path).exists()) File(report_path).readText() else ""
344+
FileWriter(report_path).use {
345+
it.write("### Gradle Test report\n")
346+
it.write("Test results: ${result.resultType}\n")
347+
it.write("\n| Status |Count |")
348+
it.write("\n| :---: | :---: |")
349+
it.write("\n| Successful | ${result.successfulTestCount} |")
350+
it.write("\n| Failed | ${result.failedTestCount} |")
351+
it.write("\n| Skipped | ${result.skippedTestCount} |")
352+
it.write("\n| Skipped | ${result.skippedTestCount} |\n\n")
353+
it.write(report_content)
354+
}
355+
356+
}
357+
}))
296358
}
297359

298360
create<TestReport>("testReport") {
@@ -364,11 +426,3 @@ tasks {
364426
}
365427

366428
defaultTasks("bootRun")
367-
368-
369-
370-
371-
372-
373-
374-

0 commit comments

Comments
 (0)