2012-05-09 17 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 Blackin 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 
(:「 1」で終わる coverage.outライン ないとここに)私はちょうど gcを入力して、 がまだカバーされていないすべての行のリストを持っている

+0

Go(2013/09/19)の最新バージョンは 'go test -coverprofile ' –

+0

@FrankShearar Okを使用しています。より視認性の高い答えをあなたのコメントに追加しました。 – VonC

+0

'go -coverprofile cover.out'を実行してから、ツールのカバー-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 go go)にはありません。 – oers

+0

私はそれが./all.bashによって構築されると信じています。私はATMを検証することはできませんが、私はCLが保留中であるので、私はリリース時ではありませんが、 '〜/ go/pkg/tool/linux_amd64'に表示されるcovバイナリタイムスタンプは、 – 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の内側に、フォルダを生成.coverフォルダのJUnitやフォルダごとにカバレッジ・レポート、およびすべてのプロジェクトの組み合わせカバレッジレポートテスト。

1

Test Coverage for Golang

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

は、それが正しくインストールされているチェックして、あなたはそれはあなたがするファイルに基づいて、.jsonファイルをRederますテストケースを実行した場合、あなたはあなたのターミナル

Run the Test Case

からのアクセスを持っていますコードカバレッジレポートを.htmlファイルで入手してください。

テストケースが完了したらを使用して.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 
関連する問題