gpt4 book ai didi

unit-testing - Go 单元测试正在运行从 "go get"命令导入的包

转载 作者:IT王子 更新时间:2023-10-29 01:35:05 26 4
gpt4 key购买 nike

我导入了一个包“gopkg.in/yaml.v2”。这是在目录结构下

src/
gopkg.in/
yaml.v2
main.go
abc.go
abc_test.go

当我在构建期间运行命令 go test -v ./... 时,它也会从导入的包 gopkg.in/yaml.v2 运行单元测试.在我的情况下不需要。

我是 Go 及其测试框架的新手。是否在谷歌中搜索了答案但找不到任何具体的答案?

最佳答案

go 工具根据您的指示运行所有包的测试。

3 个点 ... 是 go 工具的一个特性,它表示给定的包和在其子文件夹中找到的所有包。引用自 Command Go: Description of package lists: 的文档

An import path is a pattern if it includes one or more "..." wildcards, each of which can match any string, including the empty string and strings containing slashes. Such a pattern expands to all package directories found in the GOPATH trees with names matching the patterns.

To make common patterns more convenient, there are two special cases. First, /... at the end of the pattern can match an empty string, so that net/... matches both net and packages in its subdirectories, like net/http. Second, any slash-separated pattern element containing a wildcard never participates in a match of the "vendor" element in the path of a vendored package, so that ./... does not match packages in subdirectories of ./vendor or ./mycode/vendor, but ./vendor/... and ./mycode/vendor/... do. Note, however, that a directory named vendor that itself contains code is not a vendored package: cmd/vendor would be a command named vendor, and the pattern cmd/... matches it. See golang.org/s/go15vendor for more about vendoring.

如果您只想运行自己的包的测试,请不要将 ... 附加到它。导航到您的包,然后运行 ​​go test .。或者从任何文件夹运行 go test your/package

当然,要做到这一点(无论如何都推荐这样做),您应该将代码放入一个包中,而不是直接放入 $GOPATH/src 文件夹中。例如。将其放入文件夹 $GOPATH/src/my/example。然后你可以像 go test my/example 一样测试它。

关于unit-testing - Go 单元测试正在运行从 "go get"命令导入的包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47362140/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com