Thuta Learning
Go
AdvancedProgrammingအခြေခံ

Testing, Benchmarks & Fuzzing

Goသင်ခန်းစာ 2535 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • _test.go file ထဲမှာ TestXxx function နဲ့ table-driven test ကို t.Run subtest တွေနဲ့ ရေးနိုင်ဖို့
  • t.Errorf နဲ့ t.Fatalf ကြား ကွာခြားချက်ကို သိပြီး မှန်ကန်စွာ ရွေးနိုင်ဖို့
  • BenchmarkXxx ရေးပြီး go test -bench ရလဒ်ကို ဖတ်တတ်ဖို့
  • FuzzXxx နဲ့ 'ဘယ် input မဆို မှန်ရမယ့်' property တစ်ခုကို စစ်ပြီး fuzz က ရှာတွေ့တဲ့ input ကို regression test အဖြစ် ထိန်းနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

Testing ကို ရိုးရိုးလေး ပြောရရင်

Test ဆိုတာ သင့် code ကို အလိုအလျောက် မေးခွန်းထုတ်ပေးတဲ့ code ပါ: 'ဒီ input ပေးရင် ဒီ output ထွက်ရမယ်'။ Code ကို ပြင်တိုင်း go test ကို run လိုက်ရုံနဲ့ အရင်က အလုပ်လုပ်ခဲ့တာ အားလုံး ဆက်အလုပ်လုပ်နေသေးလား စက္ကန့်ပိုင်းအတွင်း သိရပါတယ်။

Go မှာ test, benchmark, fuzz သုံးမျိုးလုံး testing package ထဲမှာ ပါပြီးသားပါ၊ library ထပ်ထည့်စရာ မလိုပါဘူး။ Test က မှန်လား စစ်တယ်၊ benchmark က ဘယ်လောက် မြန်လဲ တိုင်းတယ်၊ fuzz ကတော့ random input ထောင်ချီ ထုတ်ပြီး သင် မစဉ်းစားမိတဲ့ case ကို ရှာပေးပါတယ်။

Problem → Why → Solution

ပြဿနာ: ASCII နဲ့ စမ်းတော့ မှန်တယ်

Notification တစ်ခုမှာ စာသားကို စာလုံး 20 လုံးအထိ ဖြတ်ပြချင်လို့ Truncate function ရေးလိုက်ပါတယ်။ "gopher" နဲ့ စမ်းတော့ "gop" ရပြီး မှန်ပါတယ်။ ဒါပေမဲ့ user တစ်ယောက်က 'héllo' ဒါမှမဟုတ် မြန်မာစာ ရိုက်တော့ notification မှာ ? ပုံစံ စာလုံးပျက်တွေ ပေါ်လာပါတယ်။ Sample output တစ်ခု မှန်တာက code မှန်တယ်ဆိုတာ မသက်သေပြပါဘူး။

truncate_bug.go
func Truncate(s string, max int) string {
    if len(s) <= max { // len counts BYTES, not characters
        return s
    }
    return s[:max] // can cut a multi-byte character in half
}

// Truncate("gopher", 3) == "gop"   looks fine
// Truncate("héllo", 2)  == "h\xc3" invalid UTF-8: é is two bytes

Test က ဘာကြောင့် လိုတာလဲ

Go string က byte တွေ စုထားတာဖြစ်ပြီး é က byte နှစ်ခု၊ မြန်မာ စာလုံး တစ်လုံးက byte သုံးခု ယူပါတယ်။ s[:max] က byte နဲ့ ဖြတ်လို့ စာလုံးကို အလယ်က ခွဲမိပါတယ်။ ဒီလို bug က သင် ကိုယ်တိုင် ရွေးတဲ့ input နဲ့ ဘယ်တော့မှ မပေါ်ပါဘူး။ Edge case တွေကို table ထဲမှာ ရေးထားတဲ့ test နဲ့ random input တွေကို စမ်းတဲ့ fuzz က ဒါကို ဖမ်းပေးပါတယ်။

