单独的集成测试

构建约束通常用于将正常单元测试与需要外部资源(如数据库或网络访问)的集成测试分开。为此,请在测试文件的顶部添加自定义构建约束:

// +build integration
 
package main
 
import (
    "testing"
)
 
func TestThatRequiresNetworkAccess(t *testing.T) {
    t.Fatal("It failed!")
}

除非使用以下 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: It failed!
FAIL
exit status 1
FAIL    bitbucket.org/yourname/yourproject    0.003s