Go测试不运行特定的测试,尽管包含了标志。

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

Go test not running specific test despite flag inclusion

问题

文件夹结构:

./test
├── abc
│   └── abc_test.go
└── run_test.go

run_test.go

package test

import (
	. "gopkg.in/check.v1"

	"testing"
)

func Test(t *testing.T) {
	TestingT(t)
}

abc_test.go

package abc

import (
	"fmt"

	. "gopkg.in/check.v1"
)

type TestSuite struct{}

var _ = Suite(&TestSuite{})

func Test(t *testing.T) {
	TestingT(t)
}

func (s *TestSuite) SetUpSuite(c *C) {
	fmt.Println("start")
}

func (s *TestSuite) TestOne(c *C) {
	fmt.Println("start one")
}

func (s *TestSuite) TestTwo(c *C) {
	fmt.Println("start two")
}

运行所有测试:

go-playground > go test -v ./...                         
?       go-playground   [没有测试文件]
=== RUN   Test
OK: 0 通过
--- PASS: Test (0.00s)
PASS
ok      go-playground/test      (已缓存)
=== RUN   Test
start
start one
start two
OK: 2 通过
--- PASS: Test (0.00s)
PASS
ok      go-playground/test/abc  0.010s
?       go-playground/tree      [没有测试文件]

尝试只运行一个名为TestOne的测试,但都失败了

go-playground > go test -v -check=TestOne ./...                                                                                         INT 17:36:03
?       go-playground   [没有测试文件]
go-playground > go test -v -check=abc ./...                                   
?       go-playground   [没有测试文件]
go-playground > go test -v -gocheck.f=TestOne ./...                               
?       go-playground   [没有测试文件]
go-playground > go test -v -gocheck.f TestOne ./...                                
?       go-playground   [没有测试文件]
go-playground > go test -v -gocheck.f TestSuite ./...                               
?       go-playground   [没有测试文件]
go-playground > go test -v -gocheck.f TestSuite ./...                               
?           go-playground   [没有测试文件]
go-playground > go test -v -run TestOne ./test/...                                           
testing: 警告:没有要运行的测试
PASS
ok      go-playground/test      (已缓存) [没有要运行的测试]
testing: 警告:没有要运行的测试
PASS
ok      go-playground/test/abc  0.004s [没有要运行的测试]
go-playground > go test -v -run TestOne ./...                                                    
?       go-playground   [没有测试文件]
testing: 警告:没有要运行的测试
PASS
ok      go-playground/test      (已缓存) [没有要运行的测试]
testing: 警告:没有要运行的测试
PASS
ok      go-playground/test/abc  (已缓存) [没有要运行的测试]
?       go-playground/tree      [没有测试文件] 

似乎什么都不起作用。请指出我错过了什么?

英文:

Folder structure:

./test
├── abc
│   └── abc_test.go
└── run_test.go

run_test.go

package test

import (
	. "gopkg.in/check.v1"

	"testing"
)

func Test(t *testing.T) {
	TestingT(t)
}

abc_test.go

package abc

import (
	"fmt"

	. "gopkg.in/check.v1"
)

type TestSuite struct{}

var _ = Suite(&TestSuite{})

func Test(t *testing.T) {
	TestingT(t)
}

func (s *TestSuite) SetUpSuite(c *C) {
	fmt.Println("start")
}

func (s *TestSuite) TestOne(c *C) {
	fmt.Println("start one")
}

func (s *TestSuite) TestTwo(c *C) {
	fmt.Println("start two")
}

Running all tests work:

go-playground > go test -v ./...                         
?       go-playground   [no test files]
=== RUN   Test
OK: 0 passed
--- PASS: Test (0.00s)
PASS
ok      go-playground/test      (cached)
=== RUN   Test
start
start one
start two
OK: 2 passed
--- PASS: Test (0.00s)
PASS
ok      go-playground/test/abc  0.010s
?       go-playground/tree      [no test files]

Trying to run just the one test TestOne all failed

go-playground > go test -v -check=TestOne ./...                                                                                         INT 17:36:03
?       go-playground   [no test files]
go-playground > go test -v -check=abc ./...                                   
?       go-playground   [no test files]
go-playground > go test -v -gocheck.f=TestOne ./...                               
?       go-playground   [no test files]
go-playground > go test -v -gocheck.f TestOne ./...                                
?       go-playground   [no test files]
go-playground > go test -v -gocheck.f TestSuite ./...                               
?       go-playground   [no test files]
go-playground > go test -v -gocheck.f TestSuite ./...                               
?           go-playground   [no test files]
go-playground > go test -v -run TestOne ./test/...                                           
testing: warning: no tests to run
PASS
ok      go-playground/test      (cached) [no tests to run]
testing: warning: no tests to run
PASS
ok      go-playground/test/abc  0.004s [no tests to run]
go-playground > go test -v -run TestOne ./...                                                    
?       go-playground   [no test files]
testing: warning: no tests to run
PASS
ok      go-playground/test      (cached) [no tests to run]
testing: warning: no tests to run
PASS
ok      go-playground/test/abc  (cached) [no tests to run]
?       go-playground/tree      [no test files] 

Nothing seems to work. Mind pointing out what did I miss?

答案1

得分: 2

这主要是关于"gopkg.in/check.v1"的问题。作为替代,您可以cd abc并使用go test -v -check.f=TestOne。那样会起作用。您不应该期望go test -run TestOne能够工作。从go命令的角度来看,这不是一个测试。它的角度只有一个测试:func Test(t *testing.T) { TestingT(t) }

至于是否允许使用"gopkg.in/check.v1"进行包路径和包模式匹配的组合,例如:

go test -v -gocheck.f=TestOne ./...

我不确定。我建议联系该包的作者http://labix.org/gocheck。

英文:

This is mostly a question about "gopkg.in/check.v1". As an alternative you can cd abc and use go test -v -check.f=TestOne. That will work. You should not expect go test -run TestOne to work. This is a not a test from the go command's perspective. The only test from its perspective is func Test(t *testing.T) { TestingT(t) }.

As for whether combinations of package paths and package pattern matching are allowed with "gopkg.in/check.v1", such as:
> go test -v -gocheck.f=TestOne ./...

I am not sure. I suggest contacting the authors of the package http://labix.org/gocheck .

huangapple
  • 本文由 发表于 2021年8月17日 17:44:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/68815133.html
匿名

发表评论

匿名网友

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

确定