Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code coverage in gitlab CI/CD

I used Docker-dind to build and test my python code. I confused how to run coverage in gitlab-ci between two following options.

1) Gitlab has coverage by itself [here]

2) I follow python's coverage tutorial and create my own coverage with following:

 coverage:
  stage: test
  script:
      - docker pull $CONTAINER_TEST_IMAGE
      - docker run $CONTAINER_TEST_IMAGE python -m coverage run tests/tests.py
      - docker run $CONTAINER_TEST_IMAGE python -m coverage report -m

When gitlab throws an exception No data to report.:

enter image description here

I guess coverage report command can not access/find .coverage file in the container.


So my question is What is the elegant way to run coverage in this situation?

like image 592
Amir Avatar asked Sep 24 '26 02:09

Amir


1 Answers

since const's answer has already made the first part easier i.e to get the coverage details, I have tried solve how to get reports?

This is given by Gitlab coverage doc. So your coverage job must be written like this

 coverage:
  stage: test
  script:
      - docker pull $CONTAINER_TEST_IMAGE
      - docker run $CONTAINER_TEST_IMAGE /bin/bash -c "python -m coverage run tests/tests.py && python -m coverage report -m"
  coverage: '/TOTAL.+ ([0-9]{1,3}%)/'

the regex was mentioned in mondwan blog

Addon

If you add the below line in your README.md file you will get a nice badge(in master README.md) that captures your coverage details.

[![coverage report](https://gitlaburl.com/group_name/project_name/badges/master/coverage.svg?job=unittest)](https://gitlaburl.com/group_name/project_name/commits/master)

coverage scrrenshot

like image 73
Ja8zyjits Avatar answered Sep 25 '26 20:09

Ja8zyjits



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!