1. 程式人生 > >golang 一些通用的單元測試模板

golang 一些通用的單元測試模板

以下模板參考了下 elasticsearch 包裡的單元測試, 可以很方便的接入到各種單元測試函式中,後續增加案例也很方便

假設待測試的函式如下:

//待測試函式======>校驗字串是否為數字
func IsInteger(s string) bool {
    if len(s) == 0 {
        return true
    }
    b, _ := regexp.MatchString("^[0-9]+$", s)
    return b
}

單元測試函式如下:

func TestIsInteger(t *testing.T) {
    tests := []struct
{ Input string Expected bool }{ { "123456789", true, }, { "a123456789", false, }, } for i, test := range tests { got := IsInteger(test.Input) if got != test.Expected { t.Errorf("TestIsInteger case #%d: expected %v; got: %v"
, i+1, test.Expected, got) } } }

對於測試函式的多個輸入值型別不確定的情況下,可以定義 interface{} 型別的陣列,然後在傳參的時候轉為相應型別

//待測試函式======>字串轉時間戳
func ParseTimeInProp(value string, vtype int32) (t int64, err error) {
    if value == "" {
        return 0, errors.New("value 為空")
    }
    ...
    return 
}

單元測試函式:

type
TestElement interface{} func TestParseTimeInProp2(t *testing.T) { tests := []struct { Input []TestElement Expected int64 }{ { []TestElement{`{"value": "2018-05-20"}`, 0}, 1526745600000, }, { []TestElement{`{"value": "2018-05-20 12:00"}`, 1}, 1526788800000, }, } for i, test := range tests { got, err := ParseTimeInProp(test.Input[0].(string), test.Input[1].(int32)) if err != nil || got != test.Expected { t.Errorf("TestParseTimeInProp case #%d: expected %v; got: %v, err=%v", i+1, test.Expected, got, err) } } }

接下來更復雜的測試用例的寫法,比如對結構體陣列排序,一組輸入資料,在不同的排序方式下對應多組輸出結果
對於以map形式輸入的資料,可以定義匿名結構體作為測試模板

func TestSortOrders(t *testing.T) {
    tests := struct {
        Input []*PurchaseOrder
        Case  []struct {
            sortType       int32
            Expected []*PurchaseOrder
        }
    }{
        Input: []*PurchaseOrder{
            &PurchaseOrder{
                Id:           proto.Int64(1),
                PurchaseTime: proto.Int64(111000),
                PayTime:      proto.Int64(888002),
            },
            &PurchaseOrder{
                Id:           proto.Int64(2),
                PurchaseTime: proto.Int64(111000),
                PayTime:      proto.Int64(888002),
            },
            &PurchaseOrder{
                Id:           proto.Int64(1),
                PurchaseTime: proto.Int64(222000),
                PayTime:      proto.Int64(888003),
            },
            &PurchaseOrder{
                Id:           proto.Int64(3),
                PurchaseTime: proto.Int64(333000),
                PayTime:      proto.Int64(888004),
            },
        },
        Case: []struct {
            sortType       int32
            Expected []*PurchaseOrder
        }{
            {
                sortType: int32(PURCHASE_TIME_ASC),    //優先 PurchaseTime 升序,其次 id 降序
                Expected: []*PurchaseOrder{
                    &PurchaseOrder{
                        Id:           proto.Int64(2),
                        PurchaseTime: proto.Int64(111000),
                        PayTime:      proto.Int64(888002),
                    },
                    &PurchaseOrder{
                        Id:           proto.Int64(1),
                        PurchaseTime: proto.Int64(111000),
                        PayTime:      proto.Int64(888002),
                    },
                    &PurchaseOrder{
                        Id:           proto.Int64(1),
                        PurchaseTime: proto.Int64(222000),
                        PayTime:      proto.Int64(888003),
                    },
                    &PurchaseOrder{
                        Id:           proto.Int64(3),
                        PurchaseTime: proto.Int64(333000),
                        PayTime:      proto.Int64(888004),
                    },
                },
            }, {
                sortType: int32(PURCHASE_TIME_DESC),    //優先 PurchaseTime 降序,其次 id 降序
                Expected: []*PurchaseOrder{
                    &PurchaseOrder{
                        Id:           proto.Int64(3),
                        PurchaseTime: proto.Int64(333000),
                        PayTime:      proto.Int64(888004),
                    },
                    &PurchaseOrder{
                        Id:           proto.Int64(1),
                        PurchaseTime: proto.Int64(222000),
                        PayTime:      proto.Int64(888003),
                    },
                    &PurchaseOrder{
                        Id:           proto.Int64(2),
                        PurchaseTime: proto.Int64(111000),
                        PayTime:      proto.Int64(888002),
                    },
                    &PurchaseOrder{
                        Id:           proto.Int64(1),
                        PurchaseTime: proto.Int64(111000),
                        PayTime:      proto.Int64(888002),
                    },
                },
            }, {
                sortType: int32(PAY_TIME_ASC),    //優先 PayTime 升序,其次 id 降序
                Expected: []*PurchaseOrder{
                    &PurchaseOrder{
                        Id:           proto.Int64(2),
                        PurchaseTime: proto.Int64(111000),
                        PayTime:      proto.Int64(888002),
                    },
                    &PurchaseOrder{
                        Id:           proto.Int64(1),
                        PurchaseTime: proto.Int64(111000),
                        PayTime:      proto.Int64(888002),
                    },
                    &PurchaseOrder{
                        Id:           proto.Int64(1),
                        PurchaseTime: proto.Int64(222000),
                        PayTime:      proto.Int64(888003),
                    },
                    &PurchaseOrder{
                        Id:           proto.Int64(3),
                        PurchaseTime: proto.Int64(333000),
                        PayTime:      proto.Int64(888004),
                    },
                },
            }, {
                sortType: int32(PAY_TIME_DESC),    //優先 PayTime 降序,其次 id 降序
                Expected: []*PurchaseOrder{
                    &PurchaseOrder{
                        Id:           proto.Int64(3),
                        PurchaseTime: proto.Int64(333000),
                        PayTime:      proto.Int64(888004),
                    },
                    &PurchaseOrder{
                        Id:           proto.Int64(1),
                        PurchaseTime: proto.Int64(222000),
                        PayTime:      proto.Int64(888003),
                    },
                    &PurchaseOrder{
                        Id:           proto.Int64(2),
                        PurchaseTime: proto.Int64(111000),
                        PayTime:      proto.Int64(888002),
                    },
                    &PurchaseOrder{
                        Id:           proto.Int64(1),
                        PurchaseTime: proto.Int64(111000),
                        PayTime:      proto.Int64(888002),
                    },
                },
            },
        },
    }

    for _, test := range tests.Case {
        purchaseOrder.SortOrders(test.st, tests.Input)
        for i, _ := range tests.Input {
            if tests.Input[i].GetPurchaseTime() == test.Expected[i].GetPurchaseTime() &&
                tests.Input[i].GetPayTime() == test.Expected[i].GetPayTime() &&
                tests.Input[i].GetId() == test.Expected[i].GetId() {
                t.Logf("ase #%d: pass", i+1)
            }
        }
    }
}