我有一個相當大的專案,其中有許多集成測驗散布在不同的包中。我正在使用構建標簽來分離單元、集成和 e2e 測驗。
我需要在運行我的集成和 e2e 測驗之前做一些設定,所以我把一個TestMain函式放在main_test.go根目錄的一個檔案中。這很簡單:
//go:build integration || e2e
// build integration e2e
package test
import (
...
)
func TestMain(m *testing.M) {
if err := setup(); err != nil {
os.Exit(1)
}
exitCode := m.Run()
if err := tearDown(); err != nil {
os.Exit(1)
}
os.Exit(exitCode)
}
func setup() error {
// setup stuff here...
return nil
}
func tearDown() error {
// tear down stuff here...
return nil
}
但是,當我運行測驗時:
$ go test -v --tags=integration ./...
testing: warning: no tests to run
PASS
# all of my subdirectory tests now run and fail...
我真的不想TestMain在每個需要它的包中都寫一個,并希望我可以在根目錄中使用一個。您有什么解決方案可以建議嗎?謝謝。
我能想到的唯一替代方法是設定代碼之外的所有內容,然后運行集成測驗。也許一些shell腳本會設定然后呼叫$ go test?
uj5u.com熱心網友回復:
該go test ./...命令在后臺為每個包編譯一個測驗二進制檔案并一一運行。這也是cannot use -o flag with multiple packages您在嘗試指定輸出時出現錯誤的原因。這是您的主包中的代碼不會影響您的子包的原因。
因此,讓它作業的唯一方法是將所有設定邏輯放在某種“設定”包中,并從所有子包中呼叫共享代碼(我知道還有很多作業要做)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/385980.html
標籤:去
下一篇:如何在執行作業池之間正確延遲
