go测试:仅运行包含生成标记的测试?

go测试:仅运行包含生成标记的测试?,go,testing,Go,Testing,我有一组长时间运行的测试,用build标记定义。比如说, // file some_test.go //+build func_test (rest of file with test cases) 我还有许多其他短期运行的测试,没有这个构建标志。 是否有一种方法可以轻松地只运行包含构建标记“func_test”的测试 请注意,如果我只是运行go test-tags func\u test,它将运行所有测试,包括some\u test.go中的测试,您需要在-tags之后使用=/code>符

我有一组长时间运行的测试,用build标记定义。比如说,

// file some_test.go
//+build func_test

(rest of file with test cases)
我还有许多其他短期运行的测试,没有这个构建标志。 是否有一种方法可以轻松地只运行包含构建标记“func_test”的测试


请注意,如果我只是运行
go test-tags func\u test
,它将运行所有测试,包括
some\u test.go

中的测试,您需要在
-tags
之后使用
=/code>符号

根据golang文件,进行测试-标记=功能测试

build标记列出了文件应包含在包中的条件。因此,如果只想为build tags func_测试运行测试,那么需要为其他测试提供不同的标记

以下是一个例子: 我的测试目录中有以下两个测试文件

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())

}
其他测试

//+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_测试

$ go test -tags=func_test
testing: TestNormal
PASS
ok      _/D_/Project/ARC/source/prototype/go/src/go-build-test  1.395s

诀窍是使用带有和/或条件的//+构建注释。

您可以按照约定命名测试。 也就是说,让您的长时间运行的测试函数从
TestLong\uu开始
然后和他们一起跑

go测试。/-运行TestLong-tags func_test

不,这不是必需的。显然,添加更多测试不会阻止现有测试的运行。您需要按名称或约束排除其他测试,选择权在您。是的,这是我担心的。这意味着所有编写单元测试的开发人员都需要知道这个名为“test_all”的新标签