Yusuke Hosonuma
August 24, 2019
1.3k

# KotlinTest で始める Property-based Testing/kotlintest-property-based-testing

August 24, 2019

## Transcript

Testing Yusuke Hosonuma @DeNA Kotlin Fest 2019

Test ςετࣗಈԽʢiOS / Android / Goʣ CI/CDɺςετٕज़ͷݚڀ

ͬͯʁ • Property-based Testing ͬͯʁ • ͲΜͳͱ͖ʹ࢖͑Δʁ • ·ͱΊ ͳΜͱͳ͘ཧղ͕ΰʔϧ

Behavior Spec Feature Spec #%%

ςεςΟϯάϑϨʔϜϫʔΫ ͱ͍͏ҹ৅

͡Ό͋

ςετσʔλΛϥϯμϜʹੜ੒ͤ͞ • ؔ਺͕ຬͨ͢ੑ࣭ʢPropertyʣΛݕূ͢Δ

ςετσʔλΛϥϯμϜʹੜ੒ͤ͞ • ͦͷؔ਺͕ຬͨ͢ੑ࣭ʢPropertyʣΛݕূ͢Δ ͳΔ΄ͲɺΘ͔ΒΜɻ ྫΛݟͯΈΔ

= 2 / 2 + 3 = 5 class AddSpec: StringSpec({ "add" { forall( row(1, 1, 2), row(2, 3, 5) ) { a, b, expected -> (a + b) shouldBe expected } } })

"add" { forall( row(1, 1, 2), row(2, 3, 5) ) { a, b, expected -> (a + b) shouldBe expected } } }) ೖྗ஋ͱظ଴஋Λ༻ҙͯ͠ 1 + 1 = 2 / 2 + 3 = 5

"add" { forall( row(1, 1, 2), row(2, 3, 5) ) { a, b, expected -> (a + b) shouldBe expected } } }) ݁Ռͱظ଴஋͕Ұக͢Δ͔ʁ 1 + 1 = 2 / 2 + 3 = 5

b = b + a ަ׵๏ଇΛຬͨ͢

b = b + a ʁ ʁ ͲΜͳ஋Ͱ΋੒Γཱͭ

StringSpec({ "commutative" { assertAll { a: Int, b: Int -> (a + b) shouldBe (b + a) } } })

StringSpec({ “commutative" { assertAll { a: Int, b: Int -> (a + b) shouldBe (b + a) } } }) ͢΂ͯͷ

StringSpec({ “commutative" { assertAll { a: Int, b: Int -> (a + b) shouldBe (b + a) } } }) औΓ͏Δ஋BͱCʹ͍ͭͯ

StringSpec({ "commutative" { assertAll { a: Int, b: Int -> (a + b) shouldBe (b + a) } } }) B CC BͰ͋Δ͜ͱ

StringSpec({ "commutative" { assertAll { a: Int, b: Int -> (a + b) shouldBe (b + a) } } }) B CC BͰ͋Δ͜ͱ ϥϯμϜͳೖྗ஋ʹରͯ͠ ຬͨ͞ΕΔ΂͖ੑ࣭Λݕূ ৗʹ

ཚ਺Ͱ ճࢼߦ͞ΕͯΔ

ཚ਺Ͱ ճࢼߦ͞ΕͯΔ ύε͠ͳ͍σʔλ͕ͳ͍͔ େྔͷཚ਺Ͱ͋ͿΓग़͢ ͱߟ͑Δͱ෼͔Γ΍͍͢

2. ରশੑͷ͋ΔΞϧΰϦζϜ 3. ߴ଎ vs ௿଎

• ʮඞͣղ͚Δʯͱ͍͏ੑ࣭Λݕূ͢Δ

• ٯʹݴ͑͹ղ͚ͳ͍ͱόά • ʮඞͣղ͚Δʯͱ͍͏ੑ࣭Λݕূ͢Δ ྫ͑͹ Monkey Runner ΋ ϥϯμϜͳΠϕϯτΛൃߦ͠ଓ͚ ʮམͪͳ͍ʯੑ࣭Λݕূ͢Δ "OESPJEͷ

• ͲΜͳ஋ΛΤϯίʔυ → σίʔυ ͯ͠΋ • ඞͣʮݩͷ஋ʹ໭Δʯͱ͍͏ੑ࣭Λݕূ



• ߴ଎ͳίʔυ͸଎͍ʢ͚Ͳෳࡶʣ • ௿଎ͳίʔυ͸໌നʢ͚Ͳ஗͍ʣ • ͲͪΒ΋ʮಉ݁͡ՌʹͳΔʯͱ͍͏ੑ࣭Λར༻ • લऀͷݕূʹޙऀΛར༻͢Δ

͸ ςεςΟϯάϑϨʔϜϫʔΫ • Property-based Testing ΋αϙʔτ • ϥϯμϜͳೖྗΛݩʹੑ࣭Λݕূ • Example-based Λஔ͖׵͑Δ΋ͷͰ͸ͳ͍