【问题标题】:Go test not running specific test despite flag inclusion尽管包含标志,但 go test 未运行特定测试
【发布时间】:2021-10-19 05:33:06
【问题描述】:

我无计可施。

文件夹结构:

./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   [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]

尝试只运行一个测试TestOne 全部失败

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] 

似乎没有任何效果。介意指出我错过了什么吗?

【问题讨论】:

    标签: go testing


    【解决方案1】:

    这主要是关于“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”是否允许包路径和包模式匹配的组合,如:

    去测试 -v -gocheck.f=TestOne ./...

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

    【讨论】:

    • 嘿蒂姆,感谢您的回复。我试图添加一个 gocheck 标签,但它不是目前存在的标签。我想这是有道理的,从 Go 的角度来看,这些标志是没有意义的,但是感谢您指出我应该去哪里进一步澄清
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    相关资源
    最近更新 更多