Upgrade to Pro — share decks privately, control downloads, hide ads and more …

「GigaViewer For Apps」の単体テストを振り返る

kk__777
December 11, 2024
60

「GigaViewer For Apps」の単体テストを振り返る

kk__777

December 11, 2024
Tweet

Transcript

  1. アーキテクチャ 公式 + Apollo Kotlin ViewModelが直接 Apollo を扱う ことが多い 詳しくは

    以下 を見てね https://speakerdeck.com/numero anddev/graphqltogigaviewer-for- apps kubell.mobile #2 4
  2. 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
  3. 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
  4. 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
  5. 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
  6. というわけで現状 基本的にはJUnit4 + AndroidJunit4Runner を使ってる Assert は Truth モック,スタブ は

    Mockk 一部 Composeも、 Robolectric で UnitTest空間でテスト kubell.mobile #2 18
  7. 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
  8. 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
  9. 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
  10. ピラミッドの上をやっていくなら Kotest なり Cucumber なり、 BDD フレームワーク で Instrumentation Test

    も ありかも Cucumber: https://cucumber.io/docs/installation/ kubell.mobile #2 29