当前位置:网站首页>Use of go testing framework gomock
Use of go testing framework gomock
2022-06-24 04:54:00 【Johns】
One . Introduce
gomock yes golang Interface level of official development and maintenance mock programme , Contains GoMock Bao He mockgen The tools are two parts , among GoMock Package completes the life cycle management of pile objects ,mockgen Tools are used to generate interface Corresponding Mock Class source file . To use gomock A prerequisite of is that modules must rely on each other through interfaces , Instead of relying on concrete implementation , otherwise mock It will be very difficult . This tool is not widely used in the industry , The main reason is that there are too many limitations , So we just need to know how to use it .
github Address :https://github.com/golang/mock
Two . Use
1. install
go get -u github.com/golang/mock/gomock go install github.com/golang/mock/mockgen
2. Quick start
First step : Defining interfaces , Note that this tool supports interface generation mock, Other types of methods do not support .
package dao
import (
"fmt"
_ "github.com/golang/mock/mockgen/model"
)
// User persistence layer operation object
type UserDao interface {
Update(id, name, phoneNumber string) int64
Update2(param ...string) int64
Add(name, phoneNumber string) (int64, error)
Select(id string) int64
Delete(id string) int64
}
type userDao struct{}
func (u userDao) Update(id, name, phoneNumber string) int64 {
fmt.Println(id, name, phoneNumber)
return 1
}The second step : Build with command mock class
mockgen -destination mock_user_dao.go -package dao -source user_dao.go
If the command is too long , Can be directly in UserDao Add one to it go:generate The comments are as follows
package dao
import (
"fmt"
_ "github.com/golang/mock/mockgen/model"
)
//go:generate mockgen -destination mock_user_dao.go -package dao -source user_dao.go
// User persistence layer operation object
type UserDao interface {
Update(id, name, phoneNumber string) int64
Update2(param ...string) int64
Add(name, phoneNumber string) (int64, error)
Select(id string) int64
Delete(id string) int64
}
... Then in the directory of the current package bash perform go generate It can also generate mock Of documents .
The third step : Customize mock Implement and assert
package dao
import (
"errors"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"testing"
)
/**
* @Description
* @Author guirongguo
* @Email
* @Date 2021/8/31 20:37
**/
func Test_userDao(t *testing.T) {
// step1. Initialize a mock controller
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockObj := NewMockUserDao(mockCtrl)
// step2. Definition mock The concrete execution logic of
// 1. Parameter support Eq,Any,Not,Nil
//Eq(value) To express with value Equivalent value .
//Any() Can be used to represent any input parameter .
//Not(value) Used to indicate non value Value beyond .
//Nil() Express None value
//
// 2.mock The number of method calls is supported as follows
//Times() Assertion Mock The number of times the method was called .
//MaxTimes() Maximum number of times .
//MinTimes() The minimum number of times .
//AnyTimes() Any number of times ( Include 0 Time )
//
// 3. Return value support
// Return Returns the determined value
// Do Mock Method is called , What to do , Ignore return value .
// DoAndReturn You can dynamically control the return value .
c1 := mockObj.EXPECT().Update("001", "ggr", "10010").Return(int64(1))
c2 := mockObj.EXPECT().Update("001", "ggr", gomock.Any()).Return(int64(1))
mockObj.EXPECT().Add("", "1001").Return(int64(0), errors.New("name is invalid")).AnyTimes()
mockObj.EXPECT().Add("1001", gomock.Not("1001")).Return(int64(0), errors.New("phone_number is invalid")).MinTimes(1)
mockObj.EXPECT().Add(gomock.Eq("1001"), "1001").Return(int64(0), errors.New("name is empty")).Times(1)
mockObj.EXPECT().Select(gomock.Any()).Return(int64(1)).MaxTimes(2)
mockObj.EXPECT().Update2(gomock.Any()).Return(int64(1)).Times(2)
mockObj.EXPECT().Delete("1001").Do(func(i string) {
t.Log("delete id=1001")
})
mockObj.EXPECT().Delete(gomock.Not("1001")).DoAndReturn(func(i string) int64 {
return int64(1)
})
// step3. Limit stub The call order of the function , If the order is inconsistent , False report
gomock.InOrder(c1, c2)
// step4. Test verification
ret1 := mockObj.Update("001", "ggr", "10010")
ret2 := mockObj.Update("001", "ggr", "10020")
ret3 := mockObj.Update2("1", "")
ret4 := mockObj.Update2("")
ret5 := mockObj.Select("")
ret6, err1 := mockObj.Add("1001", "1001")
ret7, err2 := mockObj.Add("1001", "1002")
ret8 := mockObj.Delete("1001")
ret9 := mockObj.Delete("1002")
// step5. Assertion
assert.Equal(t, ret1, int64(1))
assert.Equal(t, ret2, int64(1))
assert.Equal(t, ret3, int64(1))
assert.Equal(t, ret4, int64(1))
assert.Equal(t, ret5, int64(1))
assert.Equal(t, ret6, int64(0))
assert.Equal(t, ret7, int64(0))
assert.Equal(t, ret8, int64(0))
assert.Equal(t, ret9, int64(1))
assert.NotNil(t, err1)
assert.NotEmpty(t, err2)
}Customize mock The implementation mainly includes user-defined parameters , Custom return value , Customize mock Number of calls and order of calls .
(1) Custom parameters
Parameter support Eq,Any,Not,Nil, Represent the meaning of :
- Eq(value) Used in scenarios with fixed parameters .
- Any() For scenarios with arbitrary parameters .
- Not(value) Used to indicate that the parameter is not value Other value scenarios .
- Nil() Used to represent parameters None Value scenarios
(2) Custom return value
The return value supports the following :
- Return The scenario used to return the determined value
- Do For scenarios with no return value .
- DoAndReturn Used to dynamically control the return value .
(3) Customize mock Call the number
mock The number of calls supports the following scenarios :
- Times() Assertion Mock The number of times the method was called , Number of times .
- MaxTimes() Maximum number of times .
- MinTimes() The minimum number of times .
- AnyTimes() Any number of times ( Include 0 Time )
(4) Customize mock Call to order
When there are multiple mock When calling each other , You can go through 2 How to define mock Order of execution :
- Directly after the function
After - Use
gomock.InOrderSet execution order
Please refer to the official documentation for more usage :https://pkg.go.dev/github.com/golang/mock/gomock#pkg-examples
边栏推荐
- What is an evpn switch?
- mini-Web框架:装饰器方式的添加路由 | 黑马程序员
- Activity recommendation | cloud native community meetup phase VII Shenzhen station begins to sign up!
- What are the advantages of ECS? Is ECS better than VM?
- getAttribute 返回值为null
- RedHat 8 time synchronization and time zone modification
- 什么是数据中台
- Bi-sql insert into
- event
- 5g and industrial Internet
猜你喜欢

