Go基础学习11-测试工具gomock和monkey的使用

server/2024/10/20 15:02:20/

文章目录

  • 基础回顾
  • Mock
    • Mock是什么
    • 安装gomock
    • Mock使用
      • 1. 创建user.go源文件
      • 2. 使用mockgen生成对应的Mock文件
      • 3. 使用mockgen命令生成后在对应包mock下可以查看生成的mock文件
      • 4. 编写测试代码
      • 5. 运行代码并查看输出
  • Gomonkey
    • Gomonkey优势
    • 安装
    • 使用
      • 对函数进行monkey
      • 对结构体中方法进行monkey
      • 对全局变量进行monkey

基础回顾

测试:
在这里插入图片描述在这里插入图片描述
● 建立完善的回归测试,如果采用微服务,可以限制测试范围,只需要保证独立的微服务内部功能正常,因为微服务对外暴露的是接口。
● 为什么要将测试文件和源文件写到一个包中:便于测试包级别的示例,方法。

  • go test 命令是一个按照一定约定和组织的测试代码驱动测试,在包目录下 所有以_test.go结尾的文件都会被视为测试文件。并且 _test.go结尾的文件不会被go build 编译到可执行文件中。

Mock

Mock是什么

Mock是单元测试中常见的一种技术,就是在测试过程中,对于一些不容易构造或者获取的对象,创建一个Mock对象来模拟对象的行为,从而把测试与测试边界以外的对象隔离开。
优点:
团队并行工作
测试驱动开发 TDD (Test-Driven Development)
测试覆盖率
隔离系统
缺点

  • Mock不是万能的,使用Mock也存在着风险,需要根据项目实际情况和具体需要来确定是否选用Mock。
  • 测试过程中如果大量使用Mock,mock测试的场景失去了真实性,可能会导致在后续的系统性测试时才发现bug,使得缺陷发现的较晚,可能会造成后续修复成本更大

Mock广泛应用于接口类的方法的生成。 针对接口可以使用mock,针对不是接口的函数或者变量使用下面的monkey。

mock_22">安装gomock

go get github.com/golang/mock/gomock
go get github.com/golang/mock/mockgen
go install github.com/golang/mock/mockgen@v1.6.0

安装完成后执行mockgen命令查看是否生效

Mock使用

1. 创建user.go源文件

// Package mock  ------------------------------------------------------------
// @file      : user.go
// @author    : WeiTao
// @contact   : 15537588047@163.com
// @time      : 2024/10/4 13:16
// ------------------------------------------------------------
package mockimport "context"type User struct {Mobile   stringPassword stringNickName string
}type UserServer struct {Db UserData
}func (us *UserServer) GetUserByMobile(ctx context.Context, mobile string) (User, error) {user, err := us.Db.GetUserByMobile(ctx, mobile)if err != nil {return User{}, err}if user.NickName == "TestUser" {user.NickName = "TestUserModified"}return user, nil
}type UserData interface {GetUserByMobile(ctx context.Context, mobile string) (User, error)
}

上述代码中的UserData是一个Interface{}类型,后面使用Mock生成的对象就是此接口对象,生成后可以将UserData接口中的方法GetUserByMoblie方法实现黑盒,从而无需关注具体实现细节,直接可以设置此函数对应的返回值。

mockgenMock_69">2. 使用mockgen生成对应的Mock文件

mockgen使用:

// 源码模式mockgen -source 需要mock的文件名 -destination 生成的mock文件名 -package 生成mock文件的包名
// 参考示例:
mockgen -source user.go -destination=./mock/user_mock.go -package=mock

mockgenmockmock_79">3. 使用mockgen命令生成后在对应包mock下可以查看生成的mock文件

// Code generated by MockGen. DO NOT EDIT.
// Source: user.go// Package mock is a generated GoMock package.
package mockimport (context "context"reflect "reflect"gomock "github.com/golang/mock/gomock"
)// MockUserData is a mock of UserData interface.
type MockUserData struct {ctrl     *gomock.Controllerrecorder *MockUserDataMockRecorder
}// MockUserDataMockRecorder is the mock recorder for MockUserData.
type MockUserDataMockRecorder struct {mock *MockUserData
}// NewMockUserData creates a new mock instance.
func NewMockUserData(ctrl *gomock.Controller) *MockUserData {mock := &MockUserData{ctrl: ctrl}mock.recorder = &MockUserDataMockRecorder{mock}return mock
}// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockUserData) EXPECT() *MockUserDataMockRecorder {return m.recorder
}// GetUserByMobile mocks base method.
func (m *MockUserData) GetUserByMobile(ctx context.Context, mobile string) (User, error) {m.ctrl.T.Helper()ret := m.ctrl.Call(m, "GetUserByMobile", ctx, mobile)ret0, _ := ret[0].(User)ret1, _ := ret[1].(error)return ret0, ret1
}// GetUserByMobile indicates an expected call of GetUserByMobile.
func (mr *MockUserDataMockRecorder) GetUserByMobile(ctx, mobile interface{}) *gomock.Call {mr.mock.ctrl.T.Helper()return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUserByMobile", reflect.TypeOf((*MockUserData)(nil).GetUserByMobile), ctx, mobile)
}

