ရိုးရိုးလေး ရှင်းပြမယ်
Testing ကို ရိုးရိုးလေး ပြောရရင်
Test ဆိုတာ သိပြီးသား input နဲ့ ကိုယ့် code ကို ခေါ်ပြီး ရလဒ်က မျှော်လင့်တဲ့အတိုင်း ဟုတ်မဟုတ် စစ်တဲ့ program သေးသေးလေးတစ်ခုပါ။ Change တစ်ခုက တစ်ခုခုကို ဖျက်လိုက်လားဆိုတာ ကြည့်ဖို့ app ကို run ပြီး ဟိုနှိပ်ဒီနှိပ် လုပ်နေမယ့်အစား test တွေကို run လိုက်ရင် စက္ကန့်ပိုင်းအတွင်း pass ဒါမှမဟုတ် fail ရပါတယ်။ မီးခိုးသတိပေးစက်လို စဉ်းစားကြည့်ပါ: အားလုံး ကောင်းနေရင် ဘာမှ မလုပ်ပါဘူး၊ ထိန်းသိမ်းစရိတ် သက်သာပြီး တစ်ခုခု မှားနေရင် စောစောစီးစီး ကျယ်ကျယ်လောင်လောင် အသိပေးပါတယ်။
Kotlin project တွေမှာ စံတွဲက kotlin.test နဲ့ JUnit ပါ။ kotlin.test က platform အားလုံးမှာ အလုပ်လုပ်တဲ့ annotation နဲ့ assertion function တွေ ပါတဲ့ library သေးသေးလေးဖြစ်ပြီး JUnit ကတော့ JVM ပေါ်မှာ ကိုယ့် test function တွေကို ရှာပြီး တစ်ခုချင်း run ကာ report ထုတ်ပေးတဲ့ engine ပါ။ ./gradlew test ကို run တဲ့အခါ Gradle က အဲဒီ engine ကို စတင်ပေးပါတယ်။
Problem → Why → Solution
ပြဿနာ: လက်နဲ့ စစ်ရတဲ့ စျေးနှုန်း စည်းမျဉ်း
သောကြာနေ့ နေ့လည်ခင်းမှာ စျေးနှုန်း စည်းမျဉ်းတစ်ခု ပြောင်းလိုက်ပါတယ်။ လက်နဲ့ စစ်မယ်ဆိုရင် app ကို ဖွင့်၊ item တစ်ခု ထည့်၊ coupon သုံး၊ 0%၊ 100% နဲ့ အနုတ် percent တွေ စမ်းပြီး နောက်ပြောင်းလဲမှုတိုင်းမှာ အကုန် ပြန်လုပ်ရပါတယ်။ အဲဒီအဆင့်တွေကို ဘယ်သူမှ အကြိမ်တိုင်း မလုပ်တာမို့ bug တစ်ခု လွတ်သွားပါတယ်: coupon 150% က စျေးကို အနုတ်ဖြစ်စေပြီး ဆိုင်က ဝယ်သူကို ပိုက်ဆံပေးနေပါတယ်။ လက်နဲ့စစ်တာက မှတ်တမ်း မကျန်လို့ နောက်လာတဲ့ developer က ဘယ် case တွေ အရေးကြီးလဲ မသိနိုင်ပါဘူး။
fun applyDiscount(priceCents: Int, percent: Int): Int {
return priceCents - priceCents * percent / 100
}
fun main() {
// "testing" by eye: run it and squint at the output
println(applyDiscount(1000, 25)) // 750, looks fine
println(applyDiscount(1000, 150)) // -500, nobody notices
}Test က လက်နဲ့စစ်တာကို ပြန်လုပ်လို့ရအောင် လုပ်ပေးတယ်
Test က တစ်ကြိမ်သာ လက်နဲ့ စစ်ခဲ့တာကို ပြန်လုပ်လို့ရတဲ့၊ နာမည်ပေးထားတဲ့၊ အလိုအလျောက် စစ်တာအဖြစ် ပြောင်းပေးပါတယ်။ Test တစ်ခုစီက တစ်ချိန်က စိတ်ဝင်စားခဲ့တဲ့ မျှော်မှန်းချက်တစ်ခုကို မှတ်ထားပြီး suite တစ်ခုလုံးက ပြောင်းလဲမှုတိုင်းမှာ စက္ကန့်ပိုင်းအတွင်း run ပါတယ်၊ ဒါကြောင့် bug ကို ပြောင်းလဲမှုနဲ့ ကပ်လျက် သိရပြီး ရက်သတ္တပတ်ပေါင်းများစွာ နောက်ကျမှ customer က တိုင်ကြားတာ မဟုတ်တော့ပါဘူး။
Core concept: framework၊ assertion နဲ့ deterministic test
Check တွေကို ကိုယ်တိုင်ရေးလို့ရပါတယ် (worked example က အဲဒီလိုပဲ လုပ်ပါတယ်)၊ ဒါပေမဲ့ framework က ထပ်ခါထပ်ခါ လုပ်ရတဲ့ အပိုင်းတွေကို လုပ်ပေးပါတယ်: @Test နဲ့ မှတ်ထားတဲ့ function တိုင်းကို ရှာ၊ တစ်ခုချင်းစီကို test class ရဲ့ instance အသစ်မှာ run လို့ test တွေက state ကို တစ်ခုနဲ့တစ်ခု မယိုစီးစေပါဘူး၊ test တစ်ခု fail ရင် ကျန်တာတွေကို မရပ်ဘဲ ဖမ်းထား၊ ပြီးတော့ fail တဲ့ test တိုင်းရဲ့ နာမည်နဲ့ expected၊ actual တန်ဖိုးတွေပါတဲ့ report ကို ထုတ်ပေးပါတယ်။
kotlin.test က ဝေါဟာရကို ပေးပါတယ်: assertEquals(expected, actual) က တန်ဖိုးတွေ နှိုင်းယှဉ်၊ assertTrue နဲ့ assertFalse က အခြေအနေ စစ်၊ assertNull နဲ့ assertNotNull က null ဟုတ်မဟုတ် စစ်၊ assertContentEquals က collection တွေကို element တစ်ခုချင်း နှိုင်းယှဉ်၊ assertFailsWith<T> { ... } က block ကို run ပြီး T ကို မ throw ရင် fail ကာ exception ကို ပြန်ပေးလို့ message ကို စစ်လို့ရပါတယ်။ Argument အစီအစဉ်ကို သတိထားပါ: expected က ရှေ့၊ actual က နောက်၊ ဘာလို့လဲဆိုတော့ fail message က expected: <800> but was: <750> လို့ ပြောတာမို့ အစီအစဉ် ပြောင်းထားရင် အဲဒီစာကြောင်းက မမှန်တော့ပါဘူး။
| Function | သုံးရမယ့်နေရာ |
|---|---|
| assertEquals(expected, actual) | တန်ဖိုးနှစ်ခုကို equals နဲ့ နှိုင်းယှဉ် |
| assertTrue(condition) / assertFalse | boolean အခြေအနေ စစ် |
| assertNull(x) / assertNotNull(x) | တန်ဖိုးက null ဟုတ်မဟုတ် စစ် |
| assertContentEquals(a, b) | collection ဒါမှမဟုတ် array ကို element တစ်ခုချင်း နှိုင်းယှဉ် |
| assertFailsWith<T> { ... } | block က T ကို throw မယ်ဆိုတာ စစ်ပြီး exception ကို ကြည့် |
ကောင်းတဲ့ test မှာ အပိုင်းသုံးပိုင်း ရှိပါတယ်: arrange (input နဲ့ fake တွေ ဖန်တီး)၊ act (စမ်းမယ့် code ကို တစ်ကြိမ်ခေါ်)၊ assert (အပြုအမူတစ်ခုကို စစ်)။ အဲဒီ အပြုအမူအတိုင်း နာမည်ပေးပါ (ဥပမာ rejects percent above 100)၊ JVM မှာ Kotlin က backtick ကြားမှာ နာမည်ရေးခွင့်ပေးလို့ report က စာကြောင်းလို ဖတ်ရပါတယ်။ Case တွေကို တမင်ရွေးပါ: ပုံမှန်လမ်းကြောင်း၊ boundary (0, 1, အများဆုံး၊ အများဆုံးထက် တစ်ခုပိုတာ)၊ empty နဲ့ null input နဲ့ function က ကတိပေးတဲ့ failure တစ်ခုချင်းစီ (exception, null, error value) ပါ။
Test တွေက deterministic ဖြစ်ရပါမယ်: input တူရင် ရလဒ်တူ၊ အကြိမ်တိုင်း။ ပြောင်းလဲနိုင်တဲ့ အရာတိုင်း (လက်ရှိအချိန်၊ random နံပါတ်၊ network၊ ဖိုင်၊ တခြား test တွေ ကျန်ခဲ့တဲ့ အရာ) က test ကို flaky ဖြစ်စေပြီး flaky test တွေကို လူတွေက လျစ်လျူရှုလာပါတယ်။ ကုသနည်းက အဲဒါတွေကို ပြင်ပကနေ ပေးလိုက်တာပါ: Clock ဒါမှမဟုတ် Random ကို parameter အဖြစ်၊ database အစစ်အစား fake repository ကို သုံးပါ။ Suspend function တွေအတွက်ဆိုရင် kotlinx-coroutines-test ရဲ့ runTest က test body ကို virtual time ရှိတဲ့ ထိန်းချုပ်ထားတဲ့ scheduler မှာ run လို့ delay(1_000) က တစ်စက္ကန့် မစောင့်ဘဲ ချက်ချင်း ပြီးပါတယ်။
- Arrange: input နဲ့ fake တွေ ဆောက်
- Act: စမ်းမယ့် code ကို တစ်ကြိမ်ခေါ်
- Assert: ရလဒ်ကို မျှော်မှန်းချက်နဲ့ နှိုင်း
- Runner: ရလဒ်တိုင်း report၊ ဆက် run
ဘယ်တော့မှ fail မဖြစ်နိုင်တဲ့ test က test မဟုတ်ပါဘူး: code ကို တမင်ချိုးပြီး အနီရောင် ဖြစ်မဖြစ် ကြည့်ပါ။
အထွေထွေ ပုံစံ
import kotlin.test.Test
import kotlin.test.assertEquals
class SubjectTest {
@Test
fun `behaviour described in plain words`() {
val input = anInput() // arrange
val result = subject(input) // act
assertEquals(expected, result) // assert
}
}Test function တစ်ခုချင်းစီကို @Test နဲ့ မှတ်ပြီး အပြုအမူကို ဖော်ပြတဲ့ နာမည်ပေးပါ။ Body ထဲမှာ arrange, act, assert အစီအစဉ်ကို လိုက်နာပါ၊ assertEquals မှာ expected ကို ရှေ့ထားပါ။
Worked example: test framework သေးသေးလေးနဲ့ သူ့ test တွေ
ဖိုင်တစ်ခုတည်း program မှာ kotlin.test နဲ့ JUnit မရတဲ့အတွက် ဒီ example က အတွေးအခေါ်တူတူကို line ငါးဆယ်လောက်နဲ့ ပြန်ဆောက်ပါတယ်: assertEquals နဲ့ assertFailsWith ကို throw လုပ်တဲ့ ရိုးရိုး function တွေအဖြစ်၊ နာမည်ပေးထားတဲ့ test တွေကို မှတ်ပြီး တစ်ခုချင်း run၊ failure ဖမ်း၊ report ထုတ်တဲ့ TestRunner ကိုပါ ရေးထားပါတယ်။ Magic ဘယ်လောက်နည်းလဲ မြင်ရတာက အစစ်အမှန် tool တွေကို ယုံကြည်ဖို့ လွယ်စေပါတယ်။ စမ်းသပ်မယ့် function က percent အပေါ် require() စစ်ထားတဲ့ applyDiscount ပါ။
Tool အစစ်၊ ပုံစံတူတူ
Gradle project တစ်ခုမှာ runner ကို ကိုယ်တိုင် မရေးရပါဘူး။ နောက်ပိုင်း အပိုင်းမှာ test လေးခုတူတူကို kotlin.test နဲ့ JUnit နဲ့ ပြထားပြီး @Test က runner.test(...) ကို၊ Gradle ရဲ့ test task က runAll() ကို အစားထိုးပါတယ်။
class AssertionFailed(message: String) : Exception(message)
fun <T> assertEquals(expected: T, actual: T) {
if (expected != actual) {
throw AssertionFailed("expected <$expected> but was <$actual>")
}
}
inline fun <reified T : Throwable> assertFailsWith(block: () -> Unit): T {
val wanted = T::class.simpleName
try {
block()
} catch (e: Throwable) {
if (e is T) return e
throw AssertionFailed("expected $wanted but got ${e::class.simpleName}")
}
throw AssertionFailed("expected $wanted but nothing was thrown")
}
fun applyDiscount(priceCents: Int, percent: Int): Int {
require(percent in 0..100) { "percent must be 0..100 but was $percent" }
return priceCents - priceCents * percent / 100
}
class TestRunner {
private val tests = mutableListOf<Pair<String, () -> Unit>>()
fun test(name: String, body: () -> Unit) {
tests += name to body
}
fun runAll(): Int {
var failures = 0
for ((name, body) in tests) {
try {
body()
println("PASS $name")
} catch (e: AssertionFailed) {
failures++
println("FAIL $name: ${e.message}")
}
}
println("${tests.size - failures} passed, $failures failed")
return failures
}
}
fun main() {
val runner = TestRunner()
runner.test("zero percent keeps the full price") {
assertEquals(1000, applyDiscount(1000, 0))
}
runner.test("25 percent off 1000 is 750") {
assertEquals(750, applyDiscount(1000, 25))
}
runner.test("100 percent makes it free") {
assertEquals(0, applyDiscount(999, 100))
}
runner.test("rejects percent above 100") {
val error = assertFailsWith<IllegalArgumentException> { applyDiscount(1000, 101) }
assertEquals("percent must be 0..100 but was 101", error.message)
}
val failures = runner.runAll()
check(failures == 0) { "some tests failed" }
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- A failed assertion is an exception
throw AssertionFailed("expected <$expected> but was <$actual>")assertion က တန်ဖိုးနှစ်ခု မတူရင် exception တစ်ခု throw လုပ်ရုံပါပဲ၊ ဘာမှ ထူးဆန်းတာ မရှိပါဘူး။ Message မှာ expected နဲ့ actual နှစ်ခုလုံး ပါလို့ fail ရင် ဘာကို ဘာနဲ့ နှိုင်းလဲ ချက်ချင်း မြင်ရပါတယ်။
- assertFailsWith
inline fun <reified T : Throwable> assertFailsWith(block: () -> Unit): T {reified T ကြောင့် runtime မှာ T ရဲ့ class ကို သိပြီး 'is T' စစ်လို့ရပါတယ်။ Block က မှန်တဲ့ exception ကို throw ရင် ပြန်ပေးပြီး မ throw ရင်၊ ဒါမှမဟုတ် တခြား type ကို throw ရင် AssertionFailed နဲ့ fail ပါတယ်။
- Register tests as data
tests += name to bodyTest တစ်ခုချင်းစီက နာမည်နဲ့ lambda တစ်ခုပါ၊ ဒါကြောင့် စာရင်းတစ်ခုထဲ သိမ်းထားပြီး နောက်မှ run လို့ရပါတယ်။ JUnit က @Test function တွေကို reflection နဲ့ ရှာတာလည်း ဒီအတွေးပဲ ဖြစ်ပါတယ်။
- Isolate each failure
} catch (e: AssertionFailed) {Test တစ်ခု fail ရင် ဖမ်းပြီး failures ကို တစ်ခု တိုးကာ နောက် test ကို ဆက်သွားပါတယ်။ ဒါကြောင့် fail တစ်ခုက ကျန် test တွေကို မဖျောက်ပါဘူး။
- A test is an ordinary call
assertEquals(750, applyDiscount(1000, 25))Expected 750 ကို ရှေ့မှာ၊ စမ်းမယ့် code ခေါ်တာကို နောက်မှာ ထားထားပါတယ်။ 1000 ရဲ့ 25% လျော့ဈေးက 250 ဖြစ်လို့ 750 ကျန်တာကို လက်နဲ့တွက်ပြီး ရေးထားတာပါ၊ function ရဲ့ output ကို ကူးထည့်တာ မဟုတ်ပါဘူး။
- Fail the whole run
check(failures == 0) { "some tests failed" }test တစ်ခုခု fail ရင် program က exception နဲ့ ပြီးပါတယ်။ ဒါကြောင့် CI က နောက်ဆုံး exit status ကိုကြည့်ပြီး build ကို fail လုပ်နိုင်ပါတယ်၊ Gradle ရဲ့ test task လည်း အဲဒီလိုပဲ လုပ်ပါတယ်။
PASS zero percent keeps the full price
PASS 25 percent off 1000 is 750
PASS 100 percent makes it free
PASS rejects percent above 100
4 passed, 0 failed- src/main/kotlin နဲ့ src/test/kotlin ကို compile
- JUnit က test class တွေထဲက @Test function တွေကို ရှာ
- Test တစ်ခုချင်းကို class ရဲ့ instance အသစ်မှာ run
- Failure တွေကို မှတ်ပြီး ကျန် test တွေ ဆက် run
- Gradle က အနှစ်ချုပ် print ထုတ်ပြီး HTML report ရေး
kotlin.test နဲ့ JUnit နဲ့ ရေးထားတဲ့ test တူတူ
အောက်က class က worked example ထဲက test တွေကို kotlin.test နဲ့ ရေးထားတာပါ။ @Test က runner.test(...) နေရာမှာ၊ assertFailsWith<T> က exception ကို ပြန်ပေးလို့ message ကို ဆက်စစ်လို့ရပါတယ်။ တမင်ချိုးထားတဲ့ assertEquals(800, applyDiscount(1000, 25)) လို fail ရင် report က expected: <800> but was: <750> လို့ ပြပါတယ်။
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
class DiscountTest {
@Test
fun `zero percent keeps the full price`() {
assertEquals(1000, applyDiscount(1000, 0))
}
@Test
fun `25 percent off 1000 is 750`() {
assertEquals(750, applyDiscount(1000, 25))
}
@Test
fun `rejects percent above 100`() {
val error = assertFailsWith<IllegalArgumentException> {
applyDiscount(1000, 101)
}
assertEquals("percent must be 0..100 but was 101", error.message)
}
}Gradle ဘက်မှာ kotlin("test") ကို test classpath ပေါ်တင်ပြီး test task ကို JUnit Platform နဲ့ run ခိုင်းရပါတယ်။ ဖိုင်တွေက src/test/kotlin ထဲမှာ ရှိပြီး ./gradlew test ကို run ရင် ရလဒ်ကို terminal မှာရော build/reports/tests/test/index.html မှာပါ ကြည့်လို့ရပါတယ်။
dependencies {
testImplementation(kotlin("test"))
}
tasks.test {
useJUnitPlatform()
}စမ်းသပ်သင့်တဲ့ case တွေ
Test အရေအတွက်ထက် case ရွေးချယ်မှုက ပိုအရေးကြီးပါတယ်။ Bug တွေက ပုံမှန်လမ်းကြောင်းမှာ မကြာခဏ မရှိဘဲ စည်းမျဉ်းရဲ့ အစွန်းတွေမှာ ရှိတတ်ပါတယ်။ applyDiscount အတွက် အောက်က table က case အမျိုးအစားတွေကို ပြပါတယ်။
| Case အမျိုးအစား | applyDiscount နမူနာ |
|---|---|
| ပုံမှန်လမ်းကြောင်း | 1000 ကို 25% လျော့ရင် 750 |
| အနိမ့်ဆုံး boundary | 0% ဆိုရင် စျေး မပြောင်း |
| အမြင့်ဆုံး boundary | 100% ဆိုရင် 0 |
| boundary ရဲ့ အပြင် တစ်ခု | -1 နဲ့ 101 က IllegalArgumentException |
| သုညလို အလွတ်တန်ဖိုး | စျေး 0 ဆိုရင် ရလဒ် 0 |
runTest နဲ့ suspend function တွေကို test လုပ်ခြင်း
Suspend function ကို ရိုးရိုး test function ထဲက မခေါ်နိုင်ပါဘူး၊ ဒါပေမဲ့ delay ပါတဲ့ code ကို runBlocking နဲ့ test လုပ်ရင် တကယ် စောင့်ရတာမို့ နှေးပါတယ်။ kotlinx-coroutines-test ရဲ့ runTest က virtual time ကို သုံးလို့ delay(1_000) ပါတဲ့ test တောင် မီလီစက္ကန့်ပိုင်းနဲ့ ပြီးပါတယ်။ Dependency ကို kotlinx-coroutines-core နဲ့ version တူအောင် testImplementation အဖြစ် ထည့်ရပါတယ်။
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals
suspend fun slowGreeting(): String {
delay(1_000)
return "hello"
}
class SlowGreeterTest {
@Test
fun `returns the greeting without really waiting a second`() = runTest {
assertEquals("hello", slowGreeting())
}
}လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ test ကို ဘယ်မှာ တွေ့ရလဲ
Professional project တွေမှာ test က code ရဲ့ အစိတ်အပိုင်းပါပဲ၊ ထပ်ဆောင်းလုပ်ငန်း မဟုတ်ပါဘူး။ Test မရှိတဲ့ ပြောင်းလဲမှုက pull request ထဲ ဝင်ဖို့ ခက်ပါတယ်၊ ဘာလို့လဲဆိုတော့ reviewer က code ကို လက်နဲ့ ပြန် run မကြည့်ဘဲ test ရလဒ်ကို ယုံရလို့ပါ။
- CI: pull request တိုင်းမှာ CI server က ./gradlew test (ဒါမှမဟုတ် build) ကို run ပြီး fail ရင် merge မလုပ်စေပါဘူး။ Report ကို build/reports/tests/test/index.html မှာ ကြည့်နိုင်ပါတယ်။
- Android: JVM ပေါ်မှာ မြန်မြန် run တဲ့ local unit test တွေကို src/test မှာ၊ device ပေါ်မှာ run တဲ့ instrumented test တွေကို src/androidTest မှာ ခွဲထားပါတယ်။
- Spring Boot နဲ့ Ktor: JUnit 5 ကို အခြေခံပြီး controller တွေကို တကယ် server မဖွင့်ဘဲ test လုပ်တဲ့ helper တွေ ရှိပါတယ်၊ project generator တွေက test dependency ကို ကြိုထည့်ပေးတတ်ပါတယ်။
- Kotlin Multiplatform: commonTest ထဲက test တွေက target တစ်ခုချင်းစီမှာ run လို့ shared logic က platform အားလုံးမှာ တူညီစွာ အလုပ်လုပ်ကြောင်း စစ်နိုင်ပါတယ်။
- Bug ပြင်တဲ့အခါ: အရင်ဆုံး bug ကို ပြတဲ့ test အနီရောင်တစ်ခု ရေးပြီးမှ ပြင်ပါတယ်၊ ဒါမှ အဲဒီ bug ပြန်မပေါ်ဘူးဆိုတာ အမြဲစစ်နေပါတယ်။
အမှားများ
Test နဲ့ ဖြစ်လေ့ရှိတဲ့ အမှားများ
ဘယ်တော့မှ fail မဖြစ်နိုင်တဲ့ test
try { f() } catch (e: SomeException) { } လို့ရေးပြီး ဘာမှ assert မလုပ်ရင် exception ရှိလည်း မရှိလည်း test က အစိမ်းရောင်ပါပဲ။ အောက်က code မှာ buggyDiscount က 150% ကို လက်ခံနေပေမဲ့ weak test က အစိမ်းဖြစ်နေပြီး assertFailsWith လို check က 'expected IllegalArgumentException but nothing was thrown' နဲ့ အနီဖြစ်ပါတယ်။ Exception မျှော်ရင် assertFailsWith ကို သုံးပါ။
expected နဲ့ actual ကို ပြောင်းပြန်ရေးခြင်း
assertEquals(applyDiscount(1000, 25), 800) လို့ ရေးရင် test က မှန်ရာမှန်ကန်စွာ fail ပါတယ်၊ ဒါပေမဲ့ message က 'expected: <750> but was: <800>' လို့ ပြောပါတယ်။ ဒါက မျှော်မှန်းထားတာ 800 ဖြစ်ပြီး code ကပေးတာ 750 ဖြစ်တာကို ပြောင်းပြန် ဖတ်ရစေလို့ debug လုပ်ရာမှာ အချိန်ဖြုန်းစေပါတယ်။ Expected ကို အမြဲ ရှေ့ထားပါ။
အချိန်၊ random နဲ့ network ကို တိုက်ရိုက်သုံးခြင်း
Test က လက်ရှိအချိန်၊ seed မထည့်တဲ့ Random ဒါမှမဟုတ် အစစ်အမှန် network ကို မှီခိုရင် ၂၀ ကြိမ်မှာ တစ်ကြိမ် fail တတ်ပါတယ်၊ ဘာကြောင့်လဲ ရှာမတွေ့နိုင်ပါဘူး။ Clock ဒါမှမဟုတ် Random ကို parameter အဖြစ် ပေးပြီး test မှာ fake (အချိန်တူတူ ပြန်ပေးတာ) ကို သုံးပါ။
Coroutine test မှာ တကယ့် delay သုံးခြင်း
Thread.sleep ဒါမှမဟုတ် runBlocking ထဲက delay(2_000) ကို စောင့်နေရင် test suite က နှေးလာပြီး အချိန်ပေါ် မူတည်လို့ flaky ဖြစ်လာပါတယ်။ runTest နဲ့ virtual time ကို သုံးပါ။
fun buggyDiscount(priceCents: Int, percent: Int) = priceCents - priceCents * percent / 100
inline fun <reified T : Throwable> expectFailure(block: () -> Unit) {
try {
block()
} catch (e: Throwable) {
if (e is T) return
throw e
}
throw AssertionError("expected ${T::class.simpleName} but nothing was thrown")
}
fun main() {
try {
buggyDiscount(1000, 150)
} catch (e: IllegalArgumentException) {
println("never printed")
}
println("weak test: green, although 150 percent was accepted")
try {
expectFailure<IllegalArgumentException> { buggyDiscount(1000, 150) }
} catch (e: AssertionError) {
println("real test: red -> ${e.message}")
}
}Best Practices
Best practices
- Implementation အသေးစိတ်ကို မဟုတ်ဘဲ အပြုအမူကို test လုပ်ပါ၊ ဒါမှ refactor လုပ်တဲ့အခါ test တွေ မပျက်ပါဘူး။
- Test တစ်ခုကို အပြုအမူတစ်ခုနဲ့ စာကြောင်းလို နာမည်ပေးပါ၊ ဒါမှ fail ရင် ဘာပျက်လဲ နာမည်ကပဲ ပြောပြပါတယ်။
- Test တွေကို မြန်မြန်နဲ့ သီးခြားစီ ထားပါ၊ ဒါမှ pull request တိုင်းမှာ CI က run ဖို့ ဝန်မမှန်ပါဘူး။
- Expected တန်ဖိုးကို လက်နဲ့ တွက်ပြီး ရေးပါ၊ function ကို run ပြီး ထွက်လာတဲ့ output ကို ကူးထည့်ရင် bug ကိုပါ မှန်တယ်လို့ မှတ်ယူသွားပါလိမ့်မယ်။
Mini challenge
Mini challenge: median ကို test အရင်ရေးပြီး တည်ဆောက်ခြင်း
Int list တစ်ခုရဲ့ median ကို ပြန်ပေးတဲ့ median function ကို ရေးပါ။ List အလွတ်ဆိုရင် IllegalArgumentException ကို require() နဲ့ ပစ်ရပါမယ်၊ ပေးလိုက်တဲ့ list ကိုလည်း ပြောင်းလဲ (reorder) လုပ်လို့ မရပါဘူး။ ပြီးတော့ case တွေကို table အဖြစ် စုပြီး check() နဲ့ စစ်တဲ့ test code ကို main ထဲမှာ ရေးပါ။
- median(values) က Double ပြန်ပေးရမယ်: အရေအတွက်မ ဆိုရင် အလယ်တန်ဖိုး၊ စုံရင် အလယ်နှစ်ခုရဲ့ ပျမ်းမျှ
- List အလွတ်ဆိုရင် require() နဲ့ ရှင်းလင်းတဲ့ message ပါတဲ့ IllegalArgumentException ပစ်ရမယ်
- ပေးလိုက်တဲ့ list ကို မပြောင်းလဲရဘူး (sorted() က list အသစ်ပေးတယ်ဆိုတာ သုံးပါ)
- Single value, odd count, even count နဲ့ negative number case တွေကို table နဲ့ စစ်ပြီး PASS ကို print ထုတ်ပါ
// TODO: require() a non-empty list, sort a COPY, then pick the middle value
// (odd size) or the mean of the two middle values (even size)
fun median(values: List<Int>): Double {
TODO()
}
// TODO: run block and return the message of the IllegalArgumentException it
// throws, or null when nothing is thrown
fun failureMessage(block: () -> Unit): String? {
TODO()
}
fun main() {
// TODO: a table of (name, input, expected) cases: one value, odd count,
// even count, negative numbers; check() each result and print PASS <name>
// TODO: check() that median does not reorder a mutable input list
// TODO: check() the exact message produced for an empty list
}ရလဒ်: PASS single value, PASS odd count, PASS even count, PASS negative numbers, PASS input is not modified နဲ့ PASS empty list is rejected ဆိုပြီး အကြောင်းအရာ ခြောက်ကြောင်း print ထုတ်ပါမယ်။ check တစ်ခုခု မှားရင် program က message နဲ့ exception ပစ်ပါလိမ့်မယ်။
အရိပ်အမြွက် ကြည့်ရန်
Case table အတွက် listOf("name" to (listOf(3, 1, 2) to 2.0)) လို Pair ထဲ Pair ကို သုံးနိုင်ပါတယ်၊ ပြီးတော့ for ((name, case) in cases) နဲ့ destructure လုပ်ပါ။ failureMessage ထဲမှာ try / catch (e: IllegalArgumentException) ကို သုံးပြီး block() ပြီးရင် null ပြန်ပါ။ အရေအတွက်စုံတဲ့အခါ အလယ်နှစ်ခုက sorted[mid - 1] နဲ့ sorted[mid] ပါ။
အဖြေ ကြည့်ရန်
fun median(values: List<Int>): Double {
require(values.isNotEmpty()) { "median of an empty list is undefined" }
val sorted = values.sorted()
val mid = sorted.size / 2
return if (sorted.size % 2 == 1) {
sorted[mid].toDouble()
} else {
(sorted[mid - 1] + sorted[mid]) / 2.0
}
}
fun failureMessage(block: () -> Unit): String? =
try {
block()
null
} catch (e: IllegalArgumentException) {
e.message
}
fun main() {
val cases = listOf(
"single value" to (listOf(7) to 7.0),
"odd count" to (listOf(3, 1, 2) to 2.0),
"even count" to (listOf(4, 1, 3, 2) to 2.5),
"negative numbers" to (listOf(-5, -1, -3) to -3.0),
)
for ((name, case) in cases) {
val (input, expected) = case
val actual = median(input)
check(actual == expected) { "$name: expected $expected but was $actual" }
println("PASS $name")
}
val input = mutableListOf(9, 1, 5)
median(input)
check(input == listOf(9, 1, 5)) { "median must not reorder its input" }
println("PASS input is not modified")
val message = failureMessage { median(emptyList()) }
check(message == "median of an empty list is undefined")
println("PASS empty list is rejected")
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Test framework က @Test function တွေကို ရှာ၊ တစ်ခုချင်း သီးခြား run၊ fail တဲ့ test တွေကို report ထုတ်ပေးပါတယ်။
- kotlin.test ရဲ့ assertEquals(expected, actual) နဲ့ assertFailsWith<T> တို့က အဓိက assertion တွေဖြစ်ပြီး expected ကို အမြဲ ရှေ့ထားပါ။
- Test ကို arrange, act, assert အဖြစ် ဖွဲ့ပြီး ပုံမှန်လမ်းကြောင်း၊ boundary နဲ့ failure case တွေကို ရွေးပါ။
- Gradle မှာ testImplementation(kotlin("test")) နဲ့ useJUnitPlatform() သုံးပြီး deterministic ဖြစ်အောင် fake နဲ့ runTest ကို သုံးပါ။
Test code using Kotlin and JUnit — Kotlin