首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“对单个测试成功,但在测试包时失败”

“对单个测试成功,但在测试包时失败”
EN

Stack Overflow用户
提问于 2020-08-02 11:34:13
回答 1查看 2.3K关注 0票数 4

当我使用go test -run TestNewProbeServiceIsSingleton运行单个测试时,它会像预期的那样通过。

每当我试图使用项目根目录中的go test ./...测试整个包/应用程序时,都会出现问题:

代码语言:javascript
复制
madjlzz@MadSfeirLab $ go test ./...              
?       github.com/madjlzz/madprobe     [no test files]
ok      github.com/madjlzz/madprobe/controller  (cached) [no tests to run]
?       github.com/madjlzz/madprobe/internal/alerter    [no test files]
?       github.com/madjlzz/madprobe/internal/mock       [no test files]
?       github.com/madjlzz/madprobe/internal/persistence        [no test files]
--- FAIL: TestInsertReturnErrorOnGetFailure (0.00s)
panic: Fail in goroutine after TestNewProbeServiceIsSingleton has completed [recovered]
        panic: Fail in goroutine after TestNewProbeServiceIsSingleton has completed [recovered]
        panic: Fail in goroutine after TestNewProbeServiceIsSingleton has completed

goroutine 23 [running]:
testing.tRunner.func1(0xc0000f6400)
        /usr/local/Cellar/go/1.13.6/libexec/src/testing/testing.go:874 +0x3a3
panic(0x12ccca0, 0xc000095190)
        /usr/local/Cellar/go/1.13.6/libexec/src/runtime/panic.go:679 +0x1b2
github.com/golang/mock/gomock.(*Controller).Finish(0xc000098ff0)
        /Users/madjlzz/Documents/Projects/Go/pkg/mod/github.com/golang/mock@v1.4.3/gomock/controller.go:246 +0x2b2
panic(0x12ccca0, 0xc000095190)
        /usr/local/Cellar/go/1.13.6/libexec/src/runtime/panic.go:679 +0x1b2
testing.(*common).Fail(0xc0000f6200)
        /usr/local/Cellar/go/1.13.6/libexec/src/testing/testing.go:609 +0x151
testing.(*common).FailNow(0xc0000f6200)
        /usr/local/Cellar/go/1.13.6/libexec/src/testing/testing.go:631 +0x2b
testing.(*common).Fatalf(0xc0000f6200, 0x1351581, 0x2e, 0xc0000c4140, 0x5, 0x5)
        /usr/local/Cellar/go/1.13.6/libexec/src/testing/testing.go:716 +0x90
github.com/golang/mock/gomock.(*Controller).Call.func1(0xc000098de0, 0x1301ca0, 0xc000094f90, 0x1343807, 0x3, 0xc000095110, 0x1, 0x1, 0x0, 0x0, ...)
        /Users/madjlzz/Documents/Projects/Go/pkg/mod/github.com/golang/mock@v1.4.3/gomock/controller.go:201 +0x486
github.com/golang/mock/gomock.(*Controller).Call(0xc000098de0, 0x1301ca0, 0xc000094f90, 0x1343807, 0x3, 0xc000095110, 0x1, 0x1, 0x5, 0xc00008a660, ...)
        /Users/madjlzz/Documents/Projects/Go/pkg/mod/github.com/golang/mock@v1.4.3/gomock/controller.go:217 +0xb4
github.com/madjlzz/madprobe/internal/mock.(*MockPersister).Get(0xc000094f90, 0x134426b, 0x7, 0x16, 0x0, 0x0)
        /Users/madjlzz/Documents/Projects/Go/src/github.com/madjlzz/madprobe/internal/mock/entity.go:53 +0xe5
github.com/madjlzz/madprobe/internal/prober.(*service).Insert(0xc000098e70, 0x134426b, 0x7, 0x1348f2d, 0x16, 0x0, 0x0, 0x5, 0xc00008a660, 0x156df40, ...)
        /Users/madjlzz/Documents/Projects/Go/src/github.com/madjlzz/madprobe/internal/prober/service.go:63 +0x19b
github.com/madjlzz/madprobe/internal/prober.TestInsertReturnErrorOnGetFailure(0xc0000f6400)
        /Users/madjlzz/Documents/Projects/Go/src/github.com/madjlzz/madprobe/internal/prober/service_test.go:59 +0x4fb
testing.tRunner(0xc0000f6400, 0x135be98)
        /usr/local/Cellar/go/1.13.6/libexec/src/testing/testing.go:909 +0xc9
created by testing.(*T).Run
        /usr/local/Cellar/go/1.13.6/libexec/src/testing/testing.go:960 +0x350
FAIL    github.com/madjlzz/madprobe/internal/prober     0.287s
?       github.com/madjlzz/madprobe/util        [no test files]
FAIL

应用程序构建时没有任何错误:

代码语言:javascript
复制
madjlzz@MadSfeirLab $ go build .   
madjlzz@MadSfeirLab $ 

我的Golang版本是:

代码语言:javascript
复制
madjlzz@MadSfeirLab $ go version
go version go1.13.6 darwin/amd64

我还使用mockgen来模拟1.4.3版本中的接口。

我对戈朗很陌生,但我觉得我有点想念如何进行测试.

您还可以通过克隆项目来自己运行这些测试。

EN

回答 1

Stack Overflow用户

发布于 2022-08-31 20:21:24

我假设您的代码看起来像(我有这样的问题):

代码语言:javascript
复制
for _, subtest := range subtests{
   t.Run(subtest.name,
        func(t *testing.T) {
           do something with subtest...
        }
}

但它必须看起来像:

代码语言:javascript
复制
for _, s := range subtests{
   subtest:=s

   t.Run(subtest.name,
        func(t *testing.T) {
           do something with subtest...
        }
}

subtest:=s的关注

这是在这种情况下可能的解决办法之一。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63215500

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档