Skip to content

Commit

Permalink
Add test for JsonTestExecutionReportFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
platan committed Nov 1, 2023
1 parent bc42671 commit 9898387
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ abstract class TestExecutionResultsRegisterService : BuildService<BuildServicePa
resultType: String,
type: TimedTestResult.Type
) {
val taskResults = results.getOrPut(task) { TestExecutionScheduleReportBuilder() }
taskResults.add(className, testName, startTime, endTime, resultType, type)
val reportBuilder = results.getOrPut(task) { TestExecutionScheduleReportBuilder() }
reportBuilder.addResult(className, testName, startTime, endTime, resultType, type)
}
}

Expand All @@ -35,6 +35,6 @@ abstract class TestExecutionResultsRegisterService : BuildService<BuildServicePa
fun getResults(module: Path?): Map<Test, TestExecutionScheduleReport> {
val reportBuilders =
if (module == null) register.results else register.results.filterKeys { it.identityPath.parent == module }
return reportBuilders.mapValues { it.value.getResults() }
return reportBuilders.mapValues { it.value.build() }
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
package io.github.platan.tests_execution_chart.report

import io.github.platan.tests_execution_chart.report.data.Mark
import io.github.platan.tests_execution_chart.report.data.TestExecutionScheduleReport
import io.github.platan.tests_execution_chart.report.data.TimedTestResult

class TestExecutionScheduleReportBuilder {
private var results = mutableListOf<TimedTestResult>()
fun add(
private var marks = mutableListOf<Mark>()
fun addResult(
className: String?,
testName: String,
startTime: Long,
endTime: Long,
resultType: String,
type: TimedTestResult.Type
) {
): TestExecutionScheduleReportBuilder {
results.add(TimedTestResult(className, testName, startTime, endTime, resultType, type))
return this
}

fun getResults(): TestExecutionScheduleReport {
return TestExecutionScheduleReport(results)
fun addMark(name: String, timestamp: Long): TestExecutionScheduleReportBuilder {
marks.add(Mark(name, timestamp))
return this
}

fun build(): TestExecutionScheduleReport {
return TestExecutionScheduleReport(results, marks)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import io.github.platan.tests_execution_chart.report.TestExecutionScheduleReport
import spock.lang.Specification
import spock.lang.Subject

import java.time.Instant

import static io.github.platan.tests_execution_chart.report.data.TimedTestResult.Type.TEST

class JsonTestExecutionReportFormatterTest extends Specification {

@Subject
def formatter = new JsonTestExecutionReportFormatter()

def "should format empty report"() {
given:
def emptyReport = new TestExecutionScheduleReportBuilder().getResults()
def emptyReport = new TestExecutionScheduleReportBuilder().build()

when:
def result = formatter.format(emptyReport)
Expand All @@ -26,4 +30,40 @@ class JsonTestExecutionReportFormatterTest extends Specification {
|}""".stripMargin()
}

def "should format report with one entry and one mark"() {
given:
def reportBuilder = new TestExecutionScheduleReportBuilder()
.addResult('class', 'test', toEpochMilli('2023-03-10T19:00:02Z'), toEpochMilli('2023-03-10T19:00:05Z'), 'passed', TEST)
.addMark('mark1', toEpochMilli('2023-03-10T19:00:05Z'))
def report = reportBuilder.build()

when:
def result = formatter.format(report)

then:
result ==
"""{
| "results": [
| {
| "className": "class",
| "testName": "test",
| "startTime": 1678474802000,
| "endTime": 1678474805000,
| "resultType": "passed",
| "type": "TEST"
| }
| ],
| "marks": [
| {
| "name": "mark1",
| "timestamp": 1678474805000
| }
| ]
|}""".stripMargin()
}

private static long toEpochMilli(String instant) {
Instant.parse(instant).toEpochMilli()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ class TestExecutionMermaidDiagramFormatterSpec extends Specification {
def getDefault = TimeZone.getDefault()
TimeZone.setDefault(SimpleTimeZone.getTimeZone(ZoneOffset.ofHours(2)))
def reportBuilder = new TestExecutionScheduleReportBuilder()
reportBuilder.add('Test1', 'test1', 1681402397000, 1681402397100, 'SUCCESS', TEST)
reportBuilder.add('Test1', 'test2', 1681402397100, 1681402397300, 'SUCCESS', TEST)
reportBuilder.add('Test1', 'suite', 1681402397000, 1681402397300, 'SUCCESS', SUITE)
reportBuilder.add('Test2', 'test1', 1681402397000, 1681402397100, 'FAILURE', TEST)
reportBuilder.add('Test2', 'suite', 1681402397000, 1681402397300, 'FAILURE', SUITE)
reportBuilder.add('Test3', 'test1', 1681402397100, 1681402397300, 'SKIPPED', TEST)
reportBuilder.add('Test3', 'suite', 1681402397000, 1681402397300, 'SKIPPED', SUITE)
def report = reportBuilder.getResults()
.addResult('Test1', 'test1', 1681402397000, 1681402397100, 'SUCCESS', TEST)
.addResult('Test1', 'test2', 1681402397100, 1681402397300, 'SUCCESS', TEST)
.addResult('Test1', 'suite', 1681402397000, 1681402397300, 'SUCCESS', SUITE)
.addResult('Test2', 'test1', 1681402397000, 1681402397100, 'FAILURE', TEST)
.addResult('Test2', 'suite', 1681402397000, 1681402397300, 'FAILURE', SUITE)
.addResult('Test3', 'test1', 1681402397100, 1681402397300, 'SKIPPED', TEST)
.addResult('Test3', 'suite', 1681402397000, 1681402397300, 'SKIPPED', SUITE)
def report = reportBuilder.build()

when:
def mermaidReport = new TestExecutionMermaidDiagramFormatter().format(report)
Expand Down Expand Up @@ -55,10 +55,10 @@ suite - 300 ms :2023-04-13T18:13:17.000+0200, 2023-04-13T18:13:17.300+0200
def getDefault = TimeZone.getDefault()
TimeZone.setDefault(SimpleTimeZone.getTimeZone(ZoneOffset.ofHours(2)))
def reportBuilder = new TestExecutionScheduleReportBuilder()
reportBuilder.add('Test1', 'test1', 1681402397000, 1681402397100, 'SUCCESS', TEST)
reportBuilder.add('Test1', 'test2', 1681402397100, 1681402397300, 'SUCCESS', TEST)
reportBuilder.add('Test2', 'test1', 1681402397000, 1681402397100, 'SUCCESS', TEST)
def results = reportBuilder.getResults()
.addResult('Test1', 'test1', 1681402397000, 1681402397100, 'SUCCESS', TEST)
.addResult('Test1', 'test2', 1681402397100, 1681402397300, 'SUCCESS', TEST)
.addResult('Test2', 'test1', 1681402397000, 1681402397100, 'SUCCESS', TEST)
def results = reportBuilder.build()
def report = results.copy(results.results, [new Mark('mark1', 1681402397400)])

when:
Expand Down

0 comments on commit 9898387

Please sign in to comment.