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 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 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 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 new 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 on a repo name.

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

We are all done. 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.