天天看点

Go单元测试(gomock)

摘要:​​​​​​使用Golang的官方mock工具--gomock - 简书

摘要2:GoMock框架使用指南 - 简书

摘要3:用gomock进行mock测试_weixin_34087307的博客-CSDN博客

摘要4:gomonkey框架:Golang mock 框架实践_baijiwei的博客-CSDN博客_golang mock

项目结构:

Go单元测试(gomock)

1.期望输入1,返回1

stub_test.go

packege trygomock

import (
    "github.com/golang/mock/gomock"
    "testing"
    "unit_test_class/mock"
)


func TestStub(t *testing.T) {

    ctl := gomock.NewController(t)
    defer ctl.Finish()

    client := mock.NewMockMySQLClient(ctl)  // mock包中通过interface接口生成的mock函数
    // 期望输入1,返回1
    client.EXPECT().Exec(1).Return(2)

    x := Stub(client, 1) 
    if x != 2 {
        t.Fatal()
    }
}
           

stub.go

packege trygomock

import "fmt"

func Stub(client MySQLClient, x int) int {
    if client == nil {
        return -1
    }

    res := client.Exec(x)
    fmt.Println(""res:", res)

    return res
}
           

myclient.go

packege trygomock

typr MySQLClient interface {
    Exec(x int) int
}
           

myclientimpl.go

package trygomock 

type Client struct {
    
}

func (c *Client) Exec(x int) int {
    i := x

    return i
}
           

2.强制输入指定数据,否者报错

stub_test.go

packege trygomock

import (
    "github.com/golang/mock/gomock"
    "testing"
    "unit_test_class/mock"
)


func TestStub2(t *testing.T) {

    ctl := gomock.NewController(t)
    defer ctl.Finish()

    client := mock.NewMockMySQLClient(ctl)  // mock包中通过interface接口生成的mock函数
    // 强制输入888,否则报错
    client.EXPECT().Exec(gomock.Eq(888)).Return(2)  // 强制指定输入参数为888

    x := Stub(client, 888) 
    if x != 2 {
        t.Fatal()
    }
}
           

3.无论输入参数是什么,结果都返回指定值

stub_test.go

packege trygomock

import (
    "github.com/golang/mock/gomock"
    "testing"
    "unit_test_class/mock"
)


func TestStub3(t *testing.T) {

    ctl := gomock.NewController(t)
    defer ctl.Finish()

    client := mock.NewMockMySQLClient(ctl)  // mock包中通过interface接口生成的mock函数
    // 输入参数都可以,返回结果值必须是指定的值
    client.EXPECT().Exec(gomock.Any()).Return(999)  // 指定必须返回999

    x := Stub(client, 3)  // 这里随便什么参数都行
    if x != 999 {
        t.Fatal()
    }
}
           

4.整合测试用例(打桩)

stub_test.go

packege trygomock

import (
    "github.com/golang/mock/gomock"
    "testing"
    "unit_test_class/mock"
)


func TestStub4(t *testing.T) {
    testCase := []struct {
        Name string
        GetParam func() (int, int)
        Stub func(ctl *gomock.Controller) MySQLClient
    }{
        {
            Name : "期望输入1, 返回1",
            Stub : func(ctl *gomock.Controller) *MySQLClient {
                client := mock.NewMockMySQLClient(ctl)  
                client.EXPECT().Exec(1).Return(1)
                return client 
            },
            GetParam : func() (int, int){
                return 1, 1
            }
        },
        {
            Name : "强制输入888,否则报错",
            Stub : func(ctl *gomock.Controller) *MySQLClient {
                client := mock.NewMockMySQLClient(ctl)  
                client.EXPECT().Exec(gomock.Eq(888)).Return(2)
                return client 
            },
            GetParam : func() (int, int){
                return 888, 2
            }
        }
    }

    for _, ut := range TestCase {
        t.Run(ut.Name, func(t *testing.T) {
            ctl := gomock.NewController(t)
            defer ctl.Finish()

            client := ut.Stub(ctl)

            x := Stub(client, 3)  
            if x != 999 {
                t.Fatal()
            }
        })
    }
}
           

继续阅读