有条件地使用构建标志运行测试不起作用。

huangapple go评论74阅读模式
英文:

conditionally running tests with build flags not working

问题

我正在运行一些 Golang 的测试,并且我想避免运行那些慢的测试,例如下面这个测试使用了 bcrypt,所以很慢:

// +build slow
package services

import (
	"testing"
	"testing/quick"
)

// 使用 bcrypt 需要太长时间,减少迭代次数。
var config = &quick.Config{MaxCount: 20}

func TestSignaturesAreSame(t *testing.T) {
	same := func(simple string) bool {
		result, err := Encrypt(simple)
		success := err == nil && ComparePassWithHash(simple, result)
		return success
	}

	if err := quick.Check(same, config); err != nil {
		t.Error(err)
	}
}

为了避免在每次迭代中运行该测试,我设置了 // +build slow 标记。这个测试应该只在执行 go test -tags slow 时运行,但不幸的是它每次都在运行(使用 -v 标记可以看到它在运行)。

有什么想法是出了什么问题吗?

英文:

I'm running some tests in golang and I want to avoid running the slow ones, for example this one uses bcrypt so it's slow:

// +build slow
package services

import (
	"testing"
	"testing/quick"
)

// using bcrypt takes too much time, reduce the number of iterations.
var config = &quick.Config{MaxCount: 20}

func TestSignaturesAreSame(t *testing.T) {
	same := func(simple string) bool {
		result, err := Encrypt(simple)
		success := err == nil && ComparePassWithHash(simple, result)
		return success
	}

	if err := quick.Check(same, config); err != nil {
		t.Error(err)
	}
}

To avoid running this in every iteration I've set up the // +build slow flag. This should only run when doing go test -tags slow but unfortunately it's running every time (the -v flag shows it's running).

Any idea what's wrong?

答案1

得分: 5

你的 // +build slow 需要在后面加上一个空行。

为了区分构建约束和包文档,一系列的构建约束必须在后面加上一个空行。

访问构建约束

英文:

Your // +build slow needs to be followed by a blank line

To distinguish build constraints from package documentation, a series of build constraints must be followed by a blank line.

visit Build Constraints

huangapple
  • 本文由 发表于 2016年3月11日 02:42:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/35924680.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定