testify

Returning a Mock from a package function

走远了吗. 提交于 2021-02-08 11:45:26
问题 I'm fairly new to Go and I'm having some issues with writing tests, specifically mocking the response of a package function. I'm writing an wrapper lib for github.com/go-redis/redis . At the moment it only really has better errors for failures, but it will be expanded with statsd tracking further down the line, but I digress... I have the following go package that I have created package myredis import ( "time" "github.com/go-redis/redis" errors "github.com/pkg/errors" ) var newRedisClient =

【Golang】基于录制,自动生成go test接口自动化用例

偶尔善良 提交于 2021-01-31 05:45:02
背景 之前写过一篇博客,介绍怎么用Python通过解析抓包数据,完成自动化用例的编写。最近这段时间在使用go test,所以就在想能不能也使用代码来生成自动化用例,快速提升测试用例覆盖率。说干就干。 框架 首先介绍一下我们使用的测框架: 项 信息 安装 备注 GO版本 go1.12.9 darwin/amd64 略 测试框架 ginkgo go get -u github.com/onsi/ginkgo/ginkgo 断言库 testify/assert go get github.com/stretchr/testify 官方配套的断言库是gomega ginkgo初始化 初始化: cd path/to/package/you/want/to/test && ginkgo bootstrap 创建示例用例: ginkgo generate (需要手动添加测试用例) 运行测试: go test or ginkgo 注: -v 加上参数可打印运行信息 抓包&运行脚本 使用抓包工具(如Charles)抓包,把数据包导出为har格式,保存在当前目录下 如何安装抓包工具在本文就不赘述了,抓包,过滤出想要的数据,导出,保存的格式注意选择为 har : 根据实际情况修改全局变量信息,如bizBaseFolder、serverName、userFile等 使用 go run gentest.go

2020,你需掌握go 单元测试进阶篇

大憨熊 提交于 2020-08-06 14:00:17
本文说明go语言自带的测试框架未提供或者未方便地提供的测试方案,主要是用于解决写单元测试中比较头痛的依赖问题。也就是伪造模式,经典的伪造模式有桩对象(stub),模拟对象(mock)和伪对象(fake)。比较幸运的是,社区有丰富的第三方测试框架支持支持。下面就对笔者亲身试用并实践到项目中的几个框架做介绍: 1.gomock gomock模拟对象的方式是让用户声明一个接口,然后使用gomock提供的mockgen工具生成mock对象代码。要模拟(mock)被测试代码的依赖对象时候,即可使用mock出来的对象来模拟和记录依赖对象的各种行为:比如最常用的返回值,调用次数等等。文字叙述有点抽象,直接上代码: dick.go中DickFunc依赖外部对象OutterObj,本示例就是说明如何使用gomock框架控制所依赖的对象。 func DickFunc( outterObj MockInterface,para int )(result int ){ fmt.Println( " This init DickFunc " ) fmt.Println( " call outter.func: " ) return outterObj.OutterFunc(para) } mockgen工具命令是: mockgen -source {source_file}.go -destination

JetBrains 发布 2020 年 Go 语言调查报告

给你一囗甜甜゛ 提交于 2020-07-29 05:04:06
有63%的人Go主要用于工作,28%的人用于个人项目 大多数人同时使用多个版本的Go,我估计是要维护不同版本下的项目,下面这个数据印证了这个猜测。 71%的Go开发者主要用来做微服务,排在Scala之后。 GOPATH影响深远啊,74%的人还是只使用一个全局GOPATH,其实最佳实践是分两个,一个用来下载第三方库,另一个用来做项目,但是目前引入新的module之后基本上不用GOPATH了。 版本管理基本上都已经切换到modules,dep和godep紧跟其后,我估计后面这些都是老版本维护工具了 大多数人还是使用Gin为主,echo和beego紧跟其后,44%的人不使用框架。 很多人使用gorilla/mux做路由,但是不可思议的居然有30%的人使用原生的。 大多数人使用内置的testing框架做测试,testify和gomock使用的人也很多,但是还是有20%左右的人不写test,还得继续努力啊 大多数项目看上去都不大,50%左右codebase的文件在100以内。 自从有了modules之后基本上可以脱离GOPATH,而且从数据也可以说明大多数人也是在GOPATH之外新建项目 modules虽好,但是也是存在很多bug或者问题,一半一半的人,一些人愿意,一半人不愿意。 从这个图可以看出来大多数人还是在网站开发,和我们之前调研的结果一样,国内外开发者还是主要在这一块。 来源:

Mock function without receiver

落爺英雄遲暮 提交于 2020-06-28 03:57:31
问题 I have the file util.go : func Foo(service *SomeService) error { return helper(service) } func helper(service *SomeService) error { ... } I'm writing unit tests using testify , starting with Foo . I want to: mock helper assert mocked helper was called 1 time I saw some promising solutions at https://stackoverflow.com/a/19168875/1661745, but not sure about them: Method 1: pass helper as parameter of Foo . My doubt: testify needs a Mock struct to AssertNumberOfCalls, and here there is no struct

Can I run a single test in a suite?

折月煮酒 提交于 2019-12-07 04:20:52
问题 I have setup a test suite for my struct (https://github.com/stretchr/testify#suite-package). Before I was able to run a single test by specifying just a pattern: go test -v ./services/gateways/... -run mytest This approach doesn't work after conversion. Bad luck or is there a way? 回答1: You can run single methods by specifying the -testify.m argument. to run this suite method the command is: go test -v github.com/vektra/mockery/mockery -run ^TestGeneratorSuite$ -testify.m TestGenerator 回答2: i

Can I run a single test in a suite?

六眼飞鱼酱① 提交于 2019-12-05 11:10:49
I have setup a test suite for my struct ( https://github.com/stretchr/testify#suite-package ). Before I was able to run a single test by specifying just a pattern: go test -v ./services/gateways/... -run mytest This approach doesn't work after conversion. Bad luck or is there a way? You can run single methods by specifying the -testify.m argument. to run this suite method the command is: go test -v github.com/vektra/mockery/mockery -run ^TestGeneratorSuite$ -testify.m TestGenerator i think you're SOL with that package but here's a similar approach with go 1.7's stock testing tools: package

Separating unit tests and integration tests in Go

匆匆过客 提交于 2019-11-28 15:10:44
Is there an established best practice for separating unit tests and integration tests in GoLang (testify)? I have a mix of unit tests (which do not rely on any external resources and thus run really fast) and integration tests (which do rely on any external resources and thus run slower). So, I want to be able to control whether or not to include the integration tests when I say go test . The most straight-forward technique would seem to be to define a -integrate flag in main: var runIntegrationTests = flag.Bool("integration", false , "Run the integration tests (in addition to the unit tests)"