5
7

GitHub Actions で pytest の失敗箇所をコード中にインライン表示する

Last updated at Posted at 2023-07-28

GitHub Actions にはテストなどの実行結果を、Pull Requestのコード上にアノテーションされた形で表示する機能があります。Node.js などではこれが自動で行われることがあるためご存知の方も多いかもしれませんが、Pythonでも上手く活用できます。

image.png

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 .
5
7
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
5
7