Dart Code Coverage with Github Actions and CodeCov

Dart Code Coverage with Github Actions and CodeCov

Unit tests and Code Coverage not only improves the code quality but also provides agility in developing new features with confidence.

How to set it up for a Dart repository hosted on GitHub?

We will be using the following two packages for writing/running unit tests and collecting code coverage data.

PackageDescription
testProvides a standard way of writing and running tests in Dart
coverage A tool to collect test coverage information from Dart VM tests and convert it into ICOV format

The tests will run on every commit on the GitHub server using GitHub Actions. Once the code coverage data is collected, GitHub Action will upload it to CodeCov. On CodeCov, we can view nice and flashy coverage reports integrated with the code from Github repo.

OK, let’s set it up. First, add the following dependencies in pubspec.yaml:

dev_dependencies:
  test: ^1.18.2
  coverage: ^1.0.3

Next, create a new Action on GitHub. It will create a YAML file, paste the following code into the YAML file:

name: Dart CI

on: [push]

jobs:
  build:

    runs-on: ubuntu-latest
    
    container:
      image:  google/dart:latest
    
    steps:
    - uses: actions/checkout@v1
    - name: Install dependencies
      run: pub get
    - name: Run tests with coverage
      run: pub run test --coverage="coverage"
    - name: Convert coverage to ICOV
      run: pub run coverage:format_coverage --lcov --in=coverage --out=coverage.lcov --packages=.packages --report-on=lib
    - name: Upload coverage to Codecov  
      uses: codecov/codecov-action@v1.0.2
      with:
        token: ${{secrets.CODECOV_TOKEN}}
        file: coverage.lcov

Then, create an account on CodeCov and link it with your GitHub account. You should be able to see your GitHub repos in CodeCov. Copy the Repository Upload Token from CodeCov (you should be able to find it in settings or on clicking the repo name).

Finally, store the Upload Token in GitHub secrets for your repository.

We are all done. The next commit should trigger our GitHub Action and we should get our coverage on CodeCov.

Here are some screenshots from CodeCov for my Dart repo.

Links

Leave a Reply

Your email address will not be published.