当前位置:网站首页>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
)
边栏推荐
- json序列化时案例总结
- pytorch 载入历史模型时更换gpu卡号,map_location设置
- Open PHP error prompt under Ubuntu 14.04
- Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases
- The latest BSC can pay dividends. Any B usdt Shib eth dividend destruction marketing can
- Spark practice 1: build spark operation environment in single node local mode
- Kivy tutorial how to load kV file design interface by string (tutorial includes source code)
- Flink SQL knows why (17): Zeppelin, a sharp tool for developing Flink SQL
- windos 创建cordova 提示 因为在此系统上禁止运行脚本
- [技术发展-24]:现有物联网通信技术特点
猜你喜欢

双向链表(我们只需要关注插入和删除函数)

用户和组命令练习

Introduction to the implementation principle of rxjs observable filter operator

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

Internet of things completion -- (stm32f407 connects to cloud platform detection data)

mysql更新时条件为一查询

Error running 'application' in idea running: the solution of command line is too long

MyCms 自媒体商城 v3.4.1 发布,使用手册更新

刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?

HALCON联合C#检测表面缺陷——HALCON例程autobahn
随机推荐
MySQL functions and related cases and exercises
Stack application (balancer)
Setting up remote links to MySQL on Linux
MyCms 自媒体商城 v3.4.1 发布,使用手册更新
There is nothing new under the sun. Can the meta universe go higher?
rxjs Observable filter Operator 的实现原理介绍
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
用户和组命令练习
常见的几种最优化方法Matlab原理和深度分析
Shell timing script, starting from 0, CSV format data is regularly imported into PostgreSQL database shell script example
json序列化时案例总结
Mobile phones and computers can be used, whole people, spoof code connections, "won't you Baidu for a while" teach you to use Baidu
HALCON联合C#检测表面缺陷——HALCON例程autobahn
[quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion
Flutter dynamic | fair 2.5.0 new version features
Replace the GPU card number when pytorch loads the historical model, map_ Location settings
The network card fails to start after the cold migration of the server hard disk
The reasons why there are so many programming languages in programming internal skills
[sort] bucket sort
Realize the recognition and training of CNN images, and process the cifar10 data set and other methods through the tensorflow framework