> For the complete documentation index, see [llms.txt](https://wong-coupon.gitbook.io/flutter/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wong-coupon.gitbook.io/flutter/clean-code/coverage-codecov.md).

# Coverage

When I build a Flutter project, I like to set up coverage early.\
It helps me track test quality on every push and pull request.

## How to create a test badge like this?

![Codecov badge](https://codecov.io/gh/ngmduc2012/my_lang/branch/main/graph/badge.svg)

This badge is very useful because we can quickly see the test coverage status in the repository.

## Step 1: Login to Codecov

Go to: <https://codecov.io>\
Login with your GitHub account.

## Step 2: Generate `lcov.info` in local

Run:

```bash
flutter test --coverage
```

After that, you will have:

```
coverage/lcov.info
```

## Step 3: Create GitHub Actions workflow

Create file:

```
.github/workflows/coverage.yml
```

Add this content:

```yaml
name: Coverage

on:
  push:
    branches: [ main ]
  pull_request:

jobs:
  test-and-upload:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: subosito/flutter-action@v2
        with:
          channel: stable

      - run: flutter pub get
      - run: flutter test --coverage

      - uses: codecov/codecov-action@v4
        with:
          files: coverage/lcov.info
```

## Important: Public vs Private Repository

This workflow usually works directly for **public repositories**.

For **private repositories**, add `CODECOV_TOKEN` in GitHub Secrets, then update the Codecov step:

```yaml
      - uses: codecov/codecov-action@v4
        with:
          files: coverage/lcov.info
          token: ${{ secrets.CODECOV_TOKEN }}
```

## Step 4: Commit and push

Run:

```bash
git add .github/workflows/coverage.yml
git commit -m "Add coverage workflow"
git push
```

## Step 5: Check CI/CD status

Go to your GitHub repository -> **Actions** tab.\
Workflow **Coverage** should be green.

## Step 6: View result on Codecov

Open:

<https://app.codecov.io/gh/ngmduc2012/my_lang>

## Step 7: Add coverage badge to README

Add this code:

```md
[![codecov](https://codecov.io/gh/ngmduc2012/my_lang/branch/main/graph/badge.svg)](https://codecov.io/gh/ngmduc2012/my_lang)
```

## Note

If upload fails, check repository visibility and token setup first.

## Conclusion

This is my simple way to set up Flutter coverage with Codecov.\
It is easy to maintain and very helpful when reviewing pull requests.

[Buy Me a Coffee](https://buymeacoffee.com/ducmng12g) | [Support Me on Ko-fi](https://ko-fi.com/I2I81AEJG8)
