github.com/stretchr/testify/suite 提供了测试套件功能,
可以在整个套件开始结束时执行动作,也可以在每个测试开始结束时执行动作。
假设有以下2个函数需要测试:
demo.go
func foo() {
fmt.Printf("foo...\n")
}
func goo() {
fmt.Printf("goo...\n")
}
建立如下测试文件:
suit_test.go
package suite
import (
"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.Suite
VariableThatShouldStartAtFive 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.Time
TestStats 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 string
Start, End time.Time
Passed bool
}
TestInformation存储有关每个测试执行的信息.
type TestingSuite
type TestingSuite interface {
T() *testing.T
SetT(*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.Suite
VariableThatShouldStartAtFive 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()
}