许昌企业网站去哪开发口碑好的网站建设价格

当前位置: 首页 > news >正文

许昌企业网站去哪开发,口碑好的网站建设价格,新兴县城乡建设局网站登录,甘肃省城乡与建设厅网站首页文章目录 基础回顾MockMock是什么安装gomockMock使用1. 创建user.go源文件2. 使用mockgen生成对应的Mock文件3. 使用mockgen命令生成后在对应包mock下可以查看生成的mock文件4. 编写测试代码5. 运行代码并查看输出 GomonkeyGomonkey优势安装使用对函数进行monkey对结构体中方法… 文章目录 基础回顾MockMock是什么安装gomockMock使用1. 创建user.go源文件2. 使用mockgen生成对应的Mock文件3. 使用mockgen命令生成后在对应包mock下可以查看生成的mock文件4. 编写测试代码5. 运行代码并查看输出 GomonkeyGomonkey优势安装使用对函数进行monkey对结构体中方法进行monkey对全局变量进行monkey 基础回顾 测试 ● 建立完善的回归测试如果采用微服务可以限制测试范围只需要保证独立的微服务内部功能正常因为微服务对外暴露的是接口。 ● 为什么要将测试文件和源文件写到一个包中便于测试包级别的示例方法。 go test 命令是一个按照一定约定和组织的测试代码驱动测试在包目录下 所有以_test.go结尾的文件都会被视为测试文件。并且 _test.go结尾的文件不会被go build 编译到可执行文件中。 Mock Mock是什么 Mock是单元测试中常见的一种技术就是在测试过程中对于一些不容易构造或者获取的对象创建一个Mock对象来模拟对象的行为从而把测试与测试边界以外的对象隔离开。 优点 团队并行工作 测试驱动开发 TDD (Test-Driven Development) 测试覆盖率 隔离系统 缺点 Mock不是万能的使用Mock也存在着风险需要根据项目实际情况和具体需要来确定是否选用Mock。测试过程中如果大量使用Mockmock测试的场景失去了真实性可能会导致在后续的系统性测试时才发现bug使得缺陷发现的较晚可能会造成后续修复成本更大 Mock广泛应用于接口类的方法的生成。 针对接口可以使用mock针对不是接口的函数或者变量使用下面的monkey。 安装gomock go get github.com/golang/mock/gomock go get github.com/golang/mock/mockgen go install github.com/golang/mock/mockgenv1.6.0安装完成后执行mockgen命令查看是否生效 Mock使用

  1. 创建user.go源文件 // Package mock ———————————————————— // file : user.go // author : WeiTao // contact : 15537588047163.com // time : 2024/10/4 13:16 // ———————————————————— package mockimport contexttype 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方法实现黑盒从而无需关注具体实现细节直接可以设置此函数对应的返回值。
  2. 使用mockgen生成对应的Mock文件 mockgen使用 // 源码模式mockgen -source 需要mock的文件名 -destination 生成的mock文件名 -package 生成mock文件的包名 // 参考示例 mockgen -source user.go -destination./mock/user_mock.go -packagemock3. 使用mockgen命令生成后在对应包mock下可以查看生成的mock文件 // Code generated by MockGen. DO NOT EDIT. // Source: user.go// Package mock is a generated GoMock package. package mockimport (context contextreflect reflectgomock 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方法等。
  3. 编写测试代码 // ———————————————————— // package test // file : user_test.go // author : WeiTao // contact : 15537588047163.com // time : 2024/10/4 13:23 // ———————————————————— package mockimport (contextgithub.com/golang/mock/gomocktesting )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.vtest2json -test.paniconexit0 -test.run ^\QTestGetUserByMobile\E\(RUN TestGetUserByMobile --- PASS: TestGetUserByMobile (0.00s) PASSProcess finished with the exit code 0Gomonkey Gomonkey优势 上面使用mockgen生成对应的mock文件缺点非常明显只能对于接口类的函数进行mock然而实际项目并非所有函数都是接口类函数大部分是内部使用的临时函数以及变量等此时对这些函数以及变量无法使用mockgen生成对应的mock文件此时可以使用另一个工具gomonkey 链接https://github.com/agiledragon/gomonkey 安装 参考官网安装说明 \) go get github.com/agiledragon/gomonkey/v2v2.11.0使用 对函数进行monkey 编写函数 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 }编写测试用例 // 对函数进行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 -gcflagsall-l在Goland编辑器添加对应go运行变量-gcflagsall-l 加上 all-N -l和”all-l效果相同。 运行结果 /usr/local/go/bin/go tool test2json -t /home/wt/.cache/JetBrains/GoLand2024.2/tmp/GoLand/_7Test_compute_in_golearndetail_test_monkey.test -test.vtest2json -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 编写代码 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 }编写测试用例 // 对结构体中的方法进行mock func Test_ComputeNetworkCompute(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)} }运行结果 /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.vtest2json -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)} }