# 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:

```yaml
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:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682981368684/e2041ea9-1813-4c32-ad7c-a55d9c694181.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682981460243/5976011b-86ce-410f-b955-7ec00d4bf1c4.png align="center")

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

```yaml
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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682981583452/e8d0bb51-b957-41ac-ae24-439172577e87.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682981607155/5416ae1a-9b6e-4dba-80d1-fbbca3602d7a.png align="center")

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.

```yaml
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 &gt;  CI/CD

[https://gitlab.com/codebards/uplevel/-/settings/ci\_cd](https://gitlab.com/codebards/uplevel/-/settings/ci_cd)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682981709756/13f73648-96fc-4049-b553-1702cb29208a.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682981754449/05e3496a-3a17-4ab9-9677-4d57adae4d29.png align="center")

References:

[https://docs.gitlab.com/ee/ci/unit\_test\_reports.html](https://docs.gitlab.com/ee/ci/unit_test_reports.html)

[https://docs.gitlab.com/ee/user/project/merge\_requests/test\_coverage\_visualization.html](https://docs.gitlab.com/ee/user/project/merge_requests/test_coverage_visualization.html)  
[https://stackoverflow.com/a/56675636/2084765](https://stackoverflow.com/a/56675636/2084765)

### Code Quality

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

```yaml
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

```yaml
plugins:
  pep8:
    enabled: true
```

Reference: [https://docs.gitlab.com/ee/user/project/merge\_requests/code\_quality.html](https://docs.gitlab.com/ee/user/project/merge_requests/code_quality.html)
