繁体   English   中英

Go test 编译成功,但 go build 没有

[英]Go test compiles successfully, but go build doesn't

我正在尝试在 go(在 Windows 中)中测试一个主包,并且测试似乎被缓存了,尽管我完全删除了缓存( go clean -cache )。

为了测试这一点,我更改了一个文件( parseinput.go ),以便在编译期间产生错误(未定义的变量)。 结果是,无法构建主包:

go\src\xxx\apimonitor> go build
# xxx/apimonitor
.\inputparser.go:15:2: undefined: err
.\inputparser.go:16:19: undefined: err

,但测试仍然成功完成( go test甚至go test -a ):

go\src\xxx\apimonitor> go test
PASS
ok      xxx/apimonitor  0.786s

关于为什么这种情况不断发生以及为什么测试不重新编译的任何线索? 这个包可能从以前的构建缓存的任何其他地方?


更新

添加一些打印语句后,似乎 test ( go test ) 编译成功inputparser.go (尽管err变量未定义),但构建失败(如上所示)。 这就是让我相信测试被缓存的原因。 以下是构建失败的源代码示例:

func parseStoreInput(strArray []string) (inputStoreTransactionHash, error) {
    var parsedIn inputStoreTransactionHash
    if !validateInput(strArray, 1, true) {
        return parsedIn, errors.New("Expecting an escaped JSON string as input")
    }
    err = json.Unmarshal([]byte(strArray[0]), &parsedIn)
    return parsedIn, err
}

关于为什么会发生这种情况的任何线索/文件?

命令去

编译包和依赖

构建标志由 build、clean、get、install、list、run 和 test 命令共享:

 -a force rebuilding of packages that are already up-to-date. -n print the commands but do not run them. -v print the names of packages as they are compiled. -x print the commands.

删除目标文件和缓存文件

用法:

 go clean [clean flags] [build flags] [packages]

-cache标志导致 clean 删除整个 go build 缓存。

-testcache标志导致 clean 使 go build 缓存中的所有测试结果过期。

测试标志

当' go test '以包列表模式运行时,' go test '缓存成功的包测试结果,以避免不必要的重复运行测试。 要禁用测试缓存,请使用除可缓存标志之外的任何测试标志或参数。 显式禁用测试缓存的惯用方法是使用-count=1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM