簡體   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