Core concept: go test ရဲ့ စည်းကမ်းများ

Test file တွေ ဆိုတာ _test.go နဲ့ ဆုံးတဲ့ file တွေပါ။ ပုံမှန် go build က သူတို့ကို လျစ်လျူရှုပြီး go test ကပဲ compile လုပ်ပါတယ်။ File ထဲမှာ func TestXxx(t *testing.T) ပုံစံ function တွေကို go test က ရှာပြီး တစ်ခုချင်း run ပါတယ်။ Test ကို တူညီတဲ့ package ထဲမှာ ရေးရင် unexported function တွေကို စမ်းနိုင်ပြီး package total_test လို external test package မှာ ရေးရင် ခေါ်သူ တစ်ယောက်လို public API ကိုပဲ သုံးနိုင်ပါတယ်။

*testing.T မှာ failure ကို report လုပ်ဖို့ နည်းနှစ်မျိုး ရှိပါတယ်။ t.Errorf က fail လို့ မှတ်ပြီး ဆက် run လို့ နောက် check တွေရဲ့ ရလဒ်ကိုပါ တစ်ခါတည်း မြင်ရပါတယ်။ t.Fatalf ကတော့ fail မှတ်ပြီး အဲဒီ test ကို ချက်ချင်း ရပ်ပါတယ်၊ setup မအောင်မြင်လို့ ဆက်လုပ်ရင် အဓိပ္ပာယ်မရှိတဲ့အခါ သုံးပါ။ Go ရဲ့ စံ ပုံစံက table-driven test ပါ: name, input, want ပါတဲ့ struct slice တစ်ခုကို loop ပတ်ပြီး case တစ်ခုစီကို t.Run(tc.name, ...) နဲ့ subtest အဖြစ် run ပါတယ်။ Case အသစ်ထည့်ချင်ရင် line တစ်ကြောင်းပဲ ထပ်ရေးရုံပါ၊ go test -run 'TestTruncate/multi' လို နာမည်နဲ့ case တစ်ခုတည်းကိုလည်း ရွေး run လို့ ရပါတယ်။

Benchmark တွေက func BenchmarkXxx(b *testing.B) ပုံစံပါ။ Framework က b.N ကို အလိုအလျောက် ချိန်ညှိပြီး loop ကို တိုင်းလို့ ရလောက်တဲ့ အကြိမ်အထိ run ကာ operation တစ်ခုချင်းစီရဲ့ ns/op ကို report လုပ်ပါတယ်။ Fuzz test တွေက func FuzzXxx(f *testing.F) ပုံစံပါ: f.Add နဲ့ seed input ပေးပြီး f.Fuzz ထဲမှာ 'ဘယ် input မဆို မှန်ရမယ်' ဆိုတဲ့ property ကို စစ်ပါတယ်။ ရိုးရိုး go test က seed တွေကိုပဲ run ပြီး -fuzz flag ပေးမှ input အသစ်တွေကို အဆက်မပြတ် ထုတ်ပါတယ်။

Commandလုပ်ဆောင်ချက်
go test ./...Module ထဲက package အားလုံးရဲ့ test တွေကို run
go test -run TestTruncate -vနာမည် ကိုက်တဲ့ test ကိုပဲ အသေးစိတ် output နဲ့ run
go test -bench=. -benchmemBenchmark တွေကို run ပြီး allocation ကိုပါ ပြ
go test -fuzz=FuzzTruncate -fuzztime=30sFuzz test တစ်ခုကို စက္ကန့် 30 ကြာ fuzz လုပ်
go test -race -coverRace detector နဲ့ coverage ကို ဖွင့်ပြီး run
Test သုံးမျိုး၊ မေးခွန်း သုံးခု
  1. Test: ရွေးထားတဲ့ case တွေ မှန်လား
  2. Benchmark: ဘယ်လောက် မြန်လဲ
  3. Fuzz: ဘယ် input မှာ ကျိုးမလဲ
  4. Fuzz ရှာတွေ့တာကို seed အဖြစ် ထိန်း

