GitHub Actions にはテストなどの実行結果を、Pull Requestのコード上にアノテーションされた形で表示する機能があります。Node.js などではこれが自動で行われることがあるためご存知の方も多いかもしれませんが、Pythonでも上手く活用できます。
pytest の結果をコード上に表示する
実は Pytest が公式にそのためのプラグインを提供しています。これをインストールした状態で GitHub Actions で pytest を実行するだけで機能します。
具体的には、以下のようにインストールしておけばいいでしょう。
# pip でインストールする場合
pip install pytest-github-actions-annotate-failures
# Poetry に含めておく場合
poetry add -G dev pytest-github-actions-annotate-failures@latest
あとは GitHub Actions 上でふつうに pytest
を実行するだけです。pytest実行時のコマンドラインオプションを変更したりする必要もありません。カンタン。
Ruff の結果もインライン表示できる
Flake8 を置き換える Python 用 Linter として、Ruff というものが有名になっています。
GitHub ActionsのPython用チュートリアル もいまや Flake8 のかわりに Ruff を用いています。
GitHub Actions で Ruff を実行する際に --format=github
オプションを付与しましょう。GitHub のアノテーション機能が検出できる形式で Ruff がエラーを出力してくれます。
- name: Lint with Ruff
run: poetry run ruff --format=github .