当前位置:网站首页>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
边栏推荐
- How to create an FTP server on the ECS? Is it safe to create an FTP server on the ECS?
- Are you ready for the exam preparation strategy of level II cost engineer in 2022?
- How RedHat 8 checks whether the port is connected
- 一款支持内网脱机分享文档的接口测试软件
- Activity recommendation | cloud native community meetup phase VII Shenzhen station begins to sign up!
- How to open the port of ECS what are the precautions for using ECS
- How to build a website for ECS? What are the prices of different ECS
- Advanced authentication of uni app [Day12]
- Find the current index of gbase 8C database?
- Introduction à la méthode de descente par Gradient - document d'apprentissage automatique pour les programmeurs de chevaux noirs
猜你喜欢

线性回归的损失和优化,机器学习预测房价

Apipost interface assertion details

解析后人类时代类人机器人的优越性

Abnova多肽设计和合成解决方案

梯度下降法介绍-黑马程序员机器学习讲义

少儿编程教育在特定场景中的普及作用

SAP MTS/ATO/MTO/ETO专题之十:ETO模式 Q+空模式 未估价库存 策略自定义

Detailed explanation of tcpip protocol
uni-app进阶之认证【day12】

Recognize workplus again, not only im but also enterprise mobile application management expert
随机推荐
What are the differences between ECs and virtual hosts? Which is better, ECS or VM?
What is the role of ECS? How does FTP connect to ECS configuration?
ribbon
Replication of variables in golang concurrency
2020年Android面试题汇总(初级)
IP and traffic reconciliation tool networktrafficview
RedHat 8 time synchronization and time zone modification
mini-Web框架:装饰器方式的添加路由 | 黑马程序员
Introduction to vulnerability priority technology (VPT)
Confluence data center version is nearing its lifecycle
DP summary of ACM in recent two weeks
Training methods after the reform of children's programming course
Real time monitoring: system and application level real-time monitoring based on flow computing Oceanus (Flink)
Web penetration test - 5. Brute force cracking vulnerability - (5) SMB password cracking
Recognize workplus again, not only im but also enterprise mobile application management expert
Advantages of fixed assets management system
让孩子们学习Steam 教育的应用精髓
What technology is VPS? How does the server VPS?
黑马程序员机器学习讲义:线性回归api初步使用
How to create an FTP server on the ECS? Is it safe to create an FTP server on the ECS?