4a - Merging reports

Another core feature of Codecov is our processing and merging of coverage reports. That means we take in any number of reports and aggregate them together, so you can see your coverage data in one view. Let’s see what that looks like.

Before we continue, create a new branch step4.

git checkout main
git pull
git checkout -b 'step4'

Create a new API

So far, we've only dealt with code in our api/calculator folder. But what happens if there are multiple reports?

Create a new directory api/smiles.

mkdir api/smiles
touch api/smiles/__init__.py

Create a new file api/smiles/smiles.py

class Smiles:
    def smile():
        return ":)"
    def frown():
        return ":("

Now create a test file api/smiles/test_smiles.py

from .smiles import Smiles

def test_smile():
    assert Smiles.smile() == ":)"

Add a coverage step to CI/CD

Let's add a test and coverage step for the api/smiles directory. In your .github/workflows/api.yml file, replace the file with the below code

name: API workflow

on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest
    name: Test python API
    steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-python@v2
      with:
        python-version: '3.10'
    - name: Install requirements
      run: pip install -r api/requirements.txt
    - name: Run tests and collect coverage for calculator
      run: pytest --cov=api.calculator --cov-report=xml:calculator-coverage.xml
    - name: Run tests and collect coverage for smiles
      run: pytest --cov=api.smiles --cov-report=xml:smiles-coverage.xml
    - name: Upload coverage reports to Codecov with GitHub Action
      uses: codecov/[email protected]
      env:
        CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

We will now be producing two coverage reports calculator-coverage.xml and smiles-coverage.xml. Codecov will seamlessly merge those reports together. Create a pull request

git add .
git commit -m 'step4: add smiles'
git push origin step4

After CI/CD runs, let's take a look at the PR comment.

Notice that we have the two files api/smiles/smiles.py and api/smiles/test_smiles.py in our Impacted Files list. Codecov will list out the files that have new or changed coverage here. In the next section, we will see how to filter coverage by directory using Codecov Components.