当前位置:网站首页>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
)
边栏推荐
- AI 考高数得分 81,网友:AI 模型也免不了“内卷”!
- Today's sleep quality record 77 points
- logback日志的整理
- 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
- Kivy教程之 如何自动载入kv文件
- RichView TRVStyle ListStyle 列表样式(项目符号编号)
- Tutoriel PowerPoint, comment enregistrer une présentation sous forme de vidéo dans Powerpoint?
- 【被动收入如何挣个一百万】
- [how to earn a million passive income]
- untiy世界边缘的物体阴影闪动,靠近远点的物体阴影正常
猜你喜欢

8 Queen question

MySQL installation, uninstallation, initial password setting and general commands of Linux

Box layout of Kivy tutorial BoxLayout arranges sub items in vertical or horizontal boxes (tutorial includes source code)

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

Introduction to the implementation principle of rxjs observable filter operator

今日睡眠质量记录77分

Mycms we media mall v3.4.1 release, user manual update

挡不住了,国产芯片再度突进,部分环节已进到4nm

HALCON联合C#检测表面缺陷——HALCON例程autobahn

Flink SQL knows why (19): the transformation between table and datastream (with source code)
随机推荐
SQL Injection (POST/Search)
父亲和篮球
windos 创建cordova 提示 因为在此系统上禁止运行脚本
Flutter dynamic | fair 2.5.0 new version features
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
mysql更新时条件为一查询
Task5: multi type emotion analysis
SQL Injection (POST/Select)
Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
Annotation and reflection
8皇后问题
Asp. Net core1.1 without project JSON, so as to generate cross platform packages
Open PHP error prompt under Ubuntu 14.04
Kivy教程之 如何通过字符串方式载入kv文件设计界面(教程含源码)
网上开户哪家证券公司佣金最低,我要开户,网上客户经理开户安全吗
Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases
CVPR 2022 | interpretation of 6 excellent papers selected by meituan technical team
SwiftUI 开发经验之作为一名程序员需要掌握的五个最有力的原则
Tutoriel PowerPoint, comment enregistrer une présentation sous forme de vidéo dans Powerpoint?
已解决TypeError: Argument ‘parser‘ has incorrect type (expected lxml.etree._BaseParser, got type)