看了幾篇寫測試的都有介紹到testify,我也來研究一下這怎麼使用好了。
Go語言第三方的測試套件,滿多寫好的內建功能的。
安裝go get -u github.com/stretchr/testify
這邊提供了一些簡單的比較方法,在測試時可以省略一些寫if/else的時間,詳細可以看github。
用昨天的測試案例加上testify使用一次
package count
import (
	"testing"
	"github.com/stretchr/testify/assert"
)
var (
	input = []int{
		-1, 0, 1, 3, 10,
	}
	output = []int{0, 1, 1, 6, 3628800}
)
func TestFactorial(t *testing.T) {
	for i, v := range input {
		result := Factorial(v)
		assert.Equal(t, output[i], result)
	}
}
一樣在終端機輸入go test -v
output:
=== RUN TestFactorial
--- PASS: TestFactorial (0.00s)
PASS
ok test/count 0.236s
testify中的另一個套件,可以創建suite在每一筆測試方法上,並為測試案例加上前置與後置作業,先附上程式碼再來講解。
從建立一個struct開始,裡面包含了suite.Suite
type Suite struct {
	suite.Suite
	testInput  []int
	testOutput []int
}
我這邊將測試用input與output一起丟在裡面。
先寫一個簡單版本的。
package count
import (
	"fmt"
	"testing"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/suite"
)
type Suite struct {
	suite.Suite
	testInput  []int
	testOutput []int
}
func (s *Suite) SetupSuite() {
	fmt.Println("SetupSuite()...")
	s.testInput = []int{-1, 0, 1, 3, 10}
	s.testOutput = []int{0, 1, 1, 6, 3628800}
}
func (s *Suite) TestFactorial() {
	for i, v := range s.testInput {
		result := Factorial(v)
		assert.Equal(s.T(), s.testOutput[i], result)
	}
}
func TestStart(t *testing.T) {
	suite.Run(t, new(Suite))
}
output:
=== RUN TestStart
SetupSuite()...
=== RUN TestStart/TestFactorial
--- PASS: TestStart (0.00s)
--- PASS: TestStart/TestFactorial (0.00s)
PASS
ok test/count 0.173s
目前看起來沒什麼特別的嗎?那看一下Suite提供的完整功能範例
package count
import (
	"fmt"
	"testing"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/suite"
)
type Suite struct {
	suite.Suite
	testInput  []int
	testOutput []int
}
func (s *Suite) SetupSuite() {
	fmt.Println("SetupSuite()...")
	s.testInput = []int{-1, 0, 1, 3, 10}
	s.testOutput = []int{0, 1, 1, 6, 3628800}
}
func (s *Suite) TearDownSuite() {
	fmt.Printf("TearDowmnSuite()...\n")
}
func (s *Suite) SetupTest() {
	fmt.Printf("SetupTest()... \n")
}
func (s *Suite) TearDownTest() {
	fmt.Printf("TearDownTest()... \n")
}
func (s *Suite) BeforeTest(suiteName, testName string) {
	fmt.Printf("BeforeTest=> suiteName: %s, testName: %s\n", suiteName, testName)
}
func (s *Suite) AfterTest(suiteName, testName string) {
	fmt.Printf("AferTest=> suiteName: %s, testName: %s\n", suiteName, testName)
}
// 測試程式
func (s *Suite) TestFactorial() {
	for i, v := range s.testInput {
		result := Factorial(v)
		assert.Equal(s.T(), s.testOutput[i], result)
	}
}
func TestStart(t *testing.T) {
	suite.Run(t, new(Suite))
}
output:
=== RUN TestStart
SetupSuite()...
=== RUN TestStart/TestFactorial
SetupTest()...
BeforeTest=> suiteName: Suite, testName: TestFactorial
AferTest=> suiteName: Suite, testName: TestFactorial
TearDownTest()...
TearDowmnSuite()...
--- PASS: TestStart (0.00s)
--- PASS: TestStart/TestFactorial (0.00s)
PASS
ok test/count 0.174s
稍微講解一下,用html的方式來講解,整個程式的執行順序會是下面這樣
<SetupSuite>
    
    <SetupTest>
        <BeforeTest>
            <TestXxx1>
        <AferTest>
    <TearDownTest>
        
<TearDowmnSuite>
若有多支程式的話就除了Suite的都平行增加
<SetupSuite>
    
    <SetupTest>
        <BeforeTest>
            <TestXxx1>
        <AferTest>
    <TearDownTest>
    
    <SetupTest>
        <BeforeTest>
            <TestXxx2>
        <AferTest>
    <TearDownTest>
        
<TearDowmnSuite>
testify套件差不多就介紹到這,明天稍微統整下這三十天都做了些什麼吧ˊˇˋ
Day 24 | 自己測一下程式好嗎?淺入單元測試(二)
https://ithelp.ithome.com.tw/articles/10240349
golang测试框架testify的使用
https://cloud.tencent.com/developer/article/1869961