2 - Getting a Codecov account and uploading coverage

Create a new branch step2

git checkout main
git pull
git checkout -b 'step2'

Before we continue, let’s create an account and set up our repository on Codecov.

Create a Codecov account

Create an account on Codecov by clicking on our signup page and following the prompts.

Set up the Team Bot.

Now that we have set up our Codecov account, let’s upload coverage reports.

Add the Codecov token

From Codecov, navigate to your repo's settings page. You should see a CODECOV_TOKEN value. You will need to add this as a variable in GitLab. Copy this token.

In GitLab, go to Settings -> CI/CD. Under Variables, add your token as shown below.

Select Add variable to save.

Create CI pipeline

Create a configuration file for your CI. The CI workflows below will checkout the code, install requirements, run tests, and upload the coverage reports to Codecov.

GitLabCI

Be sure to pull the CODECOV_TOKEN from the settings tab of the repository in Codecov.
You will need to add it as an environment variable in GitLab. Go to Settings -> CI -> Variables.

Add the following code to a new .gitlab-ci.yml file

api:
  image: python:latest
  script:
    - pip install -r api/requirements.txt
    - pytest --cov api/
    - curl -Os https://cli.codecov.io/latest/linux/codecov
    - chmod +x codecov
    - ./codecov upload-process -t $CODECOV_TOKEN

Commit the changes and run the CI workflow

Let’s commit our code and open a pull request. Run

git add .
git commit -m 'step2: upload coverage reports to Codecov'
git push origin step2

Next, go to the demo repository on GitLab and open a merge request (MR).

When opening merge requests, be sure to select your own repository as the base branch.

🚧

Token not found

You should expect to see
['info'] -> Token found by environment variables
in the logs in your CI. If you are not seeing this, make sure that you have added the variable and that the CI pipeline has access to the variable.

You will see status checks on the MR from the CI. You can click on them to see the progress of the CI/CD. Once complete, you will receive status checks and a comment from Codecov.

codecov/patch and codecov/project status checks

One of Codecov’s core uses is getting code coverage information directly in a developer’s workflow. The status checks shown above are part of that.

  • The project check is used to maintain a coverage percentage across an entire codebase
  • The patch check is used for code changed in a PR.

A more mature repository may have a strict project percentage, while a project new to code coverage may only expect new code to be well-tested (patch coverage). We will be setting some of these values in this section, but you can read more about status checks here.

If you go to the project on Codecov, you will notice that the dashboard is blank. This is because no coverage information has been uploaded onto the default branch. We can fix this by merging the pull request.

Merge the merge request on GitHub and wait for the CI/CD to complete.
When opening merge requests, be sure to select your own repository as the base branch.