Go/goコマンド/tool/covdata/subtract
表示
go tool covdata subtract
[編集]何をするコマンドか
[編集]dir1 から dir2 でカバーされている部分を引いたカバレッジデータを生成します。
dir1 - dir2 = outdir
基本的な使い方
[編集]mkdir -p ./result go tool covdata subtract -i=dir1,dir2 -o=./result
-i の第1引数から第2引数を引くという順序が重要です。
フラグ解説
[編集]-i:入力ディレクトリ(必須)
[編集]go tool covdata subtract -i=./all,./unit -o=./result
カンマ区切りでちょうど2つ指定します。第1引数が被減数、第2引数が減数です。
-o:出力ディレクトリ(必須)
[編集]mkdir -p ./result go tool covdata subtract -i=./all,./unit -o=./result
事前にディレクトリを作成しておく必要があります。
-pkg:対象パッケージの絞り込み
[編集]go tool covdata subtract -i=./all,./unit -o=./result \ -pkg=github.com/myorg/myapp/pkg/...
使用例
[編集]統合テストでしかカバーされていない箇所を特定
[編集]# ユニットテストのカバレッジ go test -test.gocoverdir=./unit ./... # 統合テストのカバレッジ go test -tags=integration -test.gocoverdir=./integration ./... # 統合テストから ユニットテストをカバーしている部分を引く mkdir -p ./only_integration go tool covdata subtract -i=./integration,./unit -o=./only_integration # 統合テストでしかカバーされていない関数を確認 go tool covdata func -i=./only_integration
ユニットテストで十分カバーされていない箇所を特定
[編集]# 全テスト合算のカバレッジ mkdir -p ./all go tool covdata merge -i=./unit,./integration -o=./all # 全体からユニットテストを引く = ユニットテストが届いていない箇所 mkdir -p ./not_covered_by_unit go tool covdata subtract -i=./all,./unit -o=./not_covered_by_unit go tool covdata func -i=./not_covered_by_unit
| コマンド | 操作 | 用途 |
|---|---|---|
merge
|
dir1 ∪ dir2
|
全カバレッジの合算 |
subtract
|
dir1 - dir2
|
特定テストでしか通らない箇所の特定 |
intersect
|
dir1 ∩ dir2
|
両方のテストで共通してカバーされている箇所の特定 |
まとめ
[編集]| フラグ | 用途 |
|---|---|
-i
|
入力ディレクトリ(必須・2つ指定・順序が重要) |
-o
|
出力ディレクトリ(必須) |
-pkg
|
対象パッケージの絞り込み |
-v
|
詳細ログレベル |
「ユニットテストでカバーできていない箇所を統合テストで補っている部分」を可視化できます。統合テストへの依存度を把握し、ユニットテストを充実させるべき箇所の特定に役立ちます。