观察上述Mock文件可以看到实现了GetUserByMobile方法等。

4. 编写测试代码

// ------------------------------------------------------------
// package test
// @file      : user_test.go
// @author    : WeiTao
// @contact   : 15537588047@163.com
// @time      : 2024/10/4 13:23
// ------------------------------------------------------------
package mockimport ("context""github.com/golang/mock/gomock""testing"
)func TestGetUserByMobile(t *testing.T) {// mock准备工作ctl := gomock.NewController(t)defer ctl.Finish()userData := NewMockUserData(ctl)userData.EXPECT().GetUserByMobile(gomock.Any(), "15023076751").Return(User{Mobile:   "15023076751",Password: "123456",NickName: "TestUser",},nil,)// 实际调用过程userServer := UserServer{Db: userData,}user, err := userServer.GetUserByMobile(context.Background(), "15023076751")// 判断过程if err != nil {t.Error("GetUserByMobile error:", err)}if user.Mobile != "15023076751" || user.Password != "123456" || user.NickName != "TestUserModified" {t.Error("GetUserByMobile result is not expected.")}
}

5. 运行代码并查看输出

GOROOT=/usr/local/go #gosetup
GOPATH=/home/wt/Backend/go/goprojects #gosetup
/usr/local/go/bin/go test -c -o /home/wt/.cache/JetBrains/GoLand2024.2/tmp/GoLand/___TestGetUserByMobile_in_golearndetail_test_mock.test golearndetail/test/mock #gosetup
/usr/local/go/bin/go tool test2json -t /home/wt/.cache/JetBrains/GoLand2024.2/tmp/GoLand/___TestGetUserByMobile_in_golearndetail_test_mock.test -test.v=test2json -test.paniconexit0 -test.run ^\QTestGetUserByMobile\E$
=== RUN   TestGetUserByMobile
--- PASS: TestGetUserByMobile (0.00s)
PASSProcess finished with the exit code 0

Gomonkey

Gomonkey优势

上面使用mockgen生成对应的mock文件缺点非常明显,只能对于接口类的函数进行mock,然而实际项目并非所有函数都是接口类函数,大部分是内部使用的临时函数以及变量等,此时对这些函数以及变量无法使用mockgen生成对应的mock文件,此时可以使用另一个工具gomonkey
链接:https://github.com/agiledragon/gomonkey

安装

参考官网安装说明:

$ go get github.com/agiledragon/gomonkey/v2@v2.11.0

使用

对函数进行monkey

  1. 编写函数
package monkeyfunc networkCompute(a, b int) (int, error) {c := a + breturn c, nil
}func compute(a, b int) (int, error) {c, err := networkCompute(a, b)return c, err
}
  1. 编写测试用例
// 对函数进行mock
func Test_compute(t *testing.T) {patches := gomonkey.ApplyFunc(networkCompute, func(a, b int) (int, error) {return 8, nil})defer patches.Reset()sum, err := compute(1, 2)if err != nil {t.Error("Error occurred:", err)}fmt.Printf("sum : %d\n", sum)if sum != 8 {t.Error("Error occurred in sum:", err)}
}

在使用gomonkey运行测试用例的时候,直接run会报内联错误,解决方法有两个:

  • 在终端执行命令go test时加上参数:go test -gcflags=all=-l
  • 在Goland编辑器添加对应go运行变量:-gcflags=all=-l
  • 在这里插入图片描述
    加上 "all=-N -l"和”=all=-l"效果相同。
  1. 运行结果
/usr/local/go/bin/go tool test2json -t /home/wt/.cache/JetBrains/GoLand2024.2/tmp/GoLand/___7Test_compute_in_golearndetail_test_monkey.test -test.v=test2json -test.paniconexit0 -test.run ^\QTest_compute\E$
=== RUN   Test_compute
sum : 8
--- PASS: Test_compute (0.00s)
PASSProcess finished with the exit code 0

对结构体中方法进行monkey

  1. 编写代码
