Syed Umar AnisMobileDart Code Coverage with Github Actions and CodeCov (using test_coverage package)
Syed Umar AnisMobileDart Code Coverage with Github Actions and CodeCov (using test_coverage package)

The test_coverage package has not been updated for quite some time and is not playing well with some of the fewer packages. It also doesn’t support null safety. The replacement is the new ‘coverage’ package from the Dart tools team.

The ‘coverage’ package should be used for new projects. See the post here to set it up.

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
test_coverage A simple command-line tool to collect test coverage information from Dart VM tests

The tests will run on every commit on GitHub server using the newly released GitHub feature called 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.5.0
  test_coverage: ^0.3.0

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
      run: pub run test
    - name: Code Coverage
      run: pub run test_coverage
    - name: Upload coverage to Codecov  
      uses: codecov/codecov-action@v1.0.2
      with:
        token: ${{secrets.CODECOV_TOKEN}}
        file: coverage/lcov.info

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

Hi, I’m Umar

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *