Activate test coverage for python code on Gitlab

Viewed 1305

I'm trying to create a .gilab-ci.yml step to activate the gitlab's test coverage with pytest + pytest-cov.

Current unsuccessful snippet

I've tried:

.only-default: &only-default
  only:
    - merge_requests

stages:
  - test

test-py:
  stage: test
  image: "python:3.8"
  script:
    - pip install -r requirements.txt
    - python -m pytest -vvv  src --cov-report xml --cov=src
  artifacts:
    reports:
      cobertura: coverage.xml

Among other packages used for my project, the requirements.txt file contains pytest and pytest-cov.

The associated pipeline outputted:

Uploading artifacts...
coverage.xml: found 1 matching files and directories 
Uploading artifacts as "cobertura" to coordinator... ok  id=858390324 responseStatus=201 Created token=6uBetoBX

But I'm unable to see the new feature in my MR.

Does anyone have a working solution to activate the option ?

Reference page

https://docs.gitlab.com/ee/user/project/merge_requests/test_coverage_visualization.html

2 Answers

The documentation states that coverage.py is needed to convert the report to use full relative paths. The information isn’t displayed without the conversion.

So in your example, instead of:

- python -m pytest -vvv  src --cov-report xml --cov=src

Do:

- python -m pytest -vvv  src --cov=src
- coverage xml -o coverage.xml
Related