Flutter でも Codecov を導入して test モチベを上げよう!


Codecov というカバレッジ計測のサービスを Flutter での開発に導入したのですが、めちゃめちゃ良いので紹介。
有名どころだと provider パッケージ で使われていますね。
連携がとても簡単な割に便利すぎるので、やっといて損はないと思います!
しかも private リポジトリでも1個までは無料!

筆者は、Flutter の Unit/Widget test を CircleCI で回していた(今は GitHub Actions)のですが、そこに Codecov を連携させた話を簡潔に書きます。
(参考: こんな感じの構成で開発しています)

手順

冒頭に貼った画像のように「GitHub の PR にテストのカバレッジをコメントしてもらう」のを実現する手順

1. flutter test--coverage オプションを追加

例えば、こんな感じ。

cirlceci/config.yml
version: 2.1

orbs:
  codecov: codecov/[email protected] # See: https://circleci.com/orbs/registry/orb/codecov/codecov

jobs:
  flutter_analyze_and_test:
    environment:
      - LANG: en_US.UTF-8
    docker:
      - image: circleci/android:api-28
    steps:
      - checkout
      - run:
          name: install flutter sdk
          command: git clone -b stable https://github.com/flutter/flutter.git ~/flutter
      - run:
          name: flutter analyze
          command: ~/flutter/bin/flutter analyze --write=analyzer-output.txt
      - run:
          name: flutter test
          command: ~/flutter/bin/flutter test --coverage --coverage-path=~/coverage/lcov.info
      - codecov/upload:
          file: ~/coverage/lcov.info

2. codecov.yml を追加

例えば、こんな感じ。

.codecov.yml
# See: https://docs.codecov.io/docs/codecov-yaml
codecov:
  notify:
    require_ci_to_pass: yes

coverage:
  precision: 2
  round: down
  range: "70...100"

  status:
    project:
      default:
        target: 70%
    patch: no # See: https://docs.codecov.io/docs/commit-status#section-patch-status
    changes: no

parsers:
  gcov:
    branch_detection:
      conditional: yes
      loop: yes
      method: no
      macro: no

comment: # See: https://docs.codecov.io/docs/pull-request-comments
  layout: "reach, diff, flags, files"
  behavior: default
  require_changes: no
  require_base: yes
  require_head: yes

3. Codecov と連携

Codecov からレポジトリ選んで~~~というのを指示通りにやっていく!