有沒有人成功爲Go單元測試生成代碼覆蓋率?我無法在網絡上找到該工具。如何測量Golang中的代碼覆蓋率?
回答
注意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 thego.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.
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
go test -coverprofile cover.out
and then
go tool cover -html=cover.out -o cover.html
openscover.html
in browser
我甚至不希望等待瀏覽器打開,所以我定義這個別名:
alias gc=grep -v -e " 1$" coverage.out
那我只需鍵入gc
,並已全部不尚未覆蓋的行的列表(此處用coverage.out
線不與「1
」結尾)。
$ 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
$
我還沒有使用它,這是我所知道的。
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
除了在您的瀏覽器,您將看到此工具以紅色顯示未覆蓋測試的所有代碼行:
也可以保存html覆蓋文件,而不是在瀏覽器中打開它。當您的測試+覆蓋由像Jenkins這樣的CI工具運行時,這非常有用。這樣,您可以從中央服務器提供覆蓋率文件,整個團隊將能夠查看每個構建的覆蓋率結果。
從這裏複製的代碼https://coderwall.com/p/rh-v5a/get-coverage-of-golang-test –
有趣的,我會測試它。 +1 – VonC
這太棒了!感謝分享。因爲我想在我的程序中測試一個主要軟件包,所以最終進入了一個測試腳本。乾杯 –
通過幫助菜單和其他回答這個問題的啓發,只需要運行:
f=cover.out; if [ -f $f ]; then rm $f; fi; go test -coverprofile $f && go tool cover -html $f
嘗試使用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和每個文件夾覆蓋報告,和所有項目的結合覆蓋報告的測試。
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
Alternate Method
入鄉隨俗測試覆蓋率
go test -coverprofile=coverage.out
go tool cover -html=coverage.out
- 1. 如何測量TCL代碼覆蓋率?
- 2. 我如何測試代碼覆蓋率?
- 3. 如何測量Golang集成測試覆蓋率?
- 4. 如何從我的Golang服務器獲取代碼覆蓋率?
- 5. 如何測量背景程序的代碼覆蓋率?
- 6. PHPUnit代碼覆蓋率顯示單元測試的代碼覆蓋率?
- 7. 如何測量測試覆蓋率
- 8. 測試代碼覆蓋率的JRuby
- 9. 僅在新代碼上測量代碼覆蓋率
- 10. 使用aqtime測量單元測試的代碼覆蓋率
- 11. 代碼覆蓋率:.NET代碼覆蓋率和SonarQube VSTS集成
- 12. 聲納代碼覆蓋率:%代碼覆蓋率不一致
- 13. AngularJS中量角器測試的代碼覆蓋率
- 14. 由groovy代碼測試的java代碼的代碼覆蓋率
- 15. APEX測試類0%代碼覆蓋率
- 16. Android測試代碼覆蓋率,Eclipse
- 17. maven單元測試代碼覆蓋率
- 18. 谷歌測試:代碼覆蓋率
- 19. .NET代碼覆蓋率監測
- 20. rspec的代碼覆蓋率
- 21. PHPUnitSeleniumTestcase的代碼覆蓋率
- 22. 通過API測試測量C#.net代碼覆蓋率
- 23. 用黑匣子測試測量代碼覆蓋率
- 24. 代碼覆蓋率 - 哪個代碼覆蓋哪個代碼?
- 25. 增量.NET代碼覆蓋率工具?
- 26. 如何最大化代碼覆蓋率?
- 27. 測量代碼覆蓋率的典型工作流程
- 28. 德爾福的測量代碼覆蓋率
- 29. 真實硬件目標上的代碼覆蓋率測量
- 30. Sonarqube沒有測量來自TFS 2017的代碼覆蓋率Build
請注意,最新版本的Go(2013/09/19)使用'go test -coverprofile<程序包名稱>' –
@FrankShearar Ok。我已將您的評論納入答案中,以獲得更多的知名度。 – VonC
'go test -coverprofile cover.out'然後'go tool cover -html = cover.out -o cover.html'在瀏覽器中打開'cover.html' –