单个测试通过,测试包却失败
来源:stackoverflow
时间:2024-02-23 09:18:26 295浏览 收藏
小伙伴们有没有觉得学习Golang很有意思?有意思就对了!今天就给大家带来《单个测试通过,测试包却失败》,以下内容将会涉及到,若是在学习中对其中部分知识点有疑问,或许看了本文就能帮到你!
问题内容
当我使用 go test -run testnewprobeserviceissingleton 运行单个测试时,它按预期通过。
每当我尝试使用项目根目录中的 go test ./... 测试整个包/应用程序时,就会出现问题:
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/[email protected]/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/[email protected]/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/[email protected]/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
应用程序构建没有任何错误:
madjlzz@madsfeirlab $ go build . madjlzz@madsfeirlab $
我的 golang 版本是:
madjlzz@MadSfeirLab $ go version go version go1.13.6 darwin/amd64
我还使用 mockgen 来模拟版本 1.4.3 中的接口
我是 golang 新手,但感觉我有点想念一些关于如何运行测试的东西......
您还可以通过克隆项目自行运行测试
解决方案
我假设你的代码看起来像(我有这样的问题):
for _, subtest := range subtests{
t.run(subtest.name,
func(t *testing.t) {
do something with subtest...
}
}
但它必须看起来像:
for _, s := range subtests{
subtest:=s
t.Run(subtest.name,
func(t *testing.T) {
do something with subtest...
}
}
注意subtest:=s
这是这种情况下可能的解决方案之一。
到这里,我们也就讲完了《单个测试通过,测试包却失败》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!
声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
139 收藏
-
204 收藏
-
325 收藏
-
478 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习