KotlinTest で始める Property-based Testing/kotlintest-property-based-testing

KotlinTest で始める Property-based Testing/kotlintest-property-based-testing

70d9714ea13fc1133803d61fb16e4160?s=128

Yusuke Hosonuma

August 24, 2019
Tweet

Transcript

  1. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. KotlinTest Ͱ࢝ΊΔ Property-based

    Testing Yusuke Hosonuma @DeNA Kotlin Fest 2019
  2. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. Yusuke Hosonuma @DeNA

    Swift / Haskell / Go Twitter @tobi462
  3. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. SoftWare Engineer in

    Test ςετࣗಈԽʢiOS / Android / Goʣ CI/CDɺςετٕज़ͷݚڀ
  4. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. ࣥච $PNNJOHTPPOʜ

  5. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. Agenda • KotlinTest

    ͬͯʁ • Property-based Testing ͬͯʁ • ͲΜͳͱ͖ʹ࢖͑Δʁ • ·ͱΊ ͳΜͱͳ͘ཧղ͕ΰʔϧ
  6. KotlinTest ͬͯʁ Copyright (C) DeNA Co.,Ltd. All Rights Reserved. ·ͣ

  7. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. KotlinTest

  8. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. KotlinTest ଟ͘ͷػೳΛαϙʔτ͢Δ ςεςΟϯάϑϨʔϜϫʔΫ

  9. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. ςεςΟϯάελΠϧ String Spec

    Behavior Spec Feature Spec #%%
  10. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. ϚονϟʔAPI ʴ

  11. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. ςʔϒϧۦಈςετ σʔλΛࣄલʹྻڍ

  12. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. ςʔϒϧۦಈςετ σʔλΛࣄલʹྻڍ ΘΓͱશ෦੝Γͳ

    ςεςΟϯάϑϨʔϜϫʔΫ ͱ͍͏ҹ৅
  13. Property-based Testing ͬͯʁ Copyright (C) DeNA Co.,Ltd. All Rights Reserved.

    ͡Ό͋
  14. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. Property-based Testing •

    ςετσʔλΛϥϯμϜʹੜ੒ͤ͞ • ؔ਺͕ຬͨ͢ੑ࣭ʢPropertyʣΛݕূ͢Δ
  15. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. Property-based Testing •

    ςετσʔλΛϥϯμϜʹੜ੒ͤ͞ • ͦͷؔ਺͕ຬͨ͢ੑ࣭ʢPropertyʣΛݕূ͢Δ ͳΔ΄ͲɺΘ͔ΒΜɻ ྫΛݟͯΈΔ
  16. ଍͠ࢉʢʴʣͷ ςετͰൺֱ Copyright (C) DeNA Co.,Ltd. All Rights Reserved.

  17. Example-based Testing Copyright (C) DeNA Co.,Ltd. All Rights Reserved. ීஈ΍ͬͯΔ

  18. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. 1 + 1

    = 2 / 2 + 3 = 5 class AddSpec: StringSpec({ "add" { forall( row(1, 1, 2), row(2, 3, 5) ) { a, b, expected -> (a + b) shouldBe expected } } })
  19. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. class AddSpec: StringSpec({

    "add" { forall( row(1, 1, 2), row(2, 3, 5) ) { a, b, expected -> (a + b) shouldBe expected } } }) ೖྗ஋ͱظ଴஋Λ༻ҙͯ͠ 1 + 1 = 2 / 2 + 3 = 5
  20. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. class AddSpec: StringSpec({

    "add" { forall( row(1, 1, 2), row(2, 3, 5) ) { a, b, expected -> (a + b) shouldBe expected } } }) ݁Ռͱظ଴஋͕Ұக͢Δ͔ʁ 1 + 1 = 2 / 2 + 3 = 5
  21. Property-based Testing Copyright (C) DeNA Co.,Ltd. All Rights Reserved. Ͱ͸ʁ

  22. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. ੑ࣭Λߟ͑Δ a +

    b = b + a ަ׵๏ଇΛຬͨ͢
  23. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. ੑ࣭Λߟ͑Δ a +

    b = b + a ʁ ʁ ͲΜͳ஋Ͱ΋੒Γཱͭ
  24. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. KotlinTestͰݕূ class AddProperty:

    StringSpec({ "commutative" { assertAll { a: Int, b: Int -> (a + b) shouldBe (b + a) } } })
  25. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. KotlinTestͰݕূ class AddProperty:

    StringSpec({ “commutative" { assertAll { a: Int, b: Int -> (a + b) shouldBe (b + a) } } }) ͢΂ͯͷ
  26. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. KotlinTestͰݕূ class AddProperty:

    StringSpec({ “commutative" { assertAll { a: Int, b: Int -> (a + b) shouldBe (b + a) } } }) औΓ͏Δ஋BͱCʹ͍ͭͯ
  27. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. KotlinTestͰݕূ class AddProperty:

    StringSpec({ "commutative" { assertAll { a: Int, b: Int -> (a + b) shouldBe (b + a) } } }) B CC BͰ͋Δ͜ͱ
  28. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. KotlinTestͰݕূ class AddProperty:

    StringSpec({ "commutative" { assertAll { a: Int, b: Int -> (a + b) shouldBe (b + a) } } }) B CC BͰ͋Δ͜ͱ ϥϯμϜͳೖྗ஋ʹରͯ͠ ຬͨ͞ΕΔ΂͖ੑ࣭Λݕূ ৗʹ
  29. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. debug print ͯ͠ΈΔͱ

    ཚ਺Ͱ ճࢼߦ͞ΕͯΔ
  30. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. debug print ͯ͠ΈΔͱ

    ཚ਺Ͱ ճࢼߦ͞ΕͯΔ ύε͠ͳ͍σʔλ͕ͳ͍͔ େྔͷཚ਺Ͱ͋ͿΓग़͢ ͱߟ͑Δͱ෼͔Γ΍͍͢
  31. ͲΜͳ࣌ʹ࢖͑Δʁ Copyright (C) DeNA Co.,Ltd. All Rights Reserved. Ͱ͸

  32. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. ࢖͍Ͳ͜Ζ 1. ཚ਺Λར༻ͨ͠ΞϧΰϦζϜ

    2. ରশੑͷ͋ΔΞϧΰϦζϜ 3. ߴ଎ vs ௿଎
  33. ཚ਺Λར༻ͨ͠ΞϧΰϦζϜ Copyright (C) DeNA Co.,Ltd. All Rights Reserved. 

  34. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. ໎࿏ੜ੒ • ͲΜͳ໎࿏Ͱ΋ղ͚ͳ͍ͱ੒ཱ͠ͳ͍

    • ʮඞͣղ͚Δʯͱ͍͏ੑ࣭Λݕূ͢Δ
  35. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. ໎࿏ੜ੒ • ͲΜͳ໎࿏Ͱ΋ղ͚ͳ͍ͱ੒ཱ͠ͳ͍

    • ٯʹݴ͑͹ղ͚ͳ͍ͱόά • ʮඞͣղ͚Δʯͱ͍͏ੑ࣭Λݕূ͢Δ ྫ͑͹ Monkey Runner ΋ ϥϯμϜͳΠϕϯτΛൃߦ͠ଓ͚ ʮམͪͳ͍ʯੑ࣭Λݕূ͢Δ "OESPJEͷ
  36. ରশੑͷ͋ΔΞϧΰϦζϜ Copyright (C) DeNA Co.,Ltd. All Rights Reserved. 

  37. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. Τϯίʔυ / σίʔυ

    • ͲΜͳ஋ΛΤϯίʔυ → σίʔυ ͯ͠΋ • ඞͣʮݩͷ஋ʹ໭Δʯͱ͍͏ੑ࣭Λݕূ
  38. ߴ଎ vs ௿଎ Copyright (C) DeNA Co.,Ltd. All Rights Reserved.

    
  39. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. ߴ଎ vs ௿଎

    • ߴ଎ͳίʔυ͸଎͍ʢ͚Ͳෳࡶʣ • ௿଎ͳίʔυ͸໌നʢ͚Ͳ஗͍ʣ • ͲͪΒ΋ʮಉ݁͡ՌʹͳΔʯͱ͍͏ੑ࣭Λར༻ • લऀͷݕূʹޙऀΛར༻͢Δ
  40. ·ͱΊ Copyright (C) DeNA Co.,Ltd. All Rights Reserved.

  41. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. ·ͱΊ • KotlinTest

    ͸ ςεςΟϯάϑϨʔϜϫʔΫ • Property-based Testing ΋αϙʔτ • ϥϯμϜͳೖྗΛݩʹੑ࣭Λݕূ • Example-based Λஔ͖׵͑Δ΋ͷͰ͸ͳ͍
  42. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. ·ͱΊ • KotlinTest

    ͸ DSL Λ׆༻ͨ͠ςετϥΠϒϥϦ • Property-based Testing ΋αϙʔτ • ϥϯμϜͳೖྗΛݩʹੑ࣭Λݕূ • Example-based Λஔ͖׵͑Δ΋ͷͰ͸ͳ͍ ͱ͖ͬͭͮΒ͍ߟ͑ํ͕ͩ ͓֮͑ͯ͘ͱڧྗͳΧʔυ ࠷ॳ͸
  43. એ఻ Copyright (C) DeNA Co.,Ltd. All Rights Reserved.

  44. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. ͲͳͨͰ΋ొஃ0,ʂ

  45. Copyright (C) DeNA Co.,Ltd. All Rights Reserved. ͲͳͨͰ΋ొஃ0,ʂ DeNA ʹͯ

    9/9ʢ݄ʣ 19:00 ʙ
  46. Let’s try Property-based Testing Copyright (C) DeNA Co.,Ltd. All Rights

    Reserved. XJUI,PUMJO5FTU