# 試用 go test suite
(金慶的專欄 2020.3)
github.com/stretchr/testify/suite 提供了測試套件功能,
可以在整個套件開始結束時執行動作,也可以在每個測試開始結束時執行動作。
假設有以下2個函數需要測試:
```
func foo() {
fmt.Printf("foo...\n")
}
func goo() {
fmt.Printf("goo...\n")
}
```
建立如下測試文件:
```
import (
"fmt"
"testing"
"github.com/stretchr/testify/suite"
)
type _Suite struct {
suite.Suite
}
func (s *_Suite) AfterTest(suiteName, testName string) {
fmt.Printf("AfterTest: suiteName=%s, testName=%s\n", suiteName, testName)
}
func (s *_Suite) BeforeTest(suiteName, testName string) {
fmt.Printf("BeforeTest: suiteName=%s, testName=%s\n", suiteName, testName)
}
func (s *_Suite) SetupSuite() {
fmt.Printf("SetupSuite()...\n")
}
func (s *_Suite) TearDownSuite() {
fmt.Printf("TearDownSuite()...\n")
}
func (s *_Suite) SetupTest() {
fmt.Printf("SetupTest()...\n")
}
func (s *_Suite) TearDownTest() {
fmt.Printf("TearDownTest()...\n")
}
func (s *_Suite) TestFoo() {
foo()
}
func (s *_Suite) TestGoo() {
goo()
}
// 讓 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...
AfterTest: suiteName=_Suite, testName=TestFoo
TearDownTest()...
=== RUN TestGooFoo/TestGoo
SetupTest()...
BeforeTest: suiteName=_Suite, testName=TestGoo
goo...
AfterTest: suiteName=_Suite, testName=TestGoo
TearDownTest()...
TearDownSuite()...
--- PASS: TestGooFoo (0.00s)
--- PASS: TestGooFoo/TestFoo (0.00s)
--- PASS: TestGooFoo/TestGoo (0.00s)
PASS
```
SetupSuite()/TearDownSuite() 僅執行一次,
而 SetupTest()/TearDownTest()/BeforeTest()/AfterTest()對套件中的每個測試執行一次。
缺省情況下,Suite 使用 assert.Assertion 執行斷言, 見Suite定義:
```
type Suite struct {
*assert.Assertions
require *require.Assertions
t *testing.T
}
```
可以這樣執行多個斷言,失敗時仍執行其他斷言:
```
func (m *MySuite) TestAdd() {
m.Equal(1, Add(1, 1)) // FAIL
m.Equal(0, Add(1, 1)) // FAIL
}
```
可以重載成使用 require.Assertion,失敗時中斷執行:
```
type MySuite struct {
suite.Suite
*require.Assertions
}
func (m *MySuite) TestAdd() {
m.Equal(1, Add(1, 1)) // FAIL and return
m.Equal(0, Add(1, 1)) // 不執行
}
```
或者任意指定:
```
m.Assert().Equal(1, 2)
m.Require().Equal(1, 2)
```
(金慶的專欄 2020.3)
github.com/stretchr/testify/suite 提供了測試套件功能,
可以在整個套件開始結束時執行動作,也可以在每個測試開始結束時執行動作。
假設有以下2個函數需要測試:
```
func foo() {
fmt.Printf("foo...\n")
}
func goo() {
fmt.Printf("goo...\n")
}
```
建立如下測試文件:
```
import (
"fmt"
"testing"
"github.com/stretchr/testify/suite"
)
type _Suite struct {
suite.Suite
}
func (s *_Suite) AfterTest(suiteName, testName string) {
fmt.Printf("AfterTest: suiteName=%s, testName=%s\n", suiteName, testName)
}
func (s *_Suite) BeforeTest(suiteName, testName string) {
fmt.Printf("BeforeTest: suiteName=%s, testName=%s\n", suiteName, testName)
}
func (s *_Suite) SetupSuite() {
fmt.Printf("SetupSuite()...\n")
}
func (s *_Suite) TearDownSuite() {
fmt.Printf("TearDownSuite()...\n")
}
func (s *_Suite) SetupTest() {
fmt.Printf("SetupTest()...\n")
}
func (s *_Suite) TearDownTest() {
fmt.Printf("TearDownTest()...\n")
}
func (s *_Suite) TestFoo() {
foo()
}
func (s *_Suite) TestGoo() {
goo()
}
// 讓 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...
AfterTest: suiteName=_Suite, testName=TestFoo
TearDownTest()...
=== RUN TestGooFoo/TestGoo
SetupTest()...
BeforeTest: suiteName=_Suite, testName=TestGoo
goo...
AfterTest: suiteName=_Suite, testName=TestGoo
TearDownTest()...
TearDownSuite()...
--- PASS: TestGooFoo (0.00s)
--- PASS: TestGooFoo/TestFoo (0.00s)
--- PASS: TestGooFoo/TestGoo (0.00s)
PASS
```
SetupSuite()/TearDownSuite() 僅執行一次,
而 SetupTest()/TearDownTest()/BeforeTest()/AfterTest()對套件中的每個測試執行一次。
缺省情況下,Suite 使用 assert.Assertion 執行斷言, 見Suite定義:
```
type Suite struct {
*assert.Assertions
require *require.Assertions
t *testing.T
}
```
可以這樣執行多個斷言,失敗時仍執行其他斷言:
```
func (m *MySuite) TestAdd() {
m.Equal(1, Add(1, 1)) // FAIL
m.Equal(0, Add(1, 1)) // FAIL
}
```
可以重載成使用 require.Assertion,失敗時中斷執行:
```
type MySuite struct {
suite.Suite
*require.Assertions
}
func (m *MySuite) TestAdd() {
m.Equal(1, Add(1, 1)) // FAIL and return
m.Equal(0, Add(1, 1)) // 不執行
}
```
或者任意指定:
```
m.Assert().Equal(1, 2)
m.Require().Equal(1, 2)
```


