Go單元測試利器testify使用示例詳解
testify
在團隊里推行單元測試的時候,有一個反對的意見是:寫單元測試耗時太多。且不論這個意見對錯,單元測試確實不應該太費時間。這時候,一個好的單測輔助工具,顯得格外重要。本文推薦的 testify(github.com/stretchr/te…) 包,具有斷言、mock 等功能,能配合標準庫,使你的單元測試更加簡潔易讀。
testify 有三個主要功能:
- 斷言,在 assert 包和 require 包。
- Mocking,在 mock 包下。
- 測試組件,在 suite 包下。
assert 包
assert 包提供了一系列很方便的斷言方法,簡化你的測試代碼。如
package yours import ( "testing" "github.com/stretchr/testify/assert" ) func TestSomething(t *testing.T) { // 斷言相等 assert.Equal(t, 123, 123, "they should be equal") // 斷言不等 assert.NotEqual(t, 123, 456, "they should not be equal") // 斷言為 nil assert.Nil(t, object) }
assert 包的函數(shù)的第一個參數(shù)為 testing.T
,用于執(zhí)行 go test
時輸出信息。
如果你有很多個斷言,可以調(diào)用New
方法實例化 Assertions
結(jié)構(gòu)體,然后就可以省略testing.T
參數(shù)了。上面的代碼,可以簡化成
func TestSomething(t *testing.T) { // 實例化 assertion 結(jié)構(gòu)體,下面的斷言都不用傳入 t 作為第一個參數(shù)了。 assert := assert.New(t) // 斷言相等 assert.Equal(123, 123, "they should be equal") // 斷言不等 assert.NotEqual(123, 456, "they should not be equal") // 斷言為 nil assert.Nil(object) }
assert 失敗的話,底層調(diào)用 t.Errorf
來輸出錯誤信息。也就是說,斷言失敗并不會中停止測試。
assert 包的斷言函數(shù),返回值是 bool 類型,表示斷言的成功或失敗。 我們可以根據(jù)返回值,進一步做斷言。如
// 當 object 不為 nil 的時候,進一步斷言 object.Value 的值 if assert.NotNil(t, object) { assert.Equal(t, "Something", object.Value) }
assert 包提供的斷言類型非常多,包括對比變量、json、目錄、Http 響應等。完整列表見:pkg.go.dev/github.com/…
require 包
require 包提供的函數(shù)和 assert 包是一樣的,區(qū)別是:
- require 包如果斷言失敗,底層調(diào)用
t.FailNow
, 會立刻中斷當前的測試,所以也不會有返回值。 - assert 包如果斷言失敗,底層調(diào)用
t.Errorf
,返回 false,不會中斷測試。
mock 包
單元測試一般僅限于測試本服務,對于別的服務的調(diào)用(比如數(shù)據(jù)庫),我們可以創(chuàng)建 Mock 對象來模擬對其他服務的調(diào)用。
和別的語言一樣,Mock 對象我們可以通過工具自動生成。mockery
(github.com/vektra/mock…) 工具可以根據(jù) golang 的 interface,生成類似下面的 Mock 對象。
package yours import ( "testing" "github.com/stretchr/testify/mock" ) // Mock 對象,會依賴 mock.Mock type MyMockedObject struct{ mock.Mock } // 要 mock 的接口的方法 func (m *MyMockedObject) DoSomething(number int) (bool, error) { args := m.Called(number) return args.Bool(0), args.Error(1) }
我們可以調(diào)用 Mock 對象的 On
方法,設置對應方法的參數(shù)和返回值。如
// 測試 func TestSomething(t *testing.T) { // 實例化 Mock 對象 testObj := new(MyMockedObject) // 設置預期,當調(diào)用 testObj.DoSomething(123)時,返回 true, nil testObj.On("DoSomething", 123).Return(true, nil) // 我們要測試的函數(shù) targetFuncThatDoesSomethingWithObj(testObj) // 斷言符合預期,即 testObj.DoSomething(123) 會被調(diào)用 testObj.AssertExpectations(t) }
用On
方法設置預期(expectations)時,傳入的參數(shù)可以使用 mock.Anything
,表示任何值都行。如
// 設置預期,當調(diào)用 DoSomething 時,返回 ture,nil testObj.On("DoSomething", mock.Anything).Return(true, nil)
如果有多個 mock 對象需要 AssertExpectations,如
testObj1.AssertExpectations(t) testObj2.AssertExpectations(t) testObj3.AssertExpectations(t)
可以使用 mock.AssertExpectations
優(yōu)化
mock.AssertExpectations(t, testObj1, testObj2, testObj3)
mock.Mock 還有一些很實用的常用斷言,如:
- AssertCalled 斷言被調(diào)用
- AssertNotCalled 斷言沒被調(diào)用
- AssertExpectations 斷言 On 和 Return 設置的參數(shù)和返回值的方法,有被調(diào)用
- AssertNumberOfCalls 斷言調(diào)用次數(shù)
完整的列表可以查看: pkg.go.dev/github.com/…
suite 包
如果你有別的面向?qū)ο笳Z言的經(jīng)驗,用 suite 包寫單元測試可能更符合你的習慣。我們可以自定義一個結(jié)構(gòu)體,它依賴 suite.Suite
,它所有的以 Test
開頭的函數(shù),都是一個測試。
import ( "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" ) // 依賴 suite.Suite type ExampleTestSuite struct { suite.Suite VariableThatShouldStartAtFive int } // 每個測試運行前,會執(zhí)行 func (suite *ExampleTestSuite) SetupTest() { suite.VariableThatShouldStartAtFive = 5 } // 所有以“Test”開頭的方法,都是一個測試 func (suite *ExampleTestSuite) TestExample() { assert.Equal(suite.T(), 5, suite.VariableThatShouldStartAtFive) } // 用于 'go test' 的入口 func TestExampleTestSuite(t *testing.T) { suite.Run(t, new(ExampleTestSuite)) }
除了 SetupTest
,suite.Suite 還有一些鉤子:
TearDownTest
每個測試之后執(zhí)行
SetupSuite
Suite 開始之前執(zhí)行一次,在所有測試之前執(zhí)行
TearDownSuite
Suite 結(jié)束之后執(zhí)行一次,在所有測試之后執(zhí)行
更多的介紹可以查看官網(wǎng):pkg.go.dev/github.com/…
引用
以上就是Go單元測試利器testify使用示例詳解的詳細內(nèi)容,更多關(guān)于Go 單元測試testify的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
golang判斷net.Conn 是否已關(guān)閉的操作
這篇文章主要介紹了golang判斷net.Conn 是否已關(guān)閉的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12gtoken替換jwt實現(xiàn)sso登錄的問題小結(jié)
這篇文章主要介紹了gtoken替換jwt實現(xiàn)sso登錄,主要介紹了替換jwt的原因分析及gtoken的優(yōu)勢,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05淺析go中的map數(shù)據(jù)結(jié)構(gòu)字典
golang中的map是一種數(shù)據(jù)類型,將鍵與值綁定到一起,底層是用哈希表實現(xiàn)的,可以快速的通過鍵找到對應的值。這篇文章主要介紹了go中的數(shù)據(jù)結(jié)構(gòu)字典-map,需要的朋友可以參考下2019-11-11go內(nèi)存緩存BigCache封裝Entry源碼解讀
這篇文章主要為大家介紹了go內(nèi)存緩存BigCache封裝Entry源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09