当前位置:网站首页>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/gomonkey3,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
)
边栏推荐
- SQL Injection (POST/Select)
- [how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]
- 双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
- 【556. 下一个更大元素 III】
- Heap structure and heap sort heapify
- ThreadPoolExecutor realizes multi-threaded concurrency and obtains the return value (elegant and concise way)
- Multi table query of MySQL - multi table relationship and related exercises
- Task6: using transformer for emotion analysis
- JS convert pseudo array to array
- 【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】
猜你喜欢

今日睡眠质量记录77分

Flink SQL knows why (17): Zeppelin, a sharp tool for developing Flink SQL

使用Tensorflow进行完整的深度神经网络CNN训练完成图片识别案例2

使用tensorflow进行完整的DNN深度神经网络CNN训练完成图片识别案例

Complete deep neural network CNN training with tensorflow to complete picture recognition case 2

Flutter动态化 | Fair 2.5.0 新版本特性

Road construction issues

Logseq 评测:优点、缺点、评价、学习教程

Typeerror resolved: argument 'parser' has incorrect type (expected lxml.etree.\u baseparser, got type)
![[技术发展-24]:现有物联网通信技术特点](/img/f3/a219fe8e7438b8974d2226b4c3d4a4.png)
[技术发展-24]:现有物联网通信技术特点
随机推荐
Spark实战1:单节点本地模式搭建Spark运行环境
The shortage of graphics cards finally came to an end: 3070ti for more than 4000 yuan, 2000 yuan cheaper than the original price, and 3090ti
Software testing is so hard to find, only outsourcing offers, should I go?
JS 将伪数组转换成数组
The network card fails to start after the cold migration of the server hard disk
[技术发展-24]:现有物联网通信技术特点
Flutter动态化 | Fair 2.5.0 新版本特性
刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?
Stack application (balancer)
MapReduce implements matrix multiplication - implementation code
SQL Injection (POST/Select)
The difference between stratifiedkfold (classification) and kfold (regression)
106. 如何提高 SAP UI5 应用路由 url 的可读性
Red Hat Satellite 6:更好地管理服务器和云
掌握Cypress命令行选项,是真正掌握Cypress的基础
Ubuntu 14.04 下开启PHP错误提示
PowerPoint tutorial, how to save a presentation as a video in PowerPoint?
CVPR 2022 | 美团技术团队精选6篇优秀论文解读
道路建设问题
R语言使用data函数获取当前R环境可用的示例数据集:获取datasets包中的所有示例数据集、获取所有包的数据集、获取特定包的数据集