Test က သင် မျှော်လင့်ထားတာကို စစ်ပြီး fuzz ကတော့ သင် မျှော်လင့်မထားတာကို ရှာပေးပါတယ်။ နှစ်ခုလုံး လိုပါတယ်။

အထွေထွေ ပုံစံ

go
// file: name_test.go
func TestName(t *testing.T) {
    for _, tc := range cases {
        t.Run(tc.name, func(t *testing.T) {
            if got := Func(tc.in); got != tc.want { t.Errorf("...") }
        })
    }
}
func BenchmarkName(b *testing.B) { for i := 0; i < b.N; i++ { Func(input) } }
func FuzzName(f *testing.F) {
    f.Add(seed); f.Fuzz(func(t *testing.T, in Type) { /* check property */ })
}

Function နာမည်ရဲ့ ရှေ့ဆက် (Test, Benchmark, Fuzz) နဲ့ parameter type (*testing.T, *testing.B, *testing.F) က go test ကို ဘာလုပ်ရမလဲ ပြောပြပါတယ်။ အားလုံး _test.go file ထဲမှာ ရှိရပါမယ်။

Worked example: Truncate ကို case table နဲ့ စစ်ခြင်း

အောက်က program က rune (Unicode စာလုံး) နဲ့ ဖြတ်တဲ့ Truncate ကို table-driven test ပုံစံနဲ့ စစ်ပါတယ်။ ဒီမှာ test framework မပါဘဲ main ထဲမှာ run ထားလို့ go run နဲ့ ချက်ချင်း စမ်းနိုင်ပါတယ်။ Loop, case struct နဲ့ got/want နှိုင်းယှဉ်ပုံက _test.go file ထဲမှာ ရေးမယ့် ပုံစံနဲ့ အတူတူပါပဲ၊ နောက်ပိုင်း section မှာ အဲဒီ version ကို ပြထားပါတယ်။

truncate.go
package main

import (
    "fmt"
    "unicode/utf8"
)

// Truncate shortens s to at most max characters (runes),
// never cutting a multi-byte character in half.
func Truncate(s string, max int) string {
    if max <= 0 {
        return ""
    }
    if utf8.RuneCountInString(s) <= max {
        return s
    }
    return string([]rune(s)[:max])
}

