2012-05-09 57 views

回答

60

注意Go 1.2 (Q4 2013, rc1 is available)現在將顯示test coverage results

One major new feature of go test is that it can now compute and, with help from a new, separately installed " go tool cover " program, display test coverage results.

The cover tool is part of the go.tools subrepository . It can be installed by running

$ go get golang.org/x/tools/cmd/cover 

The cover tool does two things.

  • First, when " go test " is given the -cover flag, it is run automatically to rewrite the source for the package and insert instrumentation statements. The test is then compiled and run as usual, and basic coverage statistics are reported:
$ go test -coverprofile fmt 
ok  fmt 0.060s coverage: 91.4% of statements 
$ 

Second, for more detailed reports, different flags to "go test" can create a coverage profile file, which the cover program, invoked with " go tool cover ", can then analyze.

Frank Sheararmentions

The latest versions of Go (2013/09/19) use:

go test -coverprofile <filename> <package name> 

Details on how to generate and analyze coverage statistics can be found by running the commands

$ go help testflag 
$ go tool cover -help 

Ivan Black提到in the comments

go test -coverprofile cover.out and then
go tool cover -html=cover.out -o cover.html opens cover.html in browser

我甚至不希望等待瀏覽器打開,所以我定義這個別名:

alias gc=grep -v -e " 1$" coverage.out 

那我只需鍵入gc,並已全部尚未覆蓋的行的列表(此處用coverage.out與「1」結尾)。

+0

請注意,最新版本的Go(2013/09/19)使用'go test -coverprofile <程序包名稱>' –

+0

@FrankShearar Ok。我已將您的評論納入答案中,以獲得更多的知名度。 – VonC

+0

'go test -coverprofile cover.out'然後'go tool cover -html = cover.out -o cover.html'在瀏覽器中打開'cover.html' –

5

這是正確的here,一些文檔here

$ go tool 
6a 
6c 
6g 
6l 
addr2line 
api 
cgo 
cov 
dist 
ebnflint 
fix 
gotype 
nm 
objdump 
pack 
pprof 
prof 
vet 
yacc 
$ go tool cov -h 
usage: cov [-lsv] [-g substring] [-m minlines] [6.out args...] 
-g specifies pattern of interesting functions or files 
go tool cov: exit status 1 
$ 

我還沒有使用它,這是我所知道的。

+0

你需要手動安裝嗎?在我的本地安裝(去版本go1)它不在那裏。 – oers

+0

我相信它是通過./all.bash構建的。我無法驗證自動櫃員機,我沒有發佈,因爲我有一個待定的CL,但是我在'〜/ go/pkg/tool/linux_amd64'中看到的cov二進制時間標記與我昨天的最後一個Go版本相匹配。 – zzzz

+0

是的,運行./all.bash,你會擁有它。感謝您的幫助,jnml! –

18

Go帶有非常棒的測試和覆蓋工具。儘管所有Go工具都有詳細記錄go tool cover -help我建議閱讀The cover story article on the official Go blog。它有很多例子,我強烈推薦它!

我在〜/ .bash_profile中有這個函數。 (你可以將它粘貼到終端中試一試)。

cover() { 
    t="/tmp/go-cover.$$.tmp" 
    go test -coverprofile=$t [email protected] && go tool cover -html=$t && unlink $t 
} 

然後,只需cd成細末項目/包文件夾,然後鍵入cover。 這將在瀏覽器中打開一個可視化工具,它向您顯示當前包中每個文件的經過測試和未經測試的代碼。非常有用的命令!我強烈建議它找到未經100%測試的產品!顯示的結果是每個文件。從左上角的下拉菜單中,您可以看到所有文件的結果。

使用這個命令還可以檢查任何封裝的覆蓋範圍,例如:

cover fmt

從這個命令中終端的輸出將是:

ok  fmt 0.031s coverage: 91.9% of statements 

除了在您的瀏覽器,您將看到此工具以紅色顯示未覆蓋測試的所有代碼行:

enter image description here

也可以保存html覆蓋文件,而不是在瀏覽器中打開它。當您的測試+覆蓋由像Jenkins這樣的CI工具運行時,這非常有用。這樣,您可以從中央服務器提供覆蓋率文件,整個團隊將能夠查看每個構建的覆蓋率結果。

+0

從這裏複製的代碼https://coderwall.com/p/rh-v5a/get-coverage-of-golang-test –

+0

有趣的,我會測試它。 +1 – VonC

+0

這太棒了!感謝分享。因爲我想在我的程序中測試一個主要軟件包,所以最終進入了一個測試腳本。乾杯 –

2

通過幫助菜單和其他回答這個問題的啓發,只需要運行:

f=cover.out; if [ -f $f ]; then rm $f; fi; go test -coverprofile $f && go tool cover -html $f 
1

嘗試使用gaia-docker/base-go-build泊塢圖片。

這是Docker映像包含構建和測試覆蓋範圍所需的全部內容。 在Docker容器內運行測試覆蓋率將創建.cover文件夾,其中包含項目的測試覆蓋率結果。運行在所有項目上

docker run --rm -v "$PWD":$PROJECT_PATH -w $PROJECT_PATH $BUILDER_IMAGE_NAME /go/script/coverage.sh 

test coverage script文件夾,並生成,內部設備罩起來文件夾JUnit和每個文件夾覆蓋報告,和所有項目的結合覆蓋報告的測試。

1

Test Coverage for Golang

go get github.com/axw/gocov/gocov go get -u gopkg.in/matm/v1/gocov-html

,查看其是否正確安裝,你必須從你的終端

Run the Test Case

訪問如果運行測試用例基於文件你將它REDER的上傳.json文件在.html文件中獲得代碼覆蓋率報告

gocov test >your_Coverage_report.json

一旦您的測試用例完成在.html文件中使用生成報告。JSON

gocov-html your_Coverage_report.json >your_Coverage_report.html

參考

GoTest Coverage Tool for go lang

Go Test Report Tool

Alternate Method

入鄉隨俗測試覆蓋率

go test -coverprofile=coverage.out 
go tool cover -html=coverage.out