3 回答

TA貢獻1829條經驗 獲得超7個贊
基于這篇文章, 這是我所做的:
main_test.go使用此內容創建了一個:
package main
// code based on technique explained here:
// https://www.elastic.co/blog/code-coverage-for-your-golang-system-tests
// you can look there if you want to see how not to execute this test
// when running unit test etc.
// This file is mandatory as otherwise the packetbeat.test binary is not generated correctly.
import (
"testing"
)
// Test started when the test binary is started. Only calls main.
func TestSystem(t *testing.T) {
main()
}
因為它是一個 Web 服務(因此處于無限循環中),所以我需要一種在 SIGTERM 上優雅退出的方法(而不會將其視為失?。?,所以我使用了該包go get gopkg.in/tylerb/graceful.v1并替換了(我使用go-restful) main.go 中的行
- log.Fatal(http.ListenAndServe(":"+port, nil))
+ graceful.Run(":"+port, 10*time.Second, nil)
然后我會像這樣運行測試
go test -c -covermode=count -coverpkg ./... -o foo.test
./foo.test -test.coverprofile coverage.cov & echo $! > /tmp/test.pid
運行我的測試套件
kill "$(cat /tmp/test.pid)"

TA貢獻2012條經驗 獲得超12個贊
你可能不想這樣做。運行具有覆蓋率、競爭檢測和/或其他工具的代碼會增加二進制文件的大小并使其變慢。在我的計算機上運行競爭檢測器和代碼覆蓋率要慢 25 倍。
僅go test -cover -race
用于測試和go build
部署時。這將為您提供您想要的輸出,盡管不是您想要的方式。
- 3 回答
- 0 關注
- 376 瀏覽
添加回答
舉報