1、ginkgo测试框架介绍:https://onsi.github.io/ginkgo/
2、重点是学习实践 + 问题记录 + 怎么解决
3、送福利:国内好用的ChatGpt有很多,比如:天工、文心一言、讯飞星火、通义万相等
1. 安装
xxx@macdeMacBook-Pro-3 /Volumes/mac/study/.../ginkgo go get github.com/onsi/ginkgo/v2/ginkgo
### 输出 ###
go: downloading github.com/onsi/ginkgo v1.16.5
go: downloading github.com/onsi/ginkgo/v2 v2.15.0
go: downloading golang.org/x/tools v0.16.1
go: downloading github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572
go: downloading github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38
go: added github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572
go: added github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38
go: added github.com/onsi/ginkgo/v2 v2.15.0
go: added golang.org/x/tools v0.16.1
xxx@macdeMacBook-Pro-3 /Volumes/mac/study/.../ginkgo go install github.com/onsi/ginkgo/v2/ginkgo
### 没有任何输出就是ok的 ###
xxx@macdeMacBook-Pro-3 /Volumes/mac/study/.../ginkgo go get github.com/onsi/gomega/...
### 输出 ###
go: downloading github.com/onsi/gomega v1.31.1
go: downloading github.com/google/go-cmp v0.6.0
go: downloading github.com/golang/protobuf v1.5.3
go: downloading golang.org/x/net v0.19.0
go: downloading gopkg.in/yaml.v3 v3.0.1
go: downloading google.golang.org/protobuf v1.28.0
go: downloading golang.org/x/text v0.14.0
go: upgraded github.com/onsi/gomega v1.30.0 => v1.31.1
2. 使用
(1)安装正确的话,在当前目录的平行目录下应该会有一个新增的bin目录,目录下有个可执行文件
ginkgo
xxx@macdeMacBook-Pro-3 /Volumes/mac/study/.../ginkgo ../bin/ginkgo version
Ginkgo Version 2.15.0
(2)需要配置一下环境变量,不然只能在这个目录下才能执行
xxx@macdeMacBook-Pro-3 /Volumes/mac/study/.../ginkgo vim ~/.bash_profile
# ...
export GINKGOPATH=/Volumes/mac/study/goRoot/LeetCodeGoLangWJ/LearnGoWithTests #Ginkgo测试框架学习
export PATH=$GOPATH/bin:$GOROOT/bin:$GINKGOPATH/bin:$PATH
# ...
xxx@macdeMacBook-Pro-3 /Volumes/mac/study/.../ginkgo . ~/.bash_profile
(3)之后就可以愉快的玩耍了
xxx@macdeMacBook-Pro-3 /Volumes/mac/study/.../ginkgo ginkgo version
Ginkgo Version 2.15.0
(4)按上面的官方介绍文档,通过命令生成一个最简单的ginkgo测试代码并执行测试
# 通过 ginkgo bootstrap 命令生成一个最简单的ginkgo测试代码
xxx@macdeMacBook-Pro-3 /Volumes/mac/study/.../ginkgo ginkgo bootstrap
Generating ginkgo test suite bootstrap for ginkgo in:
ginkgo_suite_test.go
# 执行很简单:直接在当前目录下,执行ginkgo即可
xxx@macdeMacBook-Pro-3 /Volumes/mac/study/.../ginkgo ginkgo
Failed to compile ginkgo:
# ginkgo
../../../pkg/mod/github.com/onsi/ginkgo/[email protected]/core_dsl.go:25:2: missing go.sum entry for module providing package github.com/go-logr/logr (imported by github.com/onsi/ginkgo/v2); to add:
go get github.com/onsi/ginkgo/[email protected]
FAIL ginkgo [setup failed]
FAIL
Ginkgo ran 1 suite in 477.31418ms
Test Suite Failed
可以看到,直接报错了,原因比较简单,就是当前目录下的go.sum文件中的很多包其实没有下载下来,执行命令再次下载即可(如果有没下载成功的,重复操作,直到全部成功)
ChatGpt给的标准解决方案:(国内好用的ChatGpt有很多,比如:天工、文心一言、讯飞星火、通义万相等)
- 这个错误通常意味着你在你的Go项目中引入了一个新的依赖,但是Go无法找到该依赖的go.sum条目。go.sum文件是Go用来跟踪依赖项的文件,它记录了每个依赖项的版本和哈希值。
- 要解决这个问题,你可以尝试运行go mod tidy,它会尝试自动下载缺少的依赖项并更新你的go.sum文件。如果这个命令无法解决问题,你可能需要手动检查并更新你的依赖项。
- 如果你使用的是GitHub上的依赖,你也可以尝试查看该依赖的GitHub仓库,看看是否有go.sum文件,如果没有,你可能需要联系依赖的维护者来获取它。
xxx@macdeMacBook-Pro-3 /Volumes/mac/study/.../ginkgo go mod tidy
go: downloading github.com/go-logr/logr v1.3.0
go: downloading golang.org/x/sys v0.15.0
go: downloading github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572
go: downloading golang.org/x/tools v0.16.1
go: downloading github.com/golang/protobuf v1.5.3
go: downloading gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405
go: downloading github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38
go: downloading github.com/stretchr/testify v1.6.1
go: downloading google.golang.org/protobuf v1.28.0
go: downloading github.com/pmezard/go-difflib v1.0.0
go: downloading github.com/davecgh/go-spew v1.1.1
然后再次执行测试,可以看到成功了
xxx@macdeMacBook-Pro-3 /Volumes/mac/study/.../ginkgo ginkgo
Running Suite: Ginkgo Suite - /Volumes/mac/study/goRoot/LeetCodeGoLangWJ/LearnGoWithTests/ginkgo
================================================================================================
Random Seed: 1705981574
Will run 0 of 0 specs
Ran 0 of 0 Specs in 0.000 seconds
SUCCESS! -- 0 Passed | 0 Failed | 0 Pending | 0 Skipped
PASS
Ginkgo ran 1 suite in 10.747687301s
Test Suite Passed
未完待续。。。
文章评论