コンテンツにスキップ

GitHub Actions で pytest のカバレッジを出力する

MishaKav/pytest-coverage-commentの action を使って GitHub の Pull Request に pytest のカバレッジを出力できる。 次のコードは PR のコメントにカバレッジを出力する例

---
- name: Build coverage file
  run: |
    pytest --junitxml=pytest.xml --cov-report=term-missing:skip-covered --cov=app tests/ | tee pytest-coverage.txt

- name: Pytest coverage comment
  uses: MishaKav/pytest-coverage-comment@main
  with:
    pytest-xml-coverage-path: ./path-to-file/coverage.xml
    hide-comment: false

出力例

Coverage

Coverage Report
FileStmtsMissCoverMissing
functions/example_completed
   example_completed.py641970%33, 39–45, 48–51, 55–58, 65–70, 91–92
functions/example_manager
   example_manager.py441175%31–33, 49–55, 67–69
   example_static.py40295%60–61
functions/my_exampels
   example.py20200%1–31
functions/resources
   resources.py26260%1–37
TOTAL105573930% 

次のコードは PR にコメントせず、Job の Summary にカバレッジを出力する例

- name: Pytest coverage comment
  id: coverageComment
  uses: MishaKav/pytest-coverage-comment@main
  with:
    pytest-xml-coverage-path: ./path-to-file/coverage.xml
    hide-comment: true

- name: Add Summary
  uses: actions/github-script@v6
  with:
    script: |
      core.summary.addRaw(${{ steps.coverageComment.outputs.summaryReport }}).write()

参考