用go test跳过一些测试 [英] Skip some tests with go test

查看:453
本文介绍了用go test跳过一些测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以跳过/排除一些测试运行时使用 go test



我有一个相当大量的集成类型测试调用标准的休息服务进行测试,并使用 go test 运行。开发新功能时,有时可以跳过某些测试,例如,如果尚未在测试服务器上部署新功能,并且仍想运行所有现有测试(除了那些测试的新功能除外)新功能)。



我知道 -run ,但我不想指定所有我想要的测试运行,这将是一个很长的名单。同时,我还没有能够编写排除测试的正则表达式。另外一种选择是不提交不在同一分支中运行的测试,但如果我可以指定要排除的内容,它会更容易。 VonC 表示,您可以使用 + build 标签

┌─oneofone @ Oa [/ t / tst-tag] 
└──➜ls
_test.go b_test.go c_test.go

a_test.go: $ b

 包标签

导入测试

func TestA(t * testing.T){}

b_test.go:

 < code $ // $ build!feature1 

包标签

导入测试

func TestB(t * testing.T) {}

c_test.go: $ b

  // + build!feature1 
// + build!feature2

包裹标签

导入测试

func TestC(t * testing.T){}

然后用 -tags 参数:

 ┌─oneofone @ Oa [/ t / tst -tag] 
└──➜去测试-v。 | grep通过:
---通过:TestA(0.00秒)
---通过:TestB(0.00秒)
---通过:TestC(0.00秒)
┌ ─oneofone @ Oa [/ t / tst-tag]
└──➜去测试-v -tags feature1。 | grep PASS:
--- PASS:TestA(0.00秒)
┌─oneofone @ Oa [/ t / tst-tag]
└──➜去测试-v -tags feature2。 | grep PASS:
--- PASS:TestA(0.00秒)
--- PASS:TestB(0.00秒)

//更新:不同的逻辑:

a_test.go:

  // +构建所有

包标签

导入测试

func TestA(t * testing.T){}

b_test.go:

  // +构建所有功能1 

包裹标签

导入测试

func TestB(t * testing.T){}

c_test.go:

  // +构建所有功能2 

包标签

进口测试

func TestC(t * testing.T){}


┌─oneofone @Oa [/ t / tst-tag]
└──➜去测试-v -tags all | grep通过:
---通过:TestA(0.00秒)
---通过:TestB(0.00秒)
---通过:TestC(0.00秒)
┌ ─oneofone @ Oa [/ t / tst-tag]
└──➜go test -v -tags feature1 | grep PASS:
--- PASS:TestB(0.00秒)
┌─oneofone @ Oa [/ t / tst-tag]
└──➜go test -v -tags = feature1 feature2| grep PASS:
--- PASS:TestB(0.00秒)
--- PASS:TestC(0.00秒)

或者您可以通过名称来调用特定的测试:
$ b

d_test.go:

 包标签

导入测试

func TestA1(t * testing.T) {}
func TestB1(t * testing.T){}
func TestC1(t * testing.T){}
func TestD1(t * testing.T){}

输出:

 ┌─oneofone @ Oa [/ t / tst-tag] 
└──➜go test -run =(A | B)1-v | grep通过:
---通过:TestA1(0.00秒)
---通过:TestB1(0.00秒)
┌──一个@Oa [/ t / tst-tag]
└──➜go test -run =D1-v | grep PASS:
--- PASS:TestD1(0.00秒)


Is it possible to skip/exclude some tests from being run with go test?

I have a fairly large amount of integration type tests which call a rest service written as standard go tests, and run with go test. When a new feature is developed its sometimes useful to be able to skip some of the tests, for example if the new feature is not yet deployed on the testing server and I still want to run all the existing tests (except those new ones which tests the new feature).

I know about -run, but I dont want to specify all tests I want to run, that would be a long list. At the same time I have not been able to write a regex for excluding tests.

Another option would be to not commit the tests which dont run in the same branch, but it would be easier if I could just specify what to exclude.

解决方案

Like VonC said, you can use +build tags

┌─ oneofone@Oa [/t/tst-tag]                                                                                                      
└──➜ ls
a_test.go  b_test.go  c_test.go

a_test.go :

package tags

import "testing"

func TestA(t *testing.T) {}

b_test.go :

// +build !feature1

package tags

import "testing"

func TestB(t *testing.T) {}

c_test.go :

// +build !feature1
// +build !feature2

package tags

import "testing"

func TestC(t *testing.T) {}

Then run the test with the -tags parameter :

┌─ oneofone@Oa [/t/tst-tag]                                                                                                      
└──➜ go test -v . | grep PASS:
--- PASS: TestA (0.00 seconds)
--- PASS: TestB (0.00 seconds)
--- PASS: TestC (0.00 seconds)
┌─ oneofone@Oa [/t/tst-tag]                                                                                                      
└──➜ go test -v -tags feature1 . | grep PASS:
--- PASS: TestA (0.00 seconds)
┌─ oneofone@Oa [/t/tst-tag]                                                                                                      
└──➜ go test -v -tags feature2 . | grep PASS:
--- PASS: TestA (0.00 seconds)
--- PASS: TestB (0.00 seconds)

// Update : different logic :

a_test.go:

// +build all

package tags

import "testing"

func TestA(t *testing.T) {}

b_test.go:

// +build all feature1

package tags

import "testing"

func TestB(t *testing.T) {}

c_test.go:

// +build all feature2

package tags

import "testing"

func TestC(t *testing.T) {}


┌─ oneofone@Oa [/t/tst-tag]                                                                                                      
└──➜ go test -v -tags all | grep PASS:
--- PASS: TestA (0.00 seconds)
--- PASS: TestB (0.00 seconds)
--- PASS: TestC (0.00 seconds)
┌─ oneofone@Oa [/t/tst-tag]                                                                                                      
└──➜ go test -v -tags feature1 | grep PASS:
--- PASS: TestB (0.00 seconds)
┌─ oneofone@Oa [/t/tst-tag]                                                                                                      
└──➜ go test -v -tags="feature1 feature2" | grep PASS:
--- PASS: TestB (0.00 seconds)
--- PASS: TestC (0.00 seconds)

Or you call specific tests by name like :

d_test.go:

package tags

import "testing"

func TestA1(t *testing.T) {}
func TestB1(t *testing.T) {}
func TestC1(t *testing.T) {}
func TestD1(t *testing.T) {}

Output:

┌─ oneofone@Oa [/t/tst-tag]                                                                                                      
└──➜ go test -run="(A|B)1" -v | grep PASS:
--- PASS: TestA1 (0.00 seconds)
--- PASS: TestB1 (0.00 seconds)
┌─ oneofone@Oa [/t/tst-tag]                                                                                                      
└──➜ go test -run="D1" -v | grep PASS:
--- PASS: TestD1 (0.00 seconds)

这篇关于用go test跳过一些测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