只运行单个测试而不是整个套件吗?

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

Just run single test instead of the whole suite?

问题

我有一个用于实现一打测试的Go包的测试套件。有时,测试套件中的一个测试失败了,我想单独重新运行该测试以节省调试时间。这种操作是否可行,还是每次都需要编写一个单独的文件?

英文:

I have a test suite for a Go package that implements a dozen tests. Sometimes, one of the tests in the suite fails and I'd like to re-run that test individually to save time in debugging process. Is this possible or do I have to write a separate file for this every time?

答案1

得分: 112

使用go test -run标志来运行特定的测试。该标志在测试标志部分go工具文档中有详细说明:

-run regexp
    仅运行与正则表达式匹配的测试和示例。
英文:

Use the go test -run flag to run a specific test. The flag is documented in
the testing flags section of the go tool documentation:

-run regexp
    Run only those tests and examples matching the regular
    expression.

答案2

得分: 48

如果有人在使用Ginkgo BDD框架进行Go编程时遇到相同的问题,可以通过将测试规范标记为focused(参见文档)来实现。在ItContextDescribe函数之前添加F即可。

所以,如果你有以下的规范:

	It("should be idempotent", func() {

你可以将其改写为:

	FIt("should be idempotent", func() {

这样只会运行这一个规范:

[Fail] testing Migrate setCurrentDbVersion [It] should be idempotent 
...
Ran 1 of 5 Specs in 0.003 seconds
FAIL! -- 0 Passed | 1 Failed | 0 Pending | 4 Skipped
英文:

In case someone that is using Ginkgo BDD framework for Go will have the same problem, this could be achieved in that framework by marking test spec as focused (see docs), by prepending F before It, Context or Describe functions.

So, if you have spec like:

	It("should be idempotent", func() {

You rewrite it as:

	FIt("should be idempotent", func() {

And it will run exactly that one spec:

[Fail] testing Migrate setCurrentDbVersion [It] should be idempotent 
...
Ran 1 of 5 Specs in 0.003 seconds
FAIL! -- 0 Passed | 1 Failed | 0 Pending | 4 Skipped

答案3

得分: 35

给定一个测试:

func Test_myTest() {
    //...
}

只运行该测试:

go test -run Test_myTest path/to/pkg/mypackage
英文:

Given a test:

func Test_myTest() {
    //...
}

Run only that test with:

go test -run Test_myTest path/to/pkg/mypackage

答案4

得分: 25

简单可靠:

go test -run TestMyFunction ./...

关于 ./... 的更多信息:
https://stackoverflow.com/a/28031651/5726621

英文:

Simple and reliable:

go test -run TestMyFunction ./...

More on ./... :
https://stackoverflow.com/a/28031651/5726621

答案5

得分: 15

你的测试套件结构如下所示:

type MyTestSuite struct {
  suite.Suite
}

func TestMyTestSuite(t *testing.T) {
  suite.Run(t, new(MyTestSuite))
}

func (s *MyTestSuite) TestMethodA() {
}

要在Go中运行测试套件的特定测试,你需要使用-testify.m参数。

go test -v <package> -run ^TestMyTestSuite$ -testify.m TestMethodA

更简单地说,如果方法名在包中是唯一的,你可以始终运行以下命令:

go test -v <package> -testify.m TestMethodA
英文:

Say your test suite is structured as following:

type MyTestSuite struct {
  suite.Suite
}

func TestMyTestSuite(t *testing.T) {
  suite.Run(t, new(MyTestSuite))
}

func (s *MyTestSuite) TestMethodA() {
}

To run a specific test of test suite in go, you need to use: -testify.m.

 go test -v &lt;package&gt; -run ^TestMyTestSuite$ -testify.m TestMethodA

More simply, if the method name is unique to a package, you can always run this

go test -v &lt;package&gt; -testify.m TestMethodA

答案6

得分: 12

以下是翻译好的内容:

go test -v -run

英文:
go test -v &lt;package&gt; -run &lt;TestFunction&gt;

huangapple
  • 本文由 发表于 2014年9月29日 11:35:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/26092155.html
匿名

发表评论

匿名网友

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

确定