Search code examples
dockercontinuous-integrationgitlabcode-coveragegitlab-ci

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?


Solution

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

    Yes, your assumption is correct. By running:

    - docker run $CONTAINER_TEST_IMAGE python -m coverage run tests/tests.py
    - docker run $CONTAINER_TEST_IMAGE python -m coverage report -m
    

    you actually start two completely separate containers one after the another.

    In order to extract coverage report you will have to run coverage report command after the coverage run command is finished in the same container like so (I'm assuming bash shell here):

    -  docker run $CONTAINER_TEST_IMAGE /bin/bash -c "python -m coverage run tests/tests.py && python -m coverage report -m"