golang 單元測試

hatlonely發表於2018-01-31

單元測試是質量保證十分重要的一環,好的單元測試不僅能及時地發現問題,更能夠方便地除錯,提高生產效率,所以很多人認為寫單元測試是需要額外的時間,會降低生產效率,是對單元測試最大的偏見和誤解

go 語言原生支援了單元測試,使用上非常簡單,測試程式碼只需要放到以 _test.go 結尾的檔案中即可。golang的測試分為單元測試和效能測試,單元測試的測試用例以 Test 開頭,效能測試以 Benchmark 開頭

舉個例子

實現排列組合函式對應的單元測試和效能測試

實現排列組合函式

// combination.go

package hmath

func combination(m, n int) int {
    if n > m-n {
        n = m - n
    }

    c := 1
    for i := 0; i < n; i++ {
        c *= m - i
        c /= i + 1
    }

    return c
}

實現單元測試和效能測試

// combination_test.go

package hmath

import (
    "math/rand"
    "testing"
)

// 單元測試
// 測試全域性函式,以TestFunction命名
// 測試類成員函式,以TestClass_Function命名
func TestCombination(t *testing.T) {
    // 這裡定義一個臨時的結構體來儲存測試case的引數以及期望的返回值
    for _, unit := range []struct {
        m        int
        n        int
        expected int
    }{
        {1, 0, 1},
        {4, 1, 4},
        {4, 2, 6},
        {4, 3, 4},
        {4, 4, 1},
        {10, 1, 10},
        {10, 3, 120},
        {10, 7, 120},
    } {
        // 呼叫排列組合函式,與期望的結果比對,如果不一致輸出錯誤
        if actually := combination(unit.m, unit.n); actually != unit.expected {
            t.Errorf("combination: [%v], actually: [%v]", unit, actually)
        }
    }
}

// 效能測試
func BenchmarkCombination(b *testing.B) {
    // b.N會根據函式的執行時間取一個合適的值
    for i := 0; i < b.N; i++ {
        combination(i+1, rand.Intn(i+1))
    }
}

// 併發效能測試
func BenchmarkCombinationParallel(b *testing.B) {
    // 測試一個物件或者函式在多執行緒的場景下面是否安全
    b.RunParallel(func(pb *testing.PB) {
        for pb.Next() {
            m := rand.Intn(100) + 1
            n := rand.Intn(m)
            combination(m, n)
        }
    })
}

執行測試

go test combination_test.go combination.go           # 單元測試
go test --cover combination_test.go combination.go   # 單元測試覆蓋率
go test -bench=. combination_test.go combination.go  # 效能測試

setup 和 teardown

setup 和 teardown 是在每個 case 執行前後都需要執行的操作,golang 沒有直接的實現,可以通過下面這個方法實現全域性的 setup 和 teardown,具體每個 case 的 setup 和 teardown 需要自己實現

func TestMain(m *testing.M) {
    // setup code...
    os.Exit(m.Run())
    // teardown code...
}

goconvey

這個第三方工具會自動幫我們跑測試,並且以非常友好的視覺化介面幫我們展示測試的結果,包括測試失敗的原因,測試覆蓋率等等,內部還提供了很多友好的斷言,能提高測試程式碼的可讀性

使用方法

go get github.com/smartystreets/goconvey

然後用終端在測試程式碼的目錄下執行 goconvey 命令即可

測試例子

package package_name

import (
    "testing"
    . "github.com/smartystreets/goconvey/convey"
)

func TestIntegerStuff(t *testing.T) {
    Convey("Given some integer with a starting value", t, func() {
        x := 1

        Convey("When the integer is incremented", func() {
            x++

            Convey("The value should be greater by one", func() {
                So(x, ShouldEqual, 2)
            })
        })
    })
}

參考連結

轉載請註明出處 本文連結:http://hatlonely.github.io/2018/01/31/golang-單元測試/

相關文章