当前位置:网站首页>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
)
边栏推荐
- 刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?
- 8皇后问题
- NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
- [how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]
- Flink SQL knows why (19): the transformation between table and datastream (with source code)
- [how to earn a million passive income]
- 使用Tensorflow进行完整的深度神经网络CNN训练完成图片识别案例2
- [机缘参悟-37]:人感官系统的结构决定了人类是以自我为中心
- 全面发展数字经济主航道 和数集团积极推动UTONMOS数藏市场
- Disruptor -- a high concurrency and high performance queue framework for processing tens of millions of levels
猜你喜欢

SQL Injection (GET/Search)

Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases

Can newly graduated European college students get an offer from a major Internet company in the United States?

Introduction to the implementation principle of rxjs observable filter operator

刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?
![[机缘参悟-37]:人感官系统的结构决定了人类是以自我为中心](/img/06/b71b505c7072d540955fda6da1dc1b.jpg)
[机缘参悟-37]:人感官系统的结构决定了人类是以自我为中心

太阳底下无新事,元宇宙能否更上层楼?

PowerPoint tutorial, how to save a presentation as a video in PowerPoint?

RichView TRVStyle ListStyle 列表样式(项目符号编号)

Universal dividend source code, supports the dividend of any B on the BSC
随机推荐
Unity render streaming communicates with unity through JS
研发团队资源成本优化实践
Universal dividend source code, supports the dividend of any B on the BSC
Open PHP error prompt under Ubuntu 14.04
windos 创建cordova 提示 因为在此系统上禁止运行脚本
Brief analysis of tensorboard visual processing cases
Multi table query of MySQL - multi table relationship and related exercises
Students who do not understand the code can also send their own token, which is easy to learn BSC
掌握Cypress命令行选项,是真正掌握Cypress的基础
106. 如何提高 SAP UI5 应用路由 url 的可读性
CVPR 2022 | interpretation of 6 excellent papers selected by meituan technical team
Stack application (balancer)
R语言gt包和gtExtras包优雅地、漂亮地显示表格数据:nflreadr包以及gtExtras包的gt_plt_winloss函数可视化多个分组的输赢值以及内联图(inline plot)
Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
PowerPoint 教程,如何在 PowerPoint 中將演示文稿另存為視頻?
AI scores 81 in high scores. Netizens: AI model can't avoid "internal examination"!
71 articles on Flink practice and principle analysis (necessary for interview)
Several common optimization methods matlab principle and depth analysis
Replace the GPU card number when pytorch loads the historical model, map_ Location settings
Resource Cost Optimization Practice of R & D team