Skip to content
This repository was archived by the owner on Dec 7, 2019. It is now read-only.

Commit 6b6e1b0

Browse files
Generate separate json files per test. (#31)
1 parent d14f3f0 commit 6b6e1b0

File tree

11 files changed

+224
-20
lines changed

11 files changed

+224
-20
lines changed

composer/build.gradle

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ dependencies {
2020

2121
dependencies {
2222
testCompile libraries.spek
23-
testCompile libraries.spekSubjectExtension
2423
testCompile libraries.spekJunitPlatformEngine
2524
testCompile libraries.assertJ
2625
}

composer/src/main/kotlin/com/gojuno/composer/html/HtmlFullSuite.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ data class HtmlFullSuite(
1111
val id: String,
1212

1313
@SerializedName("tests")
14-
val tests: List<HtmlTest>,
14+
val tests: List<HtmlShortTest>,
1515

1616
@SerializedName("passed_count")
1717
val passedCount: Int,
@@ -31,7 +31,7 @@ data class HtmlFullSuite(
3131

3232
fun Suite.toHtmlFullSuite(id: String) = HtmlFullSuite(
3333
id = id,
34-
tests = tests.map { it.toHtmlTest() },
34+
tests = tests.map { it.toHtmlFullTest().toHtmlShortTest() },
3535
passedCount = passedCount,
3636
ignoredCount = ignoredCount,
3737
failedCount = failedCount,

composer/src/main/kotlin/com/gojuno/composer/html/HtmlTest.kt composer/src/main/kotlin/com/gojuno/composer/html/HtmlFullTest.kt

+8-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import com.gojuno.composer.AdbDeviceTest
44
import com.google.gson.annotations.SerializedName
55
import java.util.concurrent.TimeUnit.NANOSECONDS
66

7-
data class HtmlTest(
7+
data class HtmlFullTest(
88

99
@SerializedName("package_name")
1010
val packageName: String,
@@ -15,6 +15,9 @@ data class HtmlTest(
1515
@SerializedName("name")
1616
val name: String,
1717

18+
@SerializedName("id")
19+
val id: String = "$packageName$className$name",
20+
1821
@SerializedName("duration_millis")
1922
val durationMillis: Long,
2023

@@ -52,15 +55,15 @@ data class HtmlTest(
5255
}
5356
}
5457

55-
fun AdbDeviceTest.toHtmlTest() = HtmlTest(
58+
fun AdbDeviceTest.toHtmlFullTest() = HtmlFullTest(
5659
packageName = className.substringBeforeLast("."),
5760
className = className.substringAfterLast("."),
5861
name = testName,
5962
durationMillis = NANOSECONDS.toMillis(durationNanos),
6063
status = when (status) {
61-
AdbDeviceTest.Status.Passed -> HtmlTest.Status.Passed
62-
AdbDeviceTest.Status.Ignored -> HtmlTest.Status.Ignored
63-
is AdbDeviceTest.Status.Failed -> HtmlTest.Status.Failed
64+
AdbDeviceTest.Status.Passed -> HtmlFullTest.Status.Passed
65+
AdbDeviceTest.Status.Ignored -> HtmlFullTest.Status.Ignored
66+
is AdbDeviceTest.Status.Failed -> HtmlFullTest.Status.Failed
6467
},
6568
stacktrace = when (status) {
6669
is AdbDeviceTest.Status.Failed -> status.stacktrace

composer/src/main/kotlin/com/gojuno/composer/html/HtmlReport.kt

+12-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ import com.google.gson.Gson
55
import rx.Completable
66
import java.io.File
77

8+
/**
9+
* Following file tree structure will be created:
10+
* - index.json
11+
* - suites/suiteId.json
12+
* - suites/deviceId/testId.json
13+
*/
814
fun writeHtmlReport(gson: Gson, suites: List<Suite>, outputDir: File): Completable = Completable.fromCallable {
915
outputDir.mkdirs()
1016

@@ -18,8 +24,11 @@ fun writeHtmlReport(gson: Gson, suites: List<Suite>, outputDir: File): Completab
1824

1925
val suitesDir = File(outputDir, "suites").apply { mkdirs() }
2026

21-
suites.mapIndexed { index, suite ->
22-
val suiteJson = gson.toJson(suite.toHtmlFullSuite(id = "$index"))
23-
File(suitesDir, "$index.json").writeText(suiteJson)
27+
suites.mapIndexed { suiteId, suite ->
28+
File(suitesDir, "$suiteId.json").writeText(gson.toJson(suite.toHtmlFullSuite(id = "$suiteId")))
29+
30+
suite.tests.map { it.toHtmlFullTest() }.forEach { htmlFullTest ->
31+
File(File(File(suitesDir, "$suiteId"), htmlFullTest.deviceId).apply { mkdirs() }, "${htmlFullTest.id}.json").writeText(gson.toJson(htmlFullTest))
32+
}
2433
}
2534
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.gojuno.composer.html
2+
3+
import com.google.gson.annotations.SerializedName
4+
5+
data class HtmlShortTest(
6+
7+
@SerializedName("id")
8+
val id: String,
9+
10+
@SerializedName("package_name")
11+
val packageName: String,
12+
13+
@SerializedName("class_name")
14+
val className: String,
15+
16+
@SerializedName("name")
17+
val name: String,
18+
19+
@SerializedName("duration_millis")
20+
val durationMillis: Long,
21+
22+
@SerializedName("status")
23+
val status: HtmlFullTest.Status,
24+
25+
@SerializedName("deviceId")
26+
val deviceId: String,
27+
28+
@SerializedName("properties")
29+
val properties: Map<String, Any>
30+
)
31+
32+
fun HtmlFullTest.toHtmlShortTest() = HtmlShortTest(
33+
id = id,
34+
packageName = packageName,
35+
className = className,
36+
name = name,
37+
durationMillis = durationMillis,
38+
status = status,
39+
deviceId = deviceId,
40+
properties = properties
41+
)

composer/src/test/kotlin/com/gojuno/composer/html/HtmlFullSuiteSpec.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class HtmlFullSuiteSpec : Spek({
5959
failedCount = suite.failedCount,
6060
durationMillis = NANOSECONDS.toMillis(suite.durationNanos),
6161
devices = suite.devices.map { it.toHtmlDevice() },
62-
tests = suite.tests.map { it.toHtmlTest() }
62+
tests = suite.tests.map { it.toHtmlFullTest().toHtmlShortTest() }
6363
))
6464
}
6565
}

composer/src/test/kotlin/com/gojuno/composer/html/HtmlTestSpec.kt composer/src/test/kotlin/com/gojuno/composer/html/HtmlFullTestSpec.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import org.jetbrains.spek.api.dsl.context
99
import org.jetbrains.spek.api.dsl.it
1010
import java.util.concurrent.TimeUnit.NANOSECONDS
1111

12-
class HtmlTestSpec : Spek({
12+
class HtmlFullTestSpec : Spek({
1313

1414
context("AdbDeviceTest.toHtmlTest") {
1515

@@ -24,14 +24,14 @@ class HtmlTestSpec : Spek({
2424
screenshots = listOf(testFile(), testFile())
2525
)
2626

27-
val htmlTest = adbDeviceTest.toHtmlTest()
27+
val htmlTest = adbDeviceTest.toHtmlFullTest()
2828

29-
it("converts AdbDeviceTest to HtmlTest") {
30-
assertThat(htmlTest).isEqualTo(HtmlTest(
29+
it("converts AdbDeviceTest to HtmlFullTest") {
30+
assertThat(htmlTest).isEqualTo(HtmlFullTest(
3131
packageName = "com.gojuno.example",
3232
className = "TestClass",
3333
name = adbDeviceTest.testName,
34-
status = HtmlTest.Status.Passed,
34+
status = HtmlFullTest.Status.Passed,
3535
durationMillis = NANOSECONDS.toMillis(adbDeviceTest.durationNanos),
3636
stacktrace = null,
3737
logcatPath = adbDeviceTest.logcat.path,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.gojuno.composer.html
2+
3+
import com.gojuno.commander.android.AdbDevice
4+
import com.gojuno.composer.AdbDeviceTest
5+
import com.gojuno.composer.Device
6+
import com.gojuno.composer.Suite
7+
import com.gojuno.composer.perform
8+
import com.google.gson.Gson
9+
import org.assertj.core.api.Assertions.assertThat
10+
import org.jetbrains.spek.api.Spek
11+
import org.jetbrains.spek.api.dsl.context
12+
import org.jetbrains.spek.api.dsl.it
13+
import rx.observers.TestSubscriber
14+
import java.io.File
15+
import java.util.concurrent.TimeUnit.MILLISECONDS
16+
import java.util.concurrent.TimeUnit.SECONDS
17+
18+
class HtmlReportSpec : Spek({
19+
20+
context("writeHtmlReport") {
21+
22+
val adbDevice1 = AdbDevice(
23+
id = "device1",
24+
online = true
25+
)
26+
27+
val suites = listOf(
28+
Suite(
29+
testPackage = "com.gojuno.example1",
30+
devices = listOf(Device(id = "device1", logcat = File("device1.logcat"), instrumentationOutput = File("device1.instrumentation"))),
31+
tests = listOf(
32+
AdbDeviceTest(
33+
adbDevice = adbDevice1,
34+
className = "com.gojuno.example1.TestClass",
35+
testName = "test1",
36+
durationNanos = MILLISECONDS.toNanos(1234),
37+
status = AdbDeviceTest.Status.Passed,
38+
logcat = File("com.gojuno.example1.TestClass", "test1.logcat"),
39+
files = listOf(File("com.gojuno.example1.TestClass.test1", "file1"), File("com.gojuno.example1.TestClass.test1", "file2")),
40+
screenshots = listOf(File("com.gojuno.example1.TestClass.test1", "screenshot1"), File("com.gojuno.example1.TestClass.test1", "screenshot2"))
41+
),
42+
AdbDeviceTest(
43+
adbDevice = adbDevice1,
44+
className = "com.gojuno.example1.TestClass",
45+
testName = "test2",
46+
durationNanos = MILLISECONDS.toNanos(1234),
47+
status = AdbDeviceTest.Status.Failed(stacktrace = "abc"),
48+
logcat = File("com.gojuno.example1.TestClass", "test2.logcat"),
49+
files = listOf(File("com.gojuno.example1.TestClass.test2", "file1"), File("com.gojuno.example1.TestClass.test2", "file2")),
50+
screenshots = listOf(File("com.gojuno.example1.TestClass.test2", "screenshot1"), File("com.gojuno.example1.TestClass.test2", "screenshot2"))
51+
)
52+
),
53+
passedCount = 2,
54+
ignoredCount = 0,
55+
failedCount = 1,
56+
durationNanos = MILLISECONDS.toNanos(1234 * 2),
57+
timestampMillis = 1805
58+
)
59+
)
60+
61+
val outputDir by memoized { File("${System.nanoTime()}") }
62+
63+
val subscriber = TestSubscriber<Unit>()
64+
65+
fun File.deleteOnExitRecursively() {
66+
when (isDirectory) {
67+
false -> deleteOnExit()
68+
true -> listFiles()?.forEach { inner -> inner.deleteOnExitRecursively()}
69+
}
70+
}
71+
72+
perform {
73+
writeHtmlReport(Gson(), suites, outputDir).subscribe(subscriber)
74+
subscriber.awaitTerminalEvent(5, SECONDS)
75+
outputDir.deleteOnExitRecursively()
76+
}
77+
78+
it("completes") {
79+
subscriber.assertCompleted()
80+
}
81+
82+
it("does not emit error") {
83+
subscriber.assertNoErrors()
84+
}
85+
86+
it("creates index.json") {
87+
assertThat(File(outputDir, "index.json").readText()).isEqualTo(
88+
"""{"suites":[{"id":"0","passed_count":2,"ignored_count":0,"failed_count":1,"duration_millis":2468,"devices":[{"id":"device1","logcat_path":"device1.logcat","instrumentation_output_path":"device1.instrumentation"}]}]}"""
89+
)
90+
}
91+
92+
it("creates suite json") {
93+
assertThat(File(File(outputDir, "suites"), "0.json").readText()).isEqualTo(
94+
"""{"id":"0","tests":[{"id":"com.gojuno.example1TestClasstest1","package_name":"com.gojuno.example1","class_name":"TestClass","name":"test1","duration_millis":1234,"status":"passed","deviceId":"device1","properties":{}},{"id":"com.gojuno.example1TestClasstest2","package_name":"com.gojuno.example1","class_name":"TestClass","name":"test2","duration_millis":1234,"status":"failed","deviceId":"device1","properties":{}}],"passed_count":2,"ignored_count":0,"failed_count":1,"duration_millis":2468,"devices":[{"id":"device1","logcat_path":"device1.logcat","instrumentation_output_path":"device1.instrumentation"}]}"""
95+
)
96+
}
97+
98+
it("creates json for 1st test") {
99+
assertThat(File(File(File(File(outputDir, "suites"), "0"), "device1"), "com.gojuno.example1TestClasstest1.json").readText()).isEqualTo(
100+
"""{"package_name":"com.gojuno.example1","class_name":"TestClass","name":"test1","id":"com.gojuno.example1TestClasstest1","duration_millis":1234,"status":"passed","logcat_path":"com.gojuno.example1.TestClass/test1.logcat","deviceId":"device1","properties":{},"file_paths":["com.gojuno.example1.TestClass.test1/file1","com.gojuno.example1.TestClass.test1/file2"],"screenshots_paths":["com.gojuno.example1.TestClass.test1/screenshot1","com.gojuno.example1.TestClass.test1/screenshot2"]}"""
101+
)
102+
}
103+
104+
it("creates json for 2nd test") {
105+
assertThat(File(File(File(File(outputDir, "suites"), "0"), "device1"), "com.gojuno.example1TestClasstest2.json").readText()).isEqualTo(
106+
"""{"package_name":"com.gojuno.example1","class_name":"TestClass","name":"test2","id":"com.gojuno.example1TestClasstest2","duration_millis":1234,"status":"failed","stacktrace":"abc","logcat_path":"com.gojuno.example1.TestClass/test2.logcat","deviceId":"device1","properties":{},"file_paths":["com.gojuno.example1.TestClass.test2/file1","com.gojuno.example1.TestClass.test2/file2"],"screenshots_paths":["com.gojuno.example1.TestClass.test2/screenshot1","com.gojuno.example1.TestClass.test2/screenshot2"]}"""
107+
)
108+
}
109+
}
110+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.gojuno.composer.html
2+
3+
import org.assertj.core.api.Assertions.assertThat
4+
import org.jetbrains.spek.api.Spek
5+
import org.jetbrains.spek.api.dsl.context
6+
import org.jetbrains.spek.api.dsl.it
7+
8+
class HtmlShortTestSpec : Spek({
9+
10+
context("HtmlFullTest.toHtmlShortTest") {
11+
12+
val htmlFullTest = HtmlFullTest(
13+
packageName = "com.gojuno.example",
14+
className = "TestClass",
15+
name = "test1",
16+
status = HtmlFullTest.Status.Passed,
17+
durationMillis = 1234,
18+
stacktrace = null,
19+
logcatPath = "testLogcatPath",
20+
filePaths = listOf("testFilePath1", "testFilePath2"),
21+
screenshotsPaths = listOf("testScreenshotPath1", "testScreenshotPath2"),
22+
deviceId = "test-device-id",
23+
properties = mapOf("key1" to "value1", "key2" to "value2")
24+
)
25+
26+
val htmlShortTest = htmlFullTest.toHtmlShortTest()
27+
28+
it("converts HtmlFullTest to HtmlShortTest") {
29+
assertThat(htmlShortTest).isEqualTo(HtmlShortTest(
30+
id = htmlFullTest.id,
31+
packageName = "com.gojuno.example",
32+
className = "TestClass",
33+
name = "test1",
34+
status = HtmlFullTest.Status.Passed,
35+
durationMillis = 1234,
36+
deviceId = "test-device-id",
37+
properties = mapOf("key1" to "value1", "key2" to "value2")
38+
))
39+
}
40+
}
41+
})

composer/src/test/kotlin/com/gojuno/composer/test.kt

+2
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ inline fun <reified C> fileFromJarResources(name: String) = File(C::class.java.c
88
fun testFile(): File = createTempFile().apply { deleteOnExit() }
99

1010
fun SpecBody.perform(body: () -> Unit) = beforeEachTest(body)
11+
12+
fun SpecBody.cleanup(body: () -> Unit) = afterEachTest(body)

dependencies.gradle

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ ext.versions = [
99
gson : '2.8.0',
1010

1111
junit : '4.12',
12-
junitPlatform : '1.0.0-M3',
13-
spek : '1.1.0',
12+
junitPlatform : '1.0.0-M4',
13+
spek : '1.1.2',
1414
assertJ : '3.5.2',
1515
]
1616

@@ -29,7 +29,6 @@ ext.libraries = [
2929

3030
junit : "junit:junit:$versions.junit",
3131
spek : "org.jetbrains.spek:spek-api:$versions.spek",
32-
spekSubjectExtension : "org.jetbrains.spek:spek-subject-extension:$versions.spek",
3332
spekJunitPlatformEngine: "org.jetbrains.spek:spek-junit-platform-engine:$versions.spek",
3433
assertJ : "org.assertj:assertj-core:$versions.assertJ",
3534
]

0 commit comments

Comments
 (0)