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)
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/u011228889/article/details/80813784