LoginSignup
6
0

More than 3 years have passed since last update.

CircleCIでGolangのテストカバレッジを確認できるようにする

Posted at

GameWith Advent Calendar 2020 の記事になります!

概要

CircleCIでGolangのテストカバレッジを簡易的に確認できるようにします。
Golang純正のテスト機能とカバレッジ出力機能を利用して、CircleCIのArtifactsに保存して見れるようにします。

設定

※通常CIに入れるような処理は省いて、テストカバレッジを保持する部分のみを記載します。

Makefileでのコマンド設定

Makefile
# テストを行い、テストカバレッジレポートファイルを出力する
test-coverage:
    @if [ ! -e ./coverage ]; then mkdir ./coverage; fi
    @go test -v -coverprofile=./coverage/gotest.profile ./... 2>&1 | tee ./coverage/gotest.log
    @go tool cover -html=./coverage/gotest.profile -o ./coverage/index.html

gitignoreでテストカバレッジレポートフォルダを無視する

以下の設定を追加します。

.gitignore
coverage/*

CircleCIのJobに組み込む

以下のようなCircleCIのjobを作成し(ビルドやリントなどのついでに実施でも良い)、

.circleci/config.yml
jobs:
  test-coverage:
    executor: default
    steps:
      - checkout
    # テストを行いカバレッジレポートファイルを出力します
      - run:
          name: make test coverage file
          command: make test-coverage
      # レポートファイルを Artifacts として保持させます
      - store_artifacts:
          path: coverage
      - store_test_results:
          path: coverage

結果

CircleCIのワークフローのページから、テストカバレッジレポートファイルをArtifactsに保存したJobを開き、
「ARTIFACTS」タブから、 coverage/index.html をクリックすると、カバレッジレポートを見ることができます。

スクリーンショット 2020-12-14 12.55.29.png

1ファイルずつセレクトボックスから選択して、カバレッジが何%かどうかや、実際のコードからテストが通っていない箇所が赤く表示され確認できます。

スクリーンショット 2020-12-14 12.57.16.png

6
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
0