github.com/stretchr/testify/suite
Go 单元测试
https://blog.csdn.net/weixin_39172380/article/details/88666479?utm_medium=distribute.pc_relevant.none-task-blog-baidulandingword-2&spm=1001.2101.3001.4242
github.com/stretchr/testify/suite 提供了测试套件功能,
可以在整个套件开始结束时执行动作,也可以在每个测试开始结束时执行动作。
假设有以下2个函数需要测试:
demo.go
func foo() {fmt.Printf("foo...\n")
}func goo() {fmt.Printf("goo...\n")
}
建立如下测试文件:
suit_test.go
package suiteimport ("fmt""github.com/stretchr/testify/suite""testing"
)type _Suite struct {suite.Suite
}// SetupSuite() 和 TearDownSuite() 仅执行一次
// SetupTest() TearDownTest() BeforeTest() AfterTest() 对套件中的每个测试执行一次func (s *_Suite) AfterTest(suiteName,testName string) {fmt.Printf("5.10.AferTest: suiteName=%s,testName=%s\n",suiteName,testName)
}func (s *_Suite) BeforeTest(suiteName,testName string) {fmt.Printf("3.8.BeforeTest: suiteName=%s,testName=%s\n",suiteName,testName)
}// SetupSuite() 仅执行一次
func (s *_Suite) SetupSuite() {fmt.Printf("1.SetupSuite() ...\n")
}// TearDownSuite() 仅执行一次
func (s *_Suite) TearDownSuite() {fmt.Printf("12.TearDowmnSuite()...\n")
}func (s *_Suite) SetupTest() {fmt.Printf("2.7.SetupTest()... \n")
}func (s *_Suite) TearDownTest() {fmt.Printf("6.11.TearDownTest()... \n")
}func (s *_Suite) TestFoo() {foo() // 4.
}func (s *_Suite) TestGoo() {goo() //9.
}// 让 go test 执行测试
func TestGooFoo(t *testing.T) {suite.Run(t,new(_Suite))
}
输出如下:
=== RUN TestGooFoo
SetupSuite() ...
=== RUN TestGooFoo/TestFoo
SetupTest()...
BeforeTest: suiteName=_Suite,testName=TestFoo
foo ...
AferTest: suiteName=_Suite,testName=TestFoo
TearDownTest()...
=== RUN TestGooFoo/TestGoo
SetupTest()...
BeforeTest: suiteName=_Suite,testName=TestGoo
goo ...
AferTest: suiteName=_Suite,testName=TestGoo
TearDownTest()...
TearDowmnSuite()...
--- PASS: TestGooFoo (0.00s)--- PASS: TestGooFoo/TestFoo (0.00s)--- PASS: TestGooFoo/TestGoo (0.00s)
PASS
SetupSuite()/TearDownSuite() 仅执行一次,
而 SetupTest()/TearDownTest()/BeforeTest()/AfterTest()对套件中的每个测试执行一次。
import "github.com/stretchr/testify/suite"
软件包套件包含用于创建测试套件结构并将这些结构上的方法作为测试运行的逻辑. 该软件包最有用的部分是,您可以在测试套件上创建安装/拆卸方法,该方法将在整个套件或单个测试之前/之后运行(取决于您实现的接口).
通常,通过首先从suite.Suite中扩展内置套件功能来构建测试套件. 另外,您也可以根据需要自行重现该逻辑(您只需要从suite / interfaces.go中实现TestingSuite接口即可).
之后,您可以在suite / interfaces.go中实现任何接口,以向您的套件中添加设置/拆卸功能,并添加任何以" Test"开头的方法来添加测试. 不匹配任何套件接口且不以" Test"开头的方法将不会由testify运行,并且可以安全地用作辅助方法.
一旦构建了测试套件,就需要在匹配"正在测试"的身份(例如func(* testing.T))的任何函数中运行套件(使用suite.从testify运行).
选择测试套件的正则表达式指定了命令行参数" -run". 选择要测试套件方法的正则表达式指定命令行参数" -m". 套件对象具有断言方法.
一个简单的例子:
// Basic imports
import ("testing""github.com/stretchr/testify/assert""github.com/stretchr/testify/suite"
)// Define the suite, and absorb the built-in basic suite
// functionality from testify - including a T() method which
// returns the current testing context
type ExampleTestSuite struct {suite.SuiteVariableThatShouldStartAtFive int
}// Make sure that VariableThatShouldStartAtFive is set to five
// before each test
func (suite *ExampleTestSuite) SetupTest() {suite.VariableThatShouldStartAtFive = 5
}// All methods that begin with "Test" are run as tests within a
// suite.
func (suite *ExampleTestSuite) TestExample() {assert.Equal(suite.T(), 5, suite.VariableThatShouldStartAtFive)suite.Equal(5, suite.VariableThatShouldStartAtFive)
}// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestExampleTestSuite(t *testing.T) {suite.Run(t, new(ExampleTestSuite))
}
Index
func Run(t *testing.T, suite TestingSuite)
type AfterTest
type BeforeTest
type SetupAllSuite
type SetupTestSuite
type Suite
func (suite *Suite) Assert() *assert.Assertions
func (suite *Suite) Require() *require.Assertions
func (suite *Suite) Run(name string, subtest func()) bool
func (suite *Suite) SetT(t *testing.T)
func (suite *Suite) T() *testing.T
type SuiteInformation
func (s SuiteInformation) Passed() bool
type TearDownAllSuite
type TearDownTestSuite
type TestInformation
type TestingSuite
type WithStats
Package Files
doc.go interfaces.go stats.go suite.go
func Run
func Run(t *testing.T, suite TestingSuite)
运行需要一个测试套件,并运行附加到它的所有测试.
type AfterTest
type AfterTest interface {AfterTest(suiteName, testName string)
}
AfterTest具有在测试完成后立即执行的功能,并接收套件和测试名称作为输入
type BeforeTest
type BeforeTest interface {BeforeTest(suiteName, testName string)
}
在测试开始之前,BeforeTest具有要执行的功能,并接收套件和测试名称作为输入
type SetupAllSuite
type SetupAllSuite interface {SetupSuite()
}
SetupAllSuite具有SetupSuite方法,该方法将在运行套件中的测试之前运行.
type SetupTestSuite
type SetupTestSuite interface {SetupTest()
}
SetupTestSuite具有SetupTest方法,该方法将在套件中的每个测试之前运行.
type Suite
type Suite struct {*assert.Assertions// contains filtered or unexported fields
}
套件是一个基本的测试套件,其中包含用于存储和检索当前* testing.T上下文的方法.
*func (Suite) Assert
func (suite *Suite) Assert() *assert.Assertions
断言返回套件的断言上下文. 通常,您可以调用suite.NoError(预期的,实际的),但是对于嵌入式方法被覆盖的情况(例如,您可能想要覆盖assert.Assertions和require.Assertions),提供了此方法,以便您可以呼叫suite.Assert().NoError()
.
*func (Suite) Require
func (suite *Suite) Require() *require.Assertions
Require返回套件的require上下文.
*func (Suite) Run
func (suite *Suite) Run(name string, subtest func()) bool
Run提供了围绕golang子测试的套件功能. 应该在测试套件代码中代替t.Run(name,func(t * testing.T))来调用它. 传入的func将作为带有t的新实例的子测试执行. 提供与go test pkg -run TestSuite / TestName / SubTestName的兼容性.
*func (Suite) SetT
func (suite *Suite) SetT(t *testing.T)
SetT设置当前的* testing.T上下文.
*func (Suite) T
func (suite *Suite) T() *testing.T
T检索当前的* testing.T上下文.
type SuiteInformation
type SuiteInformation struct {Start, End time.TimeTestStats map[string]*TestInformation
}
SuiteInformation统计信息存储整个套件执行的统计信息.
func (SuiteInformation) Passed
func (s SuiteInformation) Passed() bool
type TearDownAllSuite
type TearDownAllSuite interface {TearDownSuite()
}
TearDownAllSuite具有TearDownSuite方法,该方法将在运行套件中的所有测试之后运行.
type TearDownTestSuite
type TearDownTestSuite interface {TearDownTest()
}
TearDownTestSuite具有TearDownTest方法,该方法将在套件中的每个测试之后运行.
type TestInformation
type TestInformation struct {TestName stringStart, End time.TimePassed bool
}
TestInformation存储有关每个测试执行的信息.
type TestingSuite
type TestingSuite interface {T() *testing.TSetT(*testing.T)
}
TestingSuite可以存储并返回由" go test"生成的当前* testing.T上下文.
type WithStats
type WithStats interface {HandleStats(suiteName string, stats *SuiteInformation)
}
WithStats实现HandleStats,该功能将在测试套件完成时执行. 这些统计信息包含有关该套件及其测试执行的信息.
https://s0godoc0org.icopy.site/github.com/stretchr/testify/suite
testify提供了suite包提供了类似rails minitest中可以给每个测试用例进行前置操作和后置操作的功能,这个方便的功能,在前置操作和后置操作中去初始化和清空数据库,就可以帮助我们实现第一个目标。
同时,还可以声明在这个测试用例周期内都有效的全局变量
type ExampleTestSuite struct {suite.SuiteVariableThatShouldStartAtFive int
}// 每个测试用例执行前都会调用
func (suite *ExampleTestSuite) SetupTest() {test_helpers.Init(config.Cfg)
}//其中一个测试用例
func (suite *ExampleTestSuite) TestExample() {assert.Equal(suite.T(), 5, suite.VariableThatShouldStartAtFive)
}// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestExampleTestSuite(t *testing.T) {suite.Run(t, new(ExampleTestSuite))
}// 每个测试用例执行后都会调用
func (suite *ExecutorTestSuite) TearDownTest() {test_helpers.CleanTables()
}
github.com/stretchr/testify/suite相关推荐
- go语言单元测试报错:missing go.sum entry for module providing package github.com/stretchr/testify/assert
报错:missing go.sum entry for module providing package github.com/stretchr/testify/assert (imported by ...
- Go 单元测试从 0 到 1
文章目录 1.什么是单元测试 2.单元测试的作用 3.Go 如何写单元测试 4.go test 命令 4.1 简介 4.2 示例 5.快速生成单测代码 6.看看单元测试覆盖率 7.使用单测框架写单测 ...
- golang及beego框架单元测试小结
golang及beego框架单元测试小结 单元测试是保证开发质量的一个重要手段,提及golang开发,要保证开发质量,则难以回避单元测试的使用.golang开发语言有原生提供单元测试相关代码及工具 ...
- Go:测试库testify简介
文章目录 简介 1. assert 2. mock 3. suite 示例 例子1:使用assert 例子2:使用require 小结 简介 testify可以说是最流行的(从 GitHub star ...
- Go 每日一库之 testify
简介 testify可以说是最流行的(从 GitHub star 数来看)Go 语言测试库了.testify提供了很多方便的函数帮助我们做assert和错误信息输出.使用标准库testing,我们需要 ...
- 如何写好测试用例以及go单元测试工具testify简单介绍
背景 最近在工作和业余开源贡献中,和单元测试接触的比较频繁.但是在这两个场景之下写出来的单元测试貌似不太一样,即便是同一个代码场景,今天写出来的单元测试和昨天写的也不是很一样,我感受到了对于单元测 ...
- Go 写测试必学的三个库:Ginkgo、testify和GoMock
对开发而言,测试的重要性相信对每个开发者而言是老生常谈的事情.虽然我们很有可能在开发过程中由于各种原因会希望后续补全,然而事实上我更建议采用"Tests that fail then pas ...
- 【GO】23.Golang 测试库 testify
下载testify库 go get github.com/stretchr/testify 测试方法 package goconveydemoimport "errors"func ...
- 2、Go自动化测试入门-testify
先获取testify包:go get GitHub.com/stretchr/testify 一个简单的示例 让我们先看看传统上如何在 Go 中编写测试.这将让我们对提高可读性的 testify 有所 ...
最新文章
- 开源组织:Datawhale
- 从起源到未来,一文看懂70年的人工智能简史
- css3 卡片亮光_利用css3实现文字亮光特效的代码
- 01配置管理过程指南
- C语言再学习 -- NUL和NULL的区别
- OpenCV全向相机校准Omnidirectional Camera Calibration
- [js] 说说你对JS中暂性死区的理解,它有什么运用场景?
- boost库学习入门篇
- TCP/UDP的接收缓冲区和发送缓冲区
- 大话设计模式C++版——代理模式
- 简谈java的split
- 40. Combination Sum II
- PuttyPsftp命令行实现自动登录
- low power-upf-vcsnlp(五)
- 小松鼠短视频完开源源码
- 硬盘IDE和AHCI模式的区别
- 警察抓小偷打字游戏JAVA_警察抓小偷打字游戏金山打字通游戏
- 安装爬虫框架Scrapy,安装后运行不了~
- Flutter 中的应用内购买
- 4.6Proteus流水灯学习
热门文章
- java使用阿里云oss sdk
- ACM_置换群 burnside引理 Polya定理
- PTA新生训练赛----3
- vscode私钥设置_VScode链接服务器并配置公钥-SSH Keys
- 有道购物助手脚本版,支持chrome,解决bug【Update 0.03】!
- 【论文阅读报告】 Real-time Personalization using Embeddings for Search Ranking at Airbnb
- VLAN(TRUNK端口 ACCESS 端口)
- Android Studio TV开发教程(十五) Android N及更早版本中的建议
- Solaris IPMP配置
- Kony grabs funding to build mobile business apps