go语言单元测试之四:go语言用gomonkey为测试函数或方法打桩

一,安装用到的库

1,gomonkey代码的地址:

https://github.com/agiledragon/gomonkey

2,从命令行安装gomonkey

liuhongdi@ku:~$ go get -u github.com/agiledragon/gomonkey

3,goconvey库的代码地址

GitHub - smartystreets/goconvey: Go testing in the browser. Integrates with `go test`. Write behavioral tests in Go.

4,从命令行安装

liuhongdi@ku:~$ go get -u github.com/smartystreets/goconvey

说明:刘宏缔的go森林是一个专注golang的博客,
网站:https://blog.imgtouch.com
原文: go语言单元测试之四:go语言用gomonkey为测试函数或方法打桩 – 架构森林

说明:作者:刘宏缔 邮箱: 371125307@qq.com

二,演示项目的相关信息

1,项目地址:

https://github.com/liuhongdi/unittest03

2,功能说明:演示了使用gomonkey库在项目中测试时打桩

3,项目结构:如图:

三,go代码说明

1,model/user.go

package model

type MyUser struct {
	name string
}

//返回用户的name
func (s *MyUser) GetUserName() string {
	return s.name
}

2,main.go

package main

//定义一个加法方法
func Add(a, b int) int {
	return a + b
}

//定义方法,返回一个整数的2倍
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"
)



//测试double,为add函数打桩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函数的桩子
func addstub(a,b int) int {
	return a*3
}

//测试double,为add函数打桩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)
	})
}

//测试给方法打桩1,返回正确
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将返回:hello,world!", t, func() {
		var user *model.MyUser
		user = new(model.MyUser)
		So(user.GetUserName(), ShouldEqual, "hello,world!")
	})
}

//测试给方法打桩2,返回错误
func TestMethodError(t *testing.T) {
	var temp *model.MyUser
	patch := ApplyMethod(reflect.TypeOf(temp), "GetUserName", func(_ *model.MyUser) string {
		return "hello,老刘!"
	})
	defer patch.Reset()
	Convey("GetUserName将返回:hello,world!", t, func() {
		var user *model.MyUser
		user = new(model.MyUser)
		So(user.GetUserName(), ShouldEqual, "hello,world!")
	})
}

四,测试效果

执行测试:

root@ku:/data/go/unittest03# go test -v ./... -gcflags "all=-N -l"

注意:添加参数:-gcflags "all=-N -l"

否则打桩可能不会生效

返回:

五,查看用到的库:

module github.com/liuhongdi/unittest03

go 1.15

require (
	github.com/smartystreets/goconvey v1.6.4
	github.com/agiledragon/gomonkey v2.0.2+incompatible
)

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老刘你真牛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值