Add Django Coverage and Quality pipelines in Gitlab

Add Django Coverage and Quality pipelines in Gitlab

Introduction

If you have a Django project and you want to leverage Gitlab's pipelines to see your test cases running or see the code coverage percentage. This article will guide you step by step.

Code Coverage setup

For the code coverage you need to create a gitlab.yml file in your repo & add the following code:

coverage:
    stage: test
    only:
      - merge_requests
    services:
      - mysql:5.7
    before_script:
        - python3.7 -m pip install --upgrade pip
        - python3.7 -V
        - pip3.7 install -r requirements.txt
    script:
        - coverage run -m pytest
        - coverage run manage.py test

This is assuming that you are using MySQL service for the DB, if you use a different DB or don’t use one feel free to remove it.

With this you should be able to see the pipelines running & the logs below:

But wait, where is the coverage percentage?

For that, we need to enable the coverage report with coverage report -m

This would help you see the coverage of each file

Hmm… it is good but hard to read. Let’s generate the report in HTML, make it public & export it to GitLab's artifacts:

coverage:
    stage: test
    only:
      - merge_requests
    services:
      - mysql:5.7
    before_script:
          - python3.7 -m pip install --upgrade pip
        - python3.7 -V
        - pip3.7 install -r requirements.txt
    script:
        - coverage run -m pytest
        - coverage run manage.py test
        - coverage report -m
        - coverage html
    artifacts:
        paths:
            - htmlcov/
    when: always

pages:
    stage: coverage
    dependencies:
        - coverage
    script:
        - mv htmlcov/ public/
    artifacts:
        paths:
            - public/
    when: always
    only:
      - merge_requests

Please note that we are only running the pipelines when a merge request is raised or a change is made. Not after the MR is merged.

This helps us see the job artifacts with line by line covereage for each merge request.

So this is a lot of clicks to see the coverage, let’s get it parsed by cobertura (fun fact! It's Spanish for coverage) & into the GitLab UI. Let’s update the gitlab.yml, we are adding the xml report & cobertura specific report.

coverage:
    stage: test
    only:
      - merge_requests
    services:
      - mysql:5.7
    before_script:
          - python3.7 -m pip install --upgrade pip
        - python3.7 -V
        - pip3.7 install -r requirements.txt
    script:
        - coverage run -m pytest
        - coverage run manage.py test
        - coverage report -m
        - coverage html
        - coverage xml
    artifacts:
        paths:
            - htmlcov/
        reports:
          cobertura: coverage.xml
    when: always

pages:
    stage: coverage
    dependencies:
        - coverage
    script:
        - mv htmlcov/ public/
    artifacts:
        paths:
            - public/
    when: always
    only:
      - merge_requests

We need to then configure the Gitlab repo’s settings > CI/CD

https://gitlab.com/codebards/uplevel/-/settings/ci_cd

With this you should be able to see the coverage for each merge request:

References:

https://docs.gitlab.com/ee/ci/unit_test_reports.html

https://docs.gitlab.com/ee/user/project/merge_requests/test_coverage_visualization.html
https://stackoverflow.com/a/56675636/2084765

Code Quality

For adding a code quality pipeline in your repo add the following in the Gitlab.yml

code_quality:
   rules:
     - if: '$CODE_QUALITY_DISABLED'
       when: never
     - if: '$CI_PIPELINE_SOURCE == "merge_request_event"' # Run code quality job in merge request pipelines
     - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'      # Run code quality job in pipelines on the master branch (but not in other branch pipelines)
     - if: '$CI_COMMIT_TAG'                               # Run code quality job in pipelines for tags
   artifacts:
     paths: [gl-code-quality-report.json]

Create a codeclimate.yml file in your repo & add the following content

plugins:
  pep8:
    enabled: true

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