func main() {
    tests := []struct {
        name string
        in   string
        max  int
        want string
    }{
        {"shorter", "Go", 5, "Go"},
        {"exact", "gopher", 6, "gopher"},
        {"cut ascii", "gopher", 3, "gop"},
        {"zero max", "gopher", 0, ""},
        {"multi-byte", "héllo", 2, "hé"},
    }

    passed := 0
    for _, tc := range tests {
        got := Truncate(tc.in, tc.max)
        status := "FAIL"
        if got == tc.want {
            status = "PASS"
            passed++
        }
        fmt.Printf("%s %-10s Truncate(%q, %d) = %q\n",
            status, tc.name, tc.in, tc.max, got)
    }
    fmt.Printf("%d/%d cases passed\n", passed, len(tests))
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Count characters, not bytesif utf8.RuneCountInString(s) <= max {

    len(s) အစား RuneCountInString ကို သုံးလို့ é ဒါမှမဟုတ် မြန်မာ စာလုံးကို တစ်လုံးလို့ပဲ ရေတွက်ပါတယ်။

  2. Cut on rune boundariesreturn string([]rune(s)[:max])

    String ကို []rune ပြောင်းပြီးမှ ဖြတ်လို့ စာလုံးကို အလယ်က ဘယ်တော့မှ မခွဲပါဘူး။ ပြီးမှ string ပြန်ပြောင်းပါတယ်။

  3. The case table{"multi-byte", "héllo", 2, "hé"},

    Case တစ်ခုစီက name, input, max, want ပါတဲ့ line တစ်ကြောင်းပါ။ Problem section က bug ကို ဖမ်းတဲ့ case က ဒီ multi-byte case ပါပဲ။

  4. Compare got with wantif got == tc.want {

    Test တိုင်းရဲ့ အနှစ်က got နဲ့ want ကို နှိုင်းယှဉ်တာပါ။ _test.go version မှာတော့ မကိုက်ရင် t.Errorf ကို ခေါ်ပါမယ်။

  5. Readable reportfmt.Printf("%s %-10s Truncate(%q, %d) = %q\n",

    %q က string ကို quote နဲ့ ပြလို့ အလွတ် string ကို "" အဖြစ် ရှင်းရှင်း မြင်ရပါတယ်။ Fail message ကောင်းတစ်ခုမှာ input, got, want သုံးခုလုံး ပါသင့်ပါတယ်။

You should see
PASS shorter    Truncate("Go", 5) = "Go"
PASS exact      Truncate("gopher", 6) = "gopher"
PASS cut ascii  Truncate("gopher", 3) = "gop"
PASS zero max   Truncate("gopher", 0) = ""
PASS multi-byte Truncate("héllo", 2) = "hé"
5/5 cases passed

_test.go ထဲမှာ test, benchmark, fuzz

Project အစစ်မှာ Truncate က truncate.go ထဲမှာ ရှိပြီး test တွေက ဘေးက truncate_test.go ထဲမှာ ရှိပါတယ်။ အောက်က file မှာ အပေါ်က table ကို t.Run subtest အဖြစ် ပြောင်းထားပြီး benchmark တစ်ခုနဲ့ fuzz test တစ်ခု ထပ်ထည့်ထားပါတယ်။ Fuzz test က တိကျတဲ့ output ကို မစစ်ပါဘူး၊ input ဘာပဲဖြစ်ဖြစ် မှန်ရမယ့် စည်းမျဉ်း သုံးခုကို စစ်ပါတယ်။

truncate_test.go
package text

import (
    "strings"
    "testing"
    "unicode/utf8"
)

func TestTruncate(t *testing.T) {
    tests := []struct {
        name, in string
        max      int
        want     string
    }{
        {"cut ascii", "gopher", 3, "gop"},
        {"multi-byte", "héllo", 2, "hé"},
    }
    for _, tc := range tests {
        t.Run(tc.name, func(t *testing.T) {
            if got := Truncate(tc.in, tc.max); got != tc.want {
                t.Errorf("Truncate(%q, %d) = %q, want %q", tc.in, tc.max, got, tc.want)
            }
        })
    }
}

var sink string // keeps the result alive so the call is not optimised away

func BenchmarkTruncate(b *testing.B) {
    s := strings.Repeat("héllo ", 100)
    for i := 0; i < b.N; i++ {
        sink = Truncate(s, 20)
    }
}

func FuzzTruncate(f *testing.F) {
    f.Add("héllo", 2)
    f.Add("gopher", 3)
    f.Fuzz(func(t *testing.T, s string, max int) {
        if !utf8.ValidString(s) {
            t.Skip() // the rules below only make sense for valid UTF-8
        }
        got := Truncate(s, max)
        if !utf8.ValidString(got) {
            t.Errorf("invalid UTF-8 output %q", got)
        }
        if !strings.HasPrefix(s, got) {
            t.Errorf("%q is not a prefix of %q", got, s)
        }
        if max >= 0 && utf8.RuneCountInString(got) > max {
            t.Errorf("got %d runes, max %d", utf8.RuneCountInString(got), max)
        }
    })
}

Command တွေနဲ့ ရလဒ်ပုံစံက အောက်ပါအတိုင်းပါ။ Benchmark ဂဏန်းတွေက စက်တစ်လုံးနဲ့ တစ်လုံး ကွဲလို့ နမူနာ တစ်ခုအဖြစ်ပဲ ကြည့်ပါ။ Byte နဲ့ ဖြတ်တဲ့ Truncate ကို ဒီ fuzz test နဲ့ စစ်ရင် "héllo" seed က random input မထုတ်ခင်ကတည်းက ချက်ချင်း fail ဖြစ်ပါတယ်။ အဲဒီ seed ကို ဖယ်ထားရင်တောင် fuzzer က failing input တစ်ခုကို စက္ကန့်ပိုင်းအတွင်း ရှာတွေ့ပြီး testdata/fuzz/FuzzTruncate/ ထဲမှာ file အဖြစ် ရေးကာ ပြန် run ဖို့ go test -run command ကို ပြပေးပါတယ်။ အဲဒီ file ကို commit လုပ်ထားရင် ရိုးရိုး go test တိုင်းက အဲဒီ input ကို regression case အဖြစ် ထပ် run ပါတယ်။

bash
$ go test -run TestTruncate -v
=== RUN   TestTruncate
=== RUN   TestTruncate/cut_ascii
=== RUN   TestTruncate/multi-byte
--- PASS: TestTruncate (0.00s)
    --- PASS: TestTruncate/cut_ascii (0.00s)
    --- PASS: TestTruncate/multi-byte (0.00s)
PASS

$ go test -bench=Truncate -benchmem      # sample numbers, yours will differ
BenchmarkTruncate-8   156607   22386 ns/op   2720 B/op   2 allocs/op

$ go test -fuzz=FuzzTruncate -fuzztime=30s

-fuzz က fuzz test တစ်ခုကိုပဲ ရွေးရပါတယ်

-fuzz ရဲ့ pattern က package တစ်ခုထဲက fuzz test တစ်ခုတည်းနဲ့ပဲ ကိုက်ရပါတယ်။ Fuzzing က -fuzztime မပေးရင် failure တွေ့တဲ့အထိ ဒါမှမဟုတ် Ctrl+C နှိပ်တဲ့အထိ ဆက် run နေတာကိုလည်း သတိပြုပါ။

လက်တွေ့ project တွေမှာ

လက်တွေ့ project တွေမှာ

Go team တွေမှာ go test ./... က pull request တိုင်းရဲ့ အခြေခံ gate ပါ၊ go vet နဲ့ -race တို့နဲ့ တွဲပြီး CI မှာ run လေ့ရှိပါတယ်။ Testing package က အရိုးရှင်းဆုံး ဖြစ်အောင် ဒီဇိုင်းလုပ်ထားလို့ assertion library မပါဘဲ if နဲ့ t.Errorf ကိုပဲ သုံးတဲ့ project တွေ အများကြီး ရှိပါတယ်။

  • Parser, validator, encoder တွေလို input ကို ဖတ်တဲ့ code တွေအတွက် fuzz test က panic နဲ့ အမှားတွေကို ရှာပေးပြီး security ပြဿနာ မဖြစ်ခင် ဖမ်းပေးပါတယ်။
  • Performance ကို ပြင်တဲ့ PR မတင်ခင် benchmark ကို အပြောင်းအလဲ အရင်နဲ့ အပြီး run ပြီး ns/op နဲ့ allocs/op ကို နှိုင်းယှဉ်ပါတယ်။ ရလဒ် ငြိမ်အောင် အကြိမ်ကြိမ် run ပါ။
  • Bug report တစ်ခု ဝင်လာတိုင်း အဲဒီ bug ကို ပြန်ဖြစ်စေတဲ့ test case ကို table ထဲ အရင်ထည့်ပြီးမှ ပြင်ပါတယ်၊ ဒါမှ bug က နောက်တစ်ခါ ပြန်မလာပါဘူး။

အမှားများ

Testing မှာ ဖြစ်လေ့ရှိတဲ့ အမှားများ

Happy path တစ်ခုတည်းကို စစ်ခြင်း

Sum([]int{1, 2, 3}) == 6 ဆိုတဲ့ test တစ်ခုတည်းက nil slice, အနုတ် number, အရမ်းကြီးတဲ့ တန်ဖိုး တို့ကို ဘာမှ မပြောပါဘူး။ Table ထဲမှာ zero, empty, boundary, invalid input တွေကို case တစ်ခုစီ ထည့်ပါ။ Error ပြန်တဲ့ function တွေအတွက် wantErr ကိုလည်း column တစ်ခုအဖြစ် ထည့်ပါ။

Loop ထဲမှာ t.Fatalf သုံးပြီး ကျန်တဲ့ case တွေကို ဖုံးမိခြင်း

Subtest မပါတဲ့ loop ထဲမှာ t.Fatalf ကို ခေါ်ရင် ပထမ fail မှာ test တစ်ခုလုံး ရပ်လို့ ကျန်တဲ့ case တွေ မှန်လား မှားလား မသိရတော့ပါဘူး။ Case တစ်ခုစီကို t.Run ထဲ ထည့်ပါ ဒါမှမဟုတ် t.Errorf ကို သုံးပါ။

Compiler ဖယ်ပစ်လိုက်တဲ့ code ကို benchmark လုပ်ခြင်း

Benchmark loop ထဲက ရလဒ်ကို ဘယ်မှာမှ မသုံးရင် compiler က ခေါ်တာကို optimize လုပ်ပစ်နိုင်ပြီး မယုံနိုင်လောက်အောင် မြန်တဲ့ ns/op ရပါတယ်။ ရလဒ်ကို package-level variable ထဲ သိမ်းပါ။ Go 1.24 နဲ့ နောက်ပိုင်းမှာ ဒီပြဿနာကို ကာကွယ်ပေးတဲ့ for b.Loop() { ... } ပုံစံလည်း ရှိပါတယ်။

Mini challenge

Mini challenge: ParsePort ကို error case တွေပါ စစ်ပါ

"8080" လို text ကို TCP port number ပြောင်းပေးတဲ့ ParsePort ကို ရေးပြီး table-driven ပုံစံနဲ့ စစ်ပါ။ Number မဟုတ်တဲ့ text နဲ့ 1–65535 အပြင်ဘက် တန်ဖိုးတွေကို error နဲ့ ငြင်းရပြီး test table က error အမျိုးအစားကိုပါ errors.Is နဲ့ စစ်ရပါမယ်။

  • ParsePort(s string) (int, error) က strconv.Atoi ရဲ့ error ကို %w နဲ့ wrap လုပ်ရမယ်
  • Range အပြင်ဘက် တန်ဖိုးအတွက် ErrPortRange sentinel error ကို wrap လုပ်ပြီး ပြန်ပေးရမယ်
  • Table မှာ boundary (1, 65535, 0, 70000), number မဟုတ်တာ နဲ့ string အလွတ် case တွေ ပါရမယ်
  • Case တစ်ခုစီရဲ့ ရလဒ်နဲ့ failure အရေအတွက်ကို print ထုတ်ပါ
parse_port.go
package main

import (
    "errors"
    "fmt"
    "strconv"
)

var ErrPortRange = errors.New("port out of range")

func ParsePort(s string) (int, error) {
    // TODO: convert with strconv.Atoi and wrap its error using %w
    // TODO: reject values outside 1-65535 with a wrapped ErrPortRange
    return 0, nil
}

func main() {
    cases := []struct {
        in      string
        want    int
        wantErr error // nil means "expect success"
    }{
        // TODO: add boundary, invalid and empty-string cases
    }
    // TODO: loop, compare got/want and errors.Is(err, c.wantErr), count failures
    _ = cases
    fmt.Println("failures:", 0)
    _ = strconv.ErrSyntax
}

ရလဒ်: Case ခုနစ်ခုလုံး ok ဖြစ်ပြီး 0 နဲ့ 70000 က 'port out of range'၊ "http" နဲ့ "" က 'invalid syntax' error ကို ပြပါတယ်။ နောက်ဆုံးမှာ failures: 0 ကို print ထုတ်ပါတယ်။

အရိပ်အမြွက် ကြည့်ရန်

errors.Is(nil, nil) က true ပြန်ပြီး errors.Is(someErr, nil) က false ပြန်ပါတယ်။ ဒါကြောင့် ok := got == c.want && errors.Is(err, c.wantErr) ဆိုတဲ့ စစ်ဆေးချက် တစ်ခုတည်းက အောင်မြင်တဲ့ case ရော error case ရော နှစ်မျိုးလုံးကို ကိုင်တွယ်ပေးပါတယ်။ strconv.Atoi ရဲ့ error က strconv.ErrSyntax ကို wrap လုပ်ထားပါတယ်။

အဖြေ ကြည့်ရန်
parse_port.go
package main

import (
    "errors"
    "fmt"
    "strconv"
)

var ErrPortRange = errors.New("port out of range")

// ParsePort turns text such as "8080" into a TCP port number.
func ParsePort(s string) (int, error) {
    n, err := strconv.Atoi(s)
    if err != nil {
        return 0, fmt.Errorf("parse port %q: %w", s, err)
    }
    if n < 1 || n > 65535 {
        return 0, fmt.Errorf("parse port %q: %w", s, ErrPortRange)
    }
    return n, nil
}

func main() {
    cases := []struct {
        in      string
        want    int
        wantErr error
    }{
        {"8080", 8080, nil},
        {"1", 1, nil},
        {"65535", 65535, nil},
        {"0", 0, ErrPortRange},
        {"70000", 0, ErrPortRange},
        {"http", 0, strconv.ErrSyntax},
        {"", 0, strconv.ErrSyntax},
    }

    failures := 0
    for _, c := range cases {
        got, err := ParsePort(c.in)
        // errors.Is(nil, nil) is true, so success cases need no special branch
        ok := got == c.want && errors.Is(err, c.wantErr)
        if !ok {
            failures++
            fmt.Printf("FAIL ParsePort(%q) = %d, %v\n", c.in, got, err)
            continue
        }
        fmt.Printf("ok   ParsePort(%q) = %d, err=%v\n", c.in, got, err)
    }
    fmt.Println("failures:", failures)
}

Quick Quiz 1 / 3

Table-driven test တစ်ခုမှာ case ငါးခု ရှိပြီး subtest မပါတဲ့ loop ထဲမှာ t.Fatalf ကို သုံးထားပါတယ်။ ဒုတိယ case fail ဖြစ်ရင် ဘာဖြစ်မလဲ?

Quick Quiz 2 / 3

Fuzz test ထဲမှာ ဘယ်လို စစ်ဆေးချက်မျိုးက အသင့်တော်ဆုံးလဲ?

Quick Quiz 3 / 3

Fuzzer က FuzzTruncate မှာ failing input တစ်ခု တွေ့ပါတယ်။ အဲဒီ input ကို ဘယ်မှာ သိမ်းပြီး ဘယ်အချိန် ပြန် run မလဲ?

မှတ်ထားရမယ့် အချက်များ

  • Test တွေက _test.go file ထဲက TestXxx(t *testing.T) function တွေဖြစ်ပြီး go test က ရှာ run ပါတယ်။
  • Table-driven test နဲ့ t.Run က case အသစ်ထည့်ရ လွယ်ပြီး case တစ်ခုချင်း ရွေး run နိုင်စေပါတယ်။
  • Benchmark က b.N ကြိမ် run ပြီး ns/op ကို report လုပ်ပါတယ်၊ ဂဏန်းတွေက စက်အလိုက် ကွဲပါတယ်။
  • Fuzz test က property တစ်ခုကို random input တွေနဲ့ စစ်ပြီး ရှာတွေ့တဲ့ failure ကို testdata ထဲမှာ regression case အဖြစ် သိမ်းပါတယ်။

Getting started with fuzzing — Go

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Testing, Benchmarks & Fuzzing | Thuta Learning