2 回答

TA貢獻1834條經驗 獲得超8個贊
根據 golang 文檔https://golang.org/pkg/go/build/
構建標簽列出了文件應該包含在包中的條件。因此,如果您只想為構建標簽 func_test 運行測試,那么您需要為其他測試提供不同的標簽。
這是一個示例:我的測試目錄中有以下 2 個測試文件。
func_test.go
//+build test_all func_test
package go_build_test
import (
"fmt"
"testing"
)
func TestNormal(t *testing.T) {
fmt.Println("testing:", t.Name())
}
other_test.go
//+build test_all,!func_test
package go_build_test
import "testing"
import "fmt"
func TestOtherCase(t *testing.T) {
fmt.Println("testing:", t.Name())
}
現在如果你想運行所有的測試。
$ go test -tags=test_all
testing: TestNormal
testing: TestOtherCase
PASS
ok _/D_/Project/ARC/source/prototype/go/src/go-build-test 0.186s
只運行 func_test
$ go test -tags=func_test
testing: TestNormal
PASS
ok _/D_/Project/ARC/source/prototype/go/src/go-build-test 1.395s
訣竅是使用帶有 AND/OR 條件的 //+build 注釋。

TA貢獻1801條經驗 獲得超8個贊
您可以按照慣例命名您的測試。即讓您的長時間運行的測試功能從開始TestLong_
然后運行它們 go test ./... -run TestLong -tags func_test
- 2 回答
- 0 關注
- 140 瀏覽
添加回答
舉報