Share
Embed
「GigaViewer For Apps」の単体テストを振り返る
kk__777
December 11, 2024
Transcript
31 slides
-
自己紹介 kk__777 (けーけー) 木村 洸太 株式会社はてな GigaViewer For Apps コミックDAYS
kubell.mobile #2 2 -
アーキテクチャ 公式 + Apollo Kotlin ViewModelが直接 Apollo を扱う ことが多い 詳しくは
以下 を見てね https://speakerdeck.com/numero anddev/graphqltogigaviewer-for- apps kubell.mobile #2 4 -
Spek いわゆる テストスイート- テストケース を構造化して 自然言語で書いていける object CalculatorSpec: Spek({ describe("A
calculator") { val calculator by memoized { Calculator() } describe("addition") { it("returns the sum of its arguments") { assertEquals(3, calculator.add(1, 2)) } } } }) kubell.mobile #2 5 -
Gherkin 記法 object SetFeature: Spek({ Feature("Set") { val set by
memoized { mutableSetOf<String>() } Scenario("adding items") { When("adding foo") { set.add("foo") } Then("it should have a size of 1") { assertEquals(1, set.size) } Then("it should contain foo") { assertTrue(set.contains("foo")) } } kubell.mobile #2 6 -
Spek だんだんと障害が出てくる メンテがされなくなる https://github.com/spekframework/spek/issues/959 IDEで 実行できなくなったので いちいちコマンド実行のストレス Robolectric は動かない Composeのテストは変わらずJUnit4
ちょっとしたAndroid依存もモックが必要 kubell.mobile #2 9 -
class MyTests : DescribeSpec({ describe("score") { it("start as zero") {
// test here } describe("with a strike") { it("adds ten") { // test here } it("carries strike to the next frame") { // test here } } describe("for the opposite team") { it("Should negate one score") { // test here } } } }) kubell.mobile #2 11 -
Kotest kotest-extensions-robolectric https://github.com/kotest/kotest-extensions-robolectric @RobolectricTest class ContainedRobolectricRunnerDefaultApplicationTest : StringSpec({ "Get the
Application defined in AndroidManifest.xml" { ApplicationProvider.getApplicationContext<Application>()::class shouldBe MainApplication::class } }) ちょっとしたAndroid依存 を 使えるように Composeまでは.. Ruleが使えないので かけないはず kubell.mobile #2 12 -
JUnit4の @Enclosed で構造化を残す術もありそうで、 シェルでサッとできるかも しれないが.. classファイルにコンパイルした際に ファイル名 長すぎて Gradle では落ちる
とい うissueがある https://github.com/gradle/gradle/issues/21547 kubell.mobile #2 16 -
というわけで現状 基本的にはJUnit4 + AndroidJunit4Runner を使ってる Assert は Truth モック,スタブ は
Mockk 一部 Composeも、 Robolectric で UnitTest空間でテスト kubell.mobile #2 18 -
Truth value of : createSummary(...) keys with wrong values for
key : Alice expected value : 2 but got value : 1 unexpected keys for key : Alic unexpected value: 1 expected : {Alice=2, Bob=1} but was : {Alic=1, Bob=1, Alice=1} value of : createSummary(...) keys with wrong values for key : Alice expected value : 2 but got value : 1 unexpected keys for key : Alic unexpected value: 1 expected : {Alice=2, Bob=1} but was : {Alic=1, Bob=1, Alice=1} at sample.ComplexLogicTestTruth.testCreateSummary(ComplexTest.kt:34) kubell.mobile #2 23 -
powerAssert Unexpected summary: {Alie=1, Bob=1, Alice=1} assert(summary == mapOf("Alice" to
2, "Bob" to 1)) { "Unexpected summary: $summary" } | | | | | | | | | (Bob, 1) | | | (Alice, 2) | | {Alice=2, Bob=1} | false {Alie=1, Bob=1, Alice=1} java.lang.AssertionError: Unexpected summary: {Alie=1, Bob=1, Alice=1} assert(summary == mapOf("Alice" to 2, "Bob" to 1)) { "Unexpected summary: $summary" } | | | | | | | | | (Bob, 1) | | | (Alice, 2) | | {Alice=2, Bob=1} | false {Alie=1, Bob=1, Alice=1} kubell.mobile #2 24 -
kotlin.test assert(summary == mapOf("Alice" to 2, "Bob" to 1)) {
"Unexpected summary: $summary" } Truth assertThat(logic.createSummary(people)).containsExactlyEntriesIn( mapOf("Alice" to 2, "Bob" to 1) ) kubell.mobile #2 25 -
テストピラミッドで見 る現状 下三つは JUnit Component Testは VRT も 上は 手打鍵
, QAチーム kubell.mobile #2 26 -
ピラミッドの上をやっていくなら Kotest なり Cucumber なり、 BDD フレームワーク で Instrumentation Test
も ありかも Cucumber: https://cucumber.io/docs/installation/ kubell.mobile #2 29