Are you ready for the exam preparation strategy of level II cost engineer in 2022?

少儿编程课程改革后的培养方式

解析后人类时代类人机器人的优越性
Advanced authentication of uni app [Day12]

梯度下降法介紹-黑馬程序員機器學習講義

Detailed explanation of tcpip protocol

Idea creates a servlet and accesses the 404 message

Training methods after the reform of children's programming course

SAP mts/ato/mto/eto topic 10: ETO mode q+ empty mode unvalued inventory policy customization

Abnova membrane protein lipoprotein solution
随机推荐
Bi-sql order by
How does ECS build websites? Is it troublesome for ECs to build websites?
Introduction to gradient descent method - black horse programmer machine learning handout
Real time monitoring: system and application level real-time monitoring based on flow computing Oceanus (Flink)
Bi-sql where
重新认识WorkPlus,不止IM即时通讯,是企业移动应用管理专家
How to control CDN traffic gracefully in cloud development?
How to open the port of ECS what are the precautions for using ECS
DP summary of ACM in recent two weeks
数据库解答建标,按要求回答
What is an evpn switch?
What is the new generation cloud computing architecture cipu of Alibaba cloud?
Digital transformation practice of Zheshang Bank
Introduction to vulnerability priority technology (VPT)
Weak current engineer, 25g Ethernet and 40g Ethernet: which do you choose?
让孩子们学习Steam 教育的应用精髓
SAP MTS/ATO/MTO/ETO专题之十:ETO模式 Q+空模式 未估价库存 策略自定义
Analysis on the subjective enthusiasm of post-90s makers' Education
Mini web framework: adding routes in decorator mode | dark horse programmer
Introduction à la méthode de descente par Gradient - document d'apprentissage automatique pour les programmeurs de chevaux noirs