pytest runs Python unit tests as an automated quality gate in a production CI pipeline. A failing test stops image building and deployment updates, preventing unvalidated changes from progressing. section 3.4.1
A practical GitLab CI integration:
- Install application dependencies from requirements.txt in a Python 3.10 job. section 3.4.1
- Run pytest against the project directory to execute the application’s unit tests. section 3.4.1
- Allow later build and Helm-update stages to proceed only after the test job succeeds. section 3.4.1
CI test job
test_code:\n stage: test\n image:\n name: python:3.10\n script:\n - pip install -r $CI_PROJECT_DIR/requirements.txt\n - pytest $CI_PROJECT_DIR/ section 3.4.1
Listing 3.13 GitLab CI example
image: docker:20.10.16
variables:
DOCKER_TLS_CERTDIR: "/certs"
stages: #1
- test
- build
- update
services: #2
- docker:20.10.16-dind #3
test_code: #4
stage: test
image:
name: python:3.10
script:
- echo "testing code"
- pip install -r $CI_PROJECT_DIR/requirements.txt
- pytest $CI_PROJECT_DIR/
build_image: #5
stage: build
script:
- echo "building docker image"
- cd $CI_PROJECT_DIR
- echo ${CI_COMMIT_SHA:0:8}
- docker build . -t varunmallya/hello-joker:${CI_COMMIT_SHA:0:8}
- docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}
- docker push varunmallya/hello-joker:${CI_COMMIT_SHA:0:8}
update_helm_chart: #6
stage: update
image:
name: python:3.10
script:
- apt-get update && apt-get install git
- git clone
https://${GITLAB_USER_NAME}:${GITLAB_ACCESS_TOKEN}@\
gitlab.com/learn-mlops/gitlab-ci-example.git #7
- cd $CI_PROJECT_DIR
- wget
https://github.com/mikefarah/yq/releases/download/v4.2.0/\
yq_linux_amd64 -O /usr/bin/yq && chmod +x /usr/bin/yq #8
- git config --global user.email "${GITLAB_USER_EMAIL}"
- git config --global user.name "${GITLAB_USER_NAME}"
- yq e -i ".image.tag |= "${CI_COMMIT_SHA:0:8}"" \
hello-joker/values.yaml #9
- git add hello-joker/values.yaml
- git commit -m "[skip ci]Update helm chart" #10
- git push https://${GITLAB_USER_NAME}:\
${GITLAB_ACCESS_TOKEN}@gitlab.com/learn-mlops/\
gitlab-ci-example.git HEAD:main #11
Inspect the complete GitLab CI pipeline showing pytest alongside Docker image building and Helm chart updates. section 3.4.1










