【发布时间】:2018-07-30 15:55:32
【问题描述】:
问题:
当我运行相同的 go 测试两次时,第二次运行根本没有完成。结果是第一次运行时缓存的结果。
PASS
ok tester/apitests (cached)
链接
我已经检查了https://golang.org/cmd/go/#hdr-Testing_flags,但没有用于此目的的 cli 标志。
问题:
是否有可能强制 go test 始终运行测试而不缓存测试结果。
【问题讨论】:
问题:
当我运行相同的 go 测试两次时,第二次运行根本没有完成。结果是第一次运行时缓存的结果。
PASS
ok tester/apitests (cached)
链接
我已经检查了https://golang.org/cmd/go/#hdr-Testing_flags,但没有用于此目的的 cli 标志。
问题:
是否有可能强制 go test 始终运行测试而不缓存测试结果。
【问题讨论】:
在 Go11 中,我无法使用带有模块的 GOCACHE 禁用缓存,而是使用 -count=1:
go test -count=1
Go11 之前:
GOCACHE=off go test
或者,清理测试缓存并再次运行测试:
go clean -testcache && go test
【讨论】:
我解决这个问题的方法(我在 macOS 上使用 Visual Studio Code):
代码 > 首选项 > 设置
点击设置页面右侧的...
点击Open settings.json
要么:
将以下 sn-p 添加到您的 settings.json 文件中
"go.testEnvVars": {
"GOCACHE": "off"
}
go.testEnvVars 的值以包括以下内容:"GOCACHE": "off"
【讨论】:
GOCACHE 将不适用于最新版本的 Go。 VS Code 的一个解决方案是在设置中设置"go.testFlags": ["-count=1"]。
还有GOCACHE=off提到here。
【讨论】:
go 1.11 并在使用 GOCACHE=off 时具有 go modules 功能会导致错误 go: cannot use modules with build cache disabled。最好使用建议的-count 1。
GOCACHE 将在 go 1.12 中逐步淘汰,所以现在使用 go test -count=1 ... 是更安全的选择。
build cache is disabled by GOCACHE=off, but required as of Go 1.12
testing flags docs 中描述了一些选项:
go clean -testcache:所有测试结果过期-count=1
也就是说,您的代码或测试代码的更改将使缓存的测试结果无效(使用本地文件或环境变量时也有扩展逻辑),因此您不需要手动使测试缓存无效。
【讨论】:
go clean -testcache ./... 也可以工作(在 monorepo 的顶部)