当前位置:网站首页>Go language unit test 4: go language uses gomonkey to test functions or methods
Go language unit test 4: go language uses gomonkey to test functions or methods
2022-07-03 13:45:00 【Lao Liu, you are so awesome】
One , Libraries used for installation
1,gomonkey The address of the code :
https://github.com/agiledragon/gomonkey
2, Install... From the command line gomonkey
[email protected]:~$ go get -u github.com/agiledragon/gomonkey
3,goconvey Code address of the library
https://github.com/smartystreets/goconvey
4, Install... From the command line
[email protected]:~$ go get -u github.com/smartystreets/goconvey
explain : Liu Hongdi's go The forest is a focus golang The blog of ,
Address :https://blog.csdn.net/weixin_43881017
explain : author : Liu Hongdi mailbox : [email protected]
Two , Information about the demonstration project
1, Project address :
https://github.com/liuhongdi/unittest03
2, Functional specifications : Demonstrates the use of gomonkey Pile driving when the library is tested in the project
3, Project structure : Pictured :
3、 ... and ,go Code instructions
1,model/user.go
package model
type MyUser struct {
name string
}
// Return the user's name
func (s *MyUser) GetUserName() string {
return s.name
}
2,main.go
package main
// Define an addition method
func Add(a, b int) int {
return a + b
}
// Define methods , Returns an integer 2 times
func GetDouble(a int) int {
return Add(a,a)
}
3,main_test.go
package main
import (
. "github.com/agiledragon/gomonkey"
"github.com/liuhongdi/unittest03/model"
. "github.com/smartystreets/goconvey/convey"
"reflect"
"testing"
)
// test double, by add Functions 1
func TestDoubleRight(t *testing.T) {
patch := ApplyFunc(Add, func(a,b int) int {
return a * 2
})
defer patch.Reset()
//fmt.Println(GetDouble(2))
Convey("test 2 x 2", t, func() {
So(GetDouble(2), ShouldEqual,4)
})
}
//add Stake of function
func addstub(a,b int) int {
return a*3
}
// test double, by add Functions 2
func TestDoubleError(t *testing.T) {
patch := ApplyFunc(Add, addstub)
defer patch.Reset()
//fmt.Println(GetDouble(2))
Convey("test 2 x 2", t, func() {
So(GetDouble(2), ShouldEqual,4)
})
}
// Test the method of piling 1, Back to right
func TestMethodRight(t *testing.T) {
var temp *model.MyUser
patch := ApplyMethod(reflect.TypeOf(temp), "GetUserName", func(_ *model.MyUser) string {
return "hello,world!"
})
defer patch.Reset()
Convey("GetUserName Will return :hello,world!", t, func() {
var user *model.MyUser
user = new(model.MyUser)
So(user.GetUserName(), ShouldEqual, "hello,world!")
})
}
// Test the method of piling 2, Returns an error
func TestMethodError(t *testing.T) {
var temp *model.MyUser
patch := ApplyMethod(reflect.TypeOf(temp), "GetUserName", func(_ *model.MyUser) string {
return "hello, Lao Liu !"
})
defer patch.Reset()
Convey("GetUserName Will return :hello,world!", t, func() {
var user *model.MyUser
user = new(model.MyUser)
So(user.GetUserName(), ShouldEqual, "hello,world!")
})
}
Four , The test results
Perform the test :
[email protected]:/data/go/unittest03# go test -v ./... -gcflags "all=-N -l"
Be careful : Add parameter :-gcflags "all=-N -l"
Otherwise, piling may not take effect
return :
5、 ... and , View the library used :
module github.com/liuhongdi/unittest03
go 1.15
require (
github.com/smartystreets/goconvey v1.6.4
github.com/agiledragon/gomonkey v2.0.2+incompatible
)
边栏推荐
- 服务器硬盘冷迁移后网卡无法启动问题
- 刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?
- rxjs Observable filter Operator 的实现原理介绍
- Unity render streaming communicates with unity through JS
- 使用tensorflow进行完整的DNN深度神经网络CNN训练完成图片识别案例
- Realize the recognition and training of CNN images, and process the cifar10 data set and other methods through the tensorflow framework
- 双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
- 掌握Cypress命令行选项,是真正掌握Cypress的基础
- When updating mysql, the condition is a query
- Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases
猜你喜欢
[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]
There is nothing new under the sun. Can the meta universe go higher?
又一个行业被中国芯片打破空白,难怪美国模拟芯片龙头降价抛售了
Logseq evaluation: advantages, disadvantages, evaluation, learning tutorial
Flutter动态化 | Fair 2.5.0 新版本特性
[quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion
The difference between stratifiedkfold (classification) and kfold (regression)
PowerPoint 教程,如何在 PowerPoint 中將演示文稿另存為視頻?
TensorBoard可视化处理案例简析
User and group command exercises
随机推荐
Kivy教程之 盒子布局 BoxLayout将子项排列在垂直或水平框中(教程含源码)
Start signing up CCF C ³- [email protected] chianxin: Perspective of Russian Ukrainian cyber war - Security confrontation and sanctions g
RichView TRVStyle ListStyle 列表样式(项目符号编号)
[机缘参悟-37]:人感官系统的结构决定了人类是以自我为中心
使用tensorflow进行完整的DNN深度神经网络CNN训练完成图片识别案例
The solution of Chinese font garbled code in keil5
Stack application (balancer)
刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?
The difference between stratifiedkfold (classification) and kfold (regression)
Complete deep neural network CNN training with tensorflow to complete picture recognition case 2
Bidirectional linked list (we only need to pay attention to insert and delete functions)
pytorch 载入历史模型时更换gpu卡号,map_location设置
The shadow of the object at the edge of the untiy world flickers, and the shadow of the object near the far point is normal
使用Tensorflow进行完整的深度神经网络CNN训练完成图片识别案例2
Brief analysis of tensorboard visual processing cases
SQL Injection (GET/Select)
常见的几种最优化方法Matlab原理和深度分析
Typeerror resolved: argument 'parser' has incorrect type (expected lxml.etree.\u baseparser, got type)
Flutter动态化 | Fair 2.5.0 新版本特性
[技术发展-24]:现有物联网通信技术特点