LoginSignup
5
2

More than 3 years have passed since last update.

GitHub ActionsのAnnotationにテスト内容を反映する

Posted at

Annotationなるもの

GitHub Actionsでworkflowを開くとAnnotationsという欄があって、テスト結果とかが表示されるのですが、例えば漫然とgo testを実行するとProcess completed with exit code 1.しか出てこないんですね。ちゃんとテスト内容について言及してほしい。

Problem Matcher

https://github.com/actions/toolkit/blob/master/docs/problem-matchers.md
このProblem Matcherという機能を使うと、ステップ毎のログをスキャンして、正規表現にマッチしたものをannotationに変換してくれるようです。

$ go test 
--- FAIL: TestGcdNegative (0.00s)
    math_test.go:22: failed
FAIL
exit status 1
FAIL    _/home/username/src/actions-playground     0.003s

go testの出力はこんな感じですね。複数行のマッチもできるようですが、とりあえずmath_test.go:22: failedにマッチさせたい場合は以下のようになります。

go-test-problem-matcher.json
{
    "problemMatcher": [
        {
            "owner": "go-test",
            "pattern": [
                {
                    "regexp": "^\\s*(.+):(\\d+): (.+)$",
                    "file": 1,
                    "line": 2,
                    "message": 3
                }
            ]
        }
    ]
}

数字は正規表現のグループ(それぞれの(~))の番号ですね。columnseveritycodeといったオプションもあるのでリンク先を参照して頂ければと思います。

これをリポジトリの/.github/に置いて、ワークフローファイルのstepsでテスト実行前にproblem matcherを有効にします。(::で始まるログ出力によってactions固有のコマンドが実行できます。参考

      - name: enable problem matcher
        run: |
          echo "::add-matcher::.github/go-test-problem-matcher.json"

成果

以下のようにAnnotationsに表示されるようになりました。(実は下に元のエラーも残っていますが)

image.png

あとプルリクエストが存在する場合、以下のようにコードにも表示してくれるんですね。元のテストの出力次第ですが、problem matcherをちゃんと設定すればかなり便利になりそうです。

image.png

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