go test 禁用缓存

go test cached
每当执行 go test 时,如果功能代码和测试代码没有变动,则在下一次执行时,会直接读取缓存中的测试结果,并通过 (cached) 进行标记。

要禁用测试缓存,可以通过 -count=1 标志来实现。

如下示例:

# ~/test/hello-drone/go_demo [master ✗ (fe95bec)] [13:45:16]
➜ go test -v ./...               
?       go_demo [no test files]
=== RUN   TestCreateUnixMillis
--- PASS: TestCreateUnixMillis (0.00s)
    utils_test.go:7: 1561614325740
PASS
ok      go_demo/utils   0.006s

# ~/test/hello-drone/go_demo [master ✗ (fe95bec)] [13:45:32]
➜ go test -v ./...
?       go_demo [no test files]
=== RUN   TestCreateUnixMillis
--- PASS: TestCreateUnixMillis (0.00s)
    utils_test.go:7: 1561614325740
PASS
ok      go_demo/utils   (cached)

# ~/test/hello-drone/go_demo [master ✗ (fe95bec)] [13:45:38]
➜ go test -v ./...
?       go_demo [no test files]
=== RUN   TestCreateUnixMillis
--- PASS: TestCreateUnixMillis (0.00s)
    utils_test.go:7: 1561614325740
PASS
ok      go_demo/utils   (cached)

# ~/test/hello-drone/go_demo [master ✗ (fe95bec)] [13:45:59]
➜ go test -v ./... -count=1
?       go_demo [no test files]
=== RUN   TestCreateUnixMillis
--- PASS: TestCreateUnixMillis (0.00s)
    utils_test.go:7: 1561614494115
PASS
ok      go_demo/utils   0.006s

# ~/test/hello-drone/go_demo [master ✗ (fe95bec)] [13:48:14]
➜ go test -v ./... -count=1
?       go_demo [no test files]
=== RUN   TestCreateUnixMillis
--- PASS: TestCreateUnixMillis (0.00s)
    utils_test.go:7: 1561614508408
PASS
ok      go_demo/utils   0.006s

# ~/test/hello-drone/go_demo [master ✗ (fe95bec)] [13:48:32]
➜

关于 -count=1 的解释,可以通过命令 go help testflag 来查看:

When 'go test' runs in package list mode, 'go test' caches successful
package test results to avoid unnecessary repeated running of tests. To
disable test caching, use any test flag or argument other than the
cacheable flags. The idiomatic way to disable test caching explicitly
is to use -count=1.

总结
在 Go 1.11 之前,通过 GOCACHE=off 的方式来禁用测试缓存:GOCACHE=off go test ./…

在 Go 1.11 之后,通过 -count=1 的方式来禁用测试缓存:go test -count=1 ./…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值