3 - Customizing Codecov

Identify untested code

Now that we have coverage on our main branch, let’s see how to improve our coverage. Go back to Codecov and click your demo repository.

🚧

Not seeing coverage information?

If you are not seeing any coverage information, click settings on the top right and ensure your default branch is main.

Back on the dashboard, you will see some data relating to our recent commit. On the top is the coverage chart which will show you coverage change over time. Below that is a sunburst graph that highlights parts of the codebase that have low coverage. To the right of that are the most recent commits. However, we will use the bottom section, the Fileviewer, to identify lines of code that haven’t been tested.

608

Click on api and calculator.py to see what line hasn’t been covered.

671

Note that we haven’t added a test for dividing by 0.

Set coverage standards

Before we add the test, let’s ensure that our coverage always hits 100%. Create a new branch step3

git checkout main
git pull
git checkout -b 'step3'

Update codecov.yml in the root directory of the git repository.

codecov:
  bot: <bot-user>
ignore:
  - "app.py"

coverage:
  status:
    project:
      default:
        target: 100%
        threshold: 1%

This configuration tells Codecov to fail a status check if 100% of the codebase is not covered with tests. However, it also allows for a 1% threshold, meaning the true minimum is 99%.

Commit the new file and open a pull request
When opening pull requests, be sure to select your own repository as the base branch.

git add .
git commit -m 'step3: add project status check target'
git push origin step3

You will notice that after CI finishes, we have a failing status check

262

Since our project still has an uncovered line, we will need to add a test to cover it before we can merge our code.

Add a test for uncovered code

At the end of api/tests/test_calculator.py add a test for the divide by 0 case

def test_divide_by_0():
    assert Calculator.divide(2.0, 0) == 'Cannot divide by 0'

to the end of the file. Let’s commit our code again and see if our project is fully covered.

git add .
git commit -m 'step3: cover divide by 0 case'
git push origin step3

We now see all of our status checks passing

258

and the Codecov comment

965

shows coverage increasing in the api/calculator.py as we expect.

Add the Codecov badge

Finally, let’s add a Codecov badge to a README.md file. You can copy the Markdown code from the Settings tab in Codecov, under Badge.

948

Your README.md file will look something like

[![codecov](https://codecov.io/bb/{{REPOSITORY}}/branch/main/graph/badge.svg)](https://codecov.io/gh/{{REPOSITORY}})

As before, commit the changes and merge the pull request

git add .
git commit -m 'step3: add Codecov badge'
git push origin step3

You should see the badge appear on the repository screen in GitLab

742