Skip to content
Sample project for setting-up Gitlab CI/CD with pytest and pytest-cov for Python-based applications
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Gitlab CI/CD Example for Python-based Apps

pipeline status coverage report



  • The main requirements are:
    • pytest for running unit tests
    • pytest-cov for checking code coverage
    • Access to a Gitlab instance
    • Access to a build/test/server PC for gitlab-runner
  • For other example-specific dependencies, see requirements.txt.


  • Configure app installation in
  • Configure test configuration in pytest.ini
  • Setup the Local Test Environment
    • Using virtualenv
      • Activate a virtual Environment
      • Install dependencies
        pip3 install -r requirements.txt
      • Install app in editable mode
        pip3 install -e .
    • Using Docker
      • Create a Docker image from the Dockerfile
        docker build --tag sample-ci-python .
        docker run -it sample-ci-python:latest /bin/bash
  • Setup Gitlab CI


  • Run the Tests on Local
    • From the virtual environment or from the Docker image
      pytest tests
      tests/provider_tests/...test_generate_integers_length PASSED            [1/3]
      tests/provider_tests/...test_generate_integers_start_and_stop PASSED    [2/3]
      tests/provider_tests/...test_generate_integers_step PASSED              [3/3]
  • Run the Tests on Gitlab
    • Make changes in src/app/ and in tests/
    • Make changes to the .gitlab-ci.yml configuration (if necessary)
    • Commit the changes then push to Gitlab
    • Go to the Gitlab project > CI/CD > Pipelines
    • Select the currently running job to view progress/result
    • It is possible to download the job log by clicking on the Raw button


  • pytest uses cached codes instead of latest
  • "This job is stuck, because you don’t have any active runners that can run this job."
    • Make sure that the .gitlab-ci.yml has the correct tags
    • Make sure the gitlab-runner service is running
    • Make sure the machine running gitlab-runner is accessible by the Gitlab instance
  • "yaml invalid"
    • Go to the Gitlab project > CI/CD
    • On the top-right portion, click the CI Lint button
    • Paste the contents of gitlab-ci.yml file and validate
  • The jobs are not running on the same runner/environment
  • The gitlab-runner is leaving a lot of -cache- containers/volumes
    • See a discussion of this behavior here
    • Possible solutions:
      • Manually regularly run docker system prune
      • Setup a cron job docker system prune -f
        # Cleanup docker containers/volumes every 3am every monday
        0 3 * * 1 /usr/bin/docker system prune -f


You can’t perform that action at this time.