LoginSignup
1
2

More than 1 year has passed since last update.

Github ActionでCIのテストレポートを取得

Last updated at Posted at 2023-03-29

プルリクエストをした後、Github Actionは自動的にテストを実行し、
一般的には合格か不合格の結果だけが表示されます。
テストレポートをどのように取得するのでしょうか?
以下にいくつかの方法を紹介します。

Codecov

Codecovに登録すると、自動的にアクセス可能なリポジトリが読み込まれます。
image.png
setup repo,をクリックし、手順に従って設定します。
image.png

Railsで以下の設定が必要です。

gemを追加

# Gemfile

group :test do
  ...
  gem 'simplecov'
  gem 'simplecov-cobertura'
end

テストレポートのフォーマットを設定

# test_helper.rb
require 'simplecov'
require 'simplecov-cobertura'

SimpleCov.start do
  coverage_dir 'tmp/coverage'
end

SimpleCov.formatter = SimpleCov::Formatter::CoberturaFormatter

actionを設定

- name: Upload coverage reports to Codecov
  uses: codecov/codecov-action@v3
  with:
    token: ${{ secrets.CODECOV_TOKEN }}
    files: ./tmp/coverage/coverage.xml
    fail_ci_if_error: true

結果

image.png
image.png

simplecov-report-action

最もシンプルなテストカバレッジレポート

actionを設定

actionを設定するだけでOKです

- name: Simplecov Report
  uses: aki77/simplecov-report-action@v1
  with:
    token: ${{ secrets.GITHUB_TOKEN }}
    failedThreshold: 10
    resultPath: tmp/coverage/.last_run.json

結果

image.png

cobertura-action

より詳細なテストカバレッジレポート

gemを追加

# Gemfile

group :test do
  ...
  gem 'simplecov'
  gem 'simplecov-cobertura'
end

テストレポートのフォーマットを設定

# test_helper.rb
require 'simplecov'
require 'simplecov-cobertura'

SimpleCov.start do
  coverage_dir 'tmp/coverage'
end

SimpleCov.formatter = SimpleCov::Formatter::CoberturaFormatter

actionを設定

- uses: 5monkeys/cobertura-action@master
  with:
    path: tmp/coverage/coverage.xml
    repo_token: ${{ secrets.GITHUB_TOKEN }}
    minimum_coverage: 30
    show_missing: true
    show_class_names: true
    link_missing_lines: true
    report_name: code coverage results

結果

image.png

完成例

name: Rails Ci

on:
  pull_request:
    branches:
      - develop
    types:
      - opened
      - synchronize
      - ready_for_review

concurrency: 
    group: ${{ github.workflow }}-${{ github.ref }}
    cancel-in-progress: true

env:
  GH_TOKEN: "${{secrets.GH_TOKEN}}"

jobs:
  test:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
      with:
        token: ${{ secrets.GH_TOKEN }}
    - name: Set up Ruby
    # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
    # change this to (see https://github.com/ruby/setup-ruby#versioning):
      uses: ruby/setup-ruby@v1
      with:
        bundler-cache: true # runs 'bundle install' and caches installed gems automatically
    - name: Run tests
      run: bundle exec rake test
    - name: Upload coverage reports to Codecov
      uses: codecov/codecov-action@v3
      with:
        token: ${{ secrets.CODECOV_TOKEN }}
        files: ./tmp/coverage/coverage.xml
        fail_ci_if_error: true
    - name: Simplecov Report
      uses: aki77/simplecov-report-action@v1
      with:
        token: ${{ secrets.GITHUB_TOKEN }}
        failedThreshold: 10
        resultPath: tmp/coverage/.last_run.json
    - uses: 5monkeys/cobertura-action@master
      with:
        path: tmp/coverage/coverage.xml
        repo_token: ${{ secrets.GITHUB_TOKEN }}
        minimum_coverage: 80
        show_missing: true
        show_class_names: true
        link_missing_lines: true
        report_name: code coverage results
1
2
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
1
2