简介 testify可以说是最流行的(从 GitHub star 数来看)Go 语言测试库了。testify提供了很多方便的函数帮助我们做assert和错误信息输出。 testify核心有三部分内容: assert:断言; mock:测试替身; suite:测试套件。 1. assert assert 子库提供了便捷的断言函数,可以大大简化测试代码的编写。 中的函数会自动生成比较清晰的错误描述信息: func TestEqual(t *testing.T) { var a = 100 var b = 200 assert.Equal(t, a, b, "") } 2. mock testify 3. suite testify提供了测试套件的功能(TestSuite),testify测试套件只是一个结构体,内嵌一个匿名的suite.Suite结构。 看一个例子: 例子1:使用assert package main import ( "testing" "github.com/stretchr/testify/assert" ) func
简介 testify可以说是最流行的(从 GitHub star 数来看)Go 语言测试库了。testify提供了很多方便的函数帮助我们做assert和错误信息输出。 testify核心有三部分内容: assert:断言; mock:测试替身; suite:测试套件。 准备工作 本文代码使用 Go Modules。 创建目录并初始化: $ mkdir -p testify && cd testify $ go mod init github.com/darjun/go-daily-lib/testify 安装testify 提供了测试套件的功能(TestSuite),testify测试套件只是一个结构体,内嵌一个匿名的suite.Suite结构。 参考 testify GitHub:github.com/stretchr/testify Go 每日一库 GitHub:https://github.com/darjun/go-daily-lib
testify是用go实现的一个assert风格的测试框架,这个包提供了我们需要的断言的功能,提供了非常丰富的断言方法,使用起来非常简单且易于理解。2. 如何使用testify进行断言? 安装go get github.com/stretchr/testify2.Quick startpackage algoimport ("github.com/stretchr/testify/assert 3. suite套件包github.com/stretchr/testify/suite 提供了测试套件功能,可以在整个套件开始结束时执行动作,也可以在每个测试开始结束时执行动作。 用法如下:package algoimport ("fmt""github.com/stretchr/testify/assert""github.com/stretchr/testify/suite" 5llx7c2j0r90sp2hhlptlc1m0000gn/T/____Suite_in_testcase_demo_cmd_algo -test.v -test.run ^\QTestAll\E$ -testify.m
一、测试框架 stretchr/testify 1.stretchr/testify/assert assert库是这样的一个库,它有一系列函数来适应各种各样不同的场景需求,下面是一个简单的判断值是否符合预期的 Subset/NotSubset 判断subset是/不是list的子集 FileExists 判断path所示的文件是否存在 DirExists 判断path所示的文件夹是否存在 2.stretchr/testify ShouldEqual, true) So(IsOdd(1), ShouldEqual, false) }) }) } 小结 本文总结了GoConvey,testify 笔者总结的是,无论什么时候都可以用Convey+So的组合优雅地实现测试用例嵌套和断言,而testify适合最基本的测试(少许测试用例)。
为了解决这个问题,我们可以借助第三方库,例如 testify,来简化这些比较操作。 testify 工具库 testify 是在 Go 语言中被广泛使用的第三方测试库,它提供了一些便捷的断言方法、测试套件支持和 mock 功能,极大地简化了测试代码的编写。 我们可以通过以下命令安装 testify 模块: go get github.com/stretchr/testify 接下来我们就可以将前面展示的部分代码: if got ! testify 常用的断言方法 testify/assert 提供了丰富的断言函数,便于我们进行复杂的比较操作。 assert.Len(t, []int{1, 2, 3}, 3) 更多的函数信息,请参考 testify/assert。
在 GoLand 2020.3 中,您可以探索 goroutines dumps,运行并导航到单个表测试(table tests),并从对 Testify 测试框架的扩展支持中获得更多信息。 扩展了对 Testify 的支持 ? 现在,IDE 可以识别启动测试套件的测试功能,并提供一种启动单个 suite.Run 和 suite.T().Run,只要子测试名称是字符串常量即可。 此外,GoLand 现在可以在一个测试用例中使用相同的方法名称分别运行 Testify 套件。 这适用于 testify/assert,testify/require,testify/mock 和 testify/suite 包。 03 代码检查 ?
这里推荐使用 Testify + Gomonkey 开源库来完成 Go 的单元测试的书写。 Testify 主要提供测试套件和断言的能力,不过也提供了 mock 的功能,但我们不使用,因为有更好用的 mock 库。 Gomonkey 主要用于提供 mock 能力。 下面使用 Testify + Gomonkey 给出使用示例。 先改造一下 Hello() 和 Add() 函数。 推荐使用 testify + gomonkey 测试框架编写 Go 的单测,关于其他的单测框架,比如 goconvey + gomock,感兴趣的你可自行了解。 [4] Testify - Thou Shalt Write Tests
testify/assert testify是一个社区非常流行的Go单元测试工具包,其中使用最多的功能就是它提供的断言工具——testify/assert或testify/require。 安装 go get github.com/stretchr/testify 使用示例 我们在写单元测试的时候,通常需要使用断言来校验测试结果,但是由于Go语言官方没有提供断言,所以我们会写出很多的if. 而testify/assert为我们提供了很多常用的断言函数,并且能够输出友好、易于阅读的错误描述信息。 testify/require拥有testify/assert所有断言函数,它们的唯一区别就是——testify/require遇到失败的用例会立即终止本次测试。 此外,testify包还提供了mock、http等其他测试工具,篇幅所限这里就不详细介绍了,有兴趣的同学可以自己了解一下。
viper.GetString("server.ip"))fmt.Println("Server Port:", viper.GetInt("server.port"))}28. github.com/stretchr/testify /assert - 测试工具库Testify 已经在之前的回答中提到过,这里再强调其在单元测试中的断言功能。 goCopy codepackage mainimport ("testing""github.com/stretchr/testify/assert")func add(x, y int) int {
test would look like this: 1package mail 2 3import ( 4 "testing" 5 6 "github.com/stretchr/testify replaced in the test: 1package mail 2 3import ( 4 "net" 5 "testing" 6 7 "github.com/stretchr/testify all its methods. 1package mail 2 3import ( 4 "net" 5 "testing" 6 7 "github.com/stretchr/testify You may have noticed I’ve used Testify, a great testing package. I’ve written basic mocks, but Testify helps you generate advanced and fluent mocks on which you can test
前面我们介绍了golang测试框架里面的testify, 下面让了解一下另一个用的也比较多的断言框架goconvey。一。 设置界面主题查看完整的测试结果使用浏览器提醒自动检测代码变动并编译测试半自动化书写测试用例:http://localhost:8080/composer.html查看测试覆盖率:http://localhost:8080/reports/临时屏蔽某个包的编译测试相比较于testify
6️⃣Testify:让测试不再“将就”testify/mock:自动生成mock实现,测试service层超轻松testify/suite:共享setup/teardown,组织复杂测试用例配合Fx的接口依赖
常用的单测框架 2.1 testing 2.1.1 单元测试 2.1.2 测试覆盖率 2.1.3 子测试t.run 2.2 goconvey 2.2.1 基本使用 2.2.2 图形化使用 2.3 testify bin加入PATH里面 或者写全路径 到测试的目录下,执行goconvey,启动http 8000,自动运行测试用例 浏览器访问 http://127.0.0.1:8000 最终效果如下 2.3 testify int) { result = x + 2 return result } 测试用例cal_test.go package pkg05 import ( "github.com/stretchr/testify TestAdd (0.00s) PASS ok pkg05 1.216s 2.3.2 表驱动测试 package pkg05 import ( "github.com/stretchr/testify }, } for _, test := range tests { ass.Equal(Add(test.input), test.expected) } } 2.3.3 mock功能 使用testify
"net""testing""go-tutorial/project/gokit_learn/sample_grpc_srv/pb" // 生成的 pb 文件"github.com/stretchr/testify insecure" // 明文传输,不启用加密"google.golang.org/grpc/test/bufconn" // bufconn 实现内存中的网络连接)说明:在此示例中,我们使用了 testify 从而避免实际网络端口冲突;自定义拨号器使得客户端能够轻松连接内存中的 gRPC Server;利用 bufconn 进行测试,仍然需要完整的 GRPC 服务实现,只是通信方式不同;使用断言库(例如 testify
YourFunction(%d) = %d; want %d", tt.input, result, tt.expected) } }) }}编写清晰的断言使用断言库(如testify /assert、testify/require)可以使你的测试代码更加简洁和易于理解。 import ( "testing" "github.com/stretchr/testify/assert")func TestYourFunctionWithAssertions(t *
cover -coverprofile=res.out go tool cover -html=res.out 将生成的 html 文件,可以使用浏览器打开,即可以看到具体的单测结果 使用断言工具 testify go get github.com/stretchr/testify 我们可以在测试函数中加上关于断言的语句就很 nice 了,无需自己去写反射对应的值,然后再进行判断 使用 assert 包,我们直接执行 assert 对应的函数即可完成断言,根据不同的断言需求,有不同的函数例如 例如我们使用 Equal 函数,就可以这样使用 import "github.com/stretchr/testify/assert myAssert := assert.New(t) myAssert.Equal("期望的值", "实际的值", "如果期望的和实际的相等就ok,不符合就报错误信息") } 关于 golang testify assert 可以查看官网:assert package - github.com/stretchr/testify/assert - Go Packages ,这里有更详细的用法,本文是为了帮助查询和索引
stretchr/testifyhttps://github.com/stretchr/testify Stars: 22.6k License: MIT testify 是一个与标准库兼容的常见断言和模拟工具包
package dbclient import ( "github.com/stretchr/testify/mock" "github.com/callistaenterprise accountservice/model" ) // MockBoltClient 是一个用于测试的数据存储客户端的模拟实现. // 我们仅仅放置一个通用模拟对象,而不是bolt.DB 指针 // strechr/testify QueryAccount函数体看起来可能有些奇怪,但它只是简单地说明“strechr/testify”如何为我们提供一个可编程模拟,并且我们可以完全控制其内部机制。 在这一部分,我们编写了我们的第一个部分——单元测试,使用第三方GoConvey 和 “stretchr/testify/mock”帮助我们。我们将在本博客系列 的后面部分进行更多测试。
gin@v1.4.0 github.com/modern-go/reflect2@v1.0.1 github.com/gin-gonic/gin@v1.4.0 github.com/stretchr/testify go-playground/validator.v8@v8.18.2 github.com/gin-gonic/gin@v1.4.0 gopkg.in/yaml.v2@v2.2.2 github.com/stretchr/testify @v1.3.0 github.com/davecgh/go-spew@v1.1.0 github.com/stretchr/testify@v1.3.0 github.com/pmezard/go-difflib @v1.0.0 github.com/stretchr/testify@v1.3.0 github.com/stretchr/objx@v0.1.0 github.com/mattn/go-isatty
5、Testify:轻量级,语法简洁 Testify是一款轻量级测试框架,语法简洁,支持测试类和测试函数的灵活组织。它适合中小型项目,尤其适合对unittest和pytest语法不熟悉的团队。