type Compute struct{}func (c *Compute) NetworkCompute(a, b int) (int, error) {sum := a + breturn sum, nil
}func (c *Compute) Compute(a, b int) (int, error) {sum, err := c.NetworkCompute(a, b)return sum, err
}
  1. 编写测试用例
// 对结构体中的方法进行mock
func Test_Compute_NetworkCompute(t *testing.T) {var compute *Computepatches := gomonkey.ApplyMethod(reflect.TypeOf(compute), "NetworkCompute", func(_ *Compute, a, b int) (int, error) {return 10, nil})defer patches.Reset()compute = &Compute{}sum, err := compute.Compute(3, 2)if err != nil {t.Error("Error occurred:", err)}fmt.Printf("sum : %d\n", sum)if sum != 10 {t.Error("Error occurred in sum:", err)}
}
  1. 运行结果
/usr/local/go/bin/go tool test2json -t /home/wt/.cache/JetBrains/GoLand2024.2/tmp/GoLand/___2Test_Compute_NetworkCompute_in_golearndetail_test_monkey.test -test.v=test2json -test.paniconexit0 -test.run ^\QTest_Compute_NetworkCompute\E$
=== RUN   Test_Compute_NetworkCompute
sum : 10
--- PASS: Test_Compute_NetworkCompute (0.00s)
PASSProcess finished with the exit code 0

对全局变量进行monkey

代码展示:

var num = 5// 对变量进行mock
func Test_GlobalVal(t *testing.T) {patches := gomonkey.ApplyGlobalVar(&num, 10)defer patches.Reset()if num != 10 {t.Error("Error occurred in num:mock failure", num)}
}

http://www.ppmy.cn/server/127985.html

相关文章

【AGC005D】~K Perm Counting(计数抽象成图)

容斥原理。 求出f(m) ,f(m)指代至少有m个位置不合法的方案数。 怎么求? 注意到位置为id,权值为v ,不合法的情况,当且仅当 v idk或 v id-k 因此,我们把每一个位置和权值抽象成点 ,不合法的情况之间连一…

Spring IoC笔记

目录 1.什么是 IoC? 2.IoC类注解(五大注解) 2.1那为什么要这么多类注解? 2.2五大注解是不是可以混用? 2.3程序被spring管理的条件是? 3.bean对象 3.1Bean 命名约定 3.2获取bean对象 4.⽅法注解 B…

STM32 OLED

文章目录 前言一、OLED是什么?二、使用步骤1.复制 OLED.C .H文件1.1 遇到问题 2.统一风格3.主函数引用头文件3.1 oled.h 提供了什么函数 4.介绍显示一个字符的函数5. 显示十进制函数的讲解 三、使用注意事项3.1 配置符合自己的引脚3.2 花屏总结 前言 提示&#xff…

TIM“PWM”输出比较原理解析

PWM最重要的就是占空比,所有都是在为占空比服务,通过设置不同的占空比,产生不同的电压,产生不同的效果 定时器的输出通道 基本定时器: 基本定时器没有通道 通用定时器: 4个通道(CH1, CH2, C…

【JWT安全】portswigger JWT labs 全解

目录 1.利用有缺陷的 JWT 签名验证 ①接受任意签名 lab1:通过未验证的签名绕过 JWT 身份验证 ②接受无签名的token lab2:通过有缺陷的签名验证来绕过 JWT 身份验证 2.暴力破解密钥 ①使用hashcat lab3:通过弱签名密钥绕过 JWT 身份验证 3.JWT 标头参数注入 ①通过 jwk…

Hive数仓操作(三)

一、Hive 数据库操作 1. 创建数据库 基本创建数据库命令: CREATE DATABASE bigdata;说明: 数据库会在 HDFS 中以目录的形式创建和保存,数据库名称会存储在 Hive 的元数据中。如果不指定目录,数据库将在 /user/hive/warehouse 下…

使用PaddleHub智能生成,献上浓情国庆福

使用PaddleHub完成国庆祝福 祝福国家繁荣 本项目作为示例演示项目,用于体验飞桨平台的高效与便利。 各部分详细内容请点击相应链接学习 数据介绍 数据集收集自网络,包括100条国庆祝福。数据以txt形式提供,一条数据为一行。 国庆佳节&#x…

Python异常处理中的9个常见错误及其解决办法,建议收藏

在Python编程中,异常处理是确保程序健壮性和可靠性的重要部分。然而,在使用异常处理时,开发者可能会犯一些常见的错误。以下是9个常见的异常处理错误及其解决办法: 1. 语法错误 (SyntaxError) 语法错误是最常见的错误之一。它通…