构建约束通常用于将常规单元测试与需要外部资源(例如数据库或网络访问权限)的集成测试分开。为此,将自定义构建约束添加到测试文件的顶部:
// +建立整合
package main
import (
"testing"
)
func TestThatRequiresNetworkAccess(t *testing.T) {
t.Fatal("失败了!")
}除非使用以下调用,否则测试文件将不会编译到构建可执行文件中go test:
go test -tags "integration"
结果:
$ go test ? bitbucket.org/yourname/yourproject [no test files] $ go test -tags "integration" --- FAIL: TestThatRequiresNetworkAccess (0.00s) main_test.go:10: 失败了! FAIL exit status 1 FAIL bitbucket.org/yourname/yourproject 0.003s