liveBook · Manning

Pytest

As explained in Machine Learning Platform Engineering

pytest is a Python testing framework for writing and running automated tests.

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

How Machine Learning Platform Engineering covers it

How deep it goes
Shows executable CI configuration and failure behavior, but does not detail test design or pytest internals.
Examples
Uses test_main.py, requirements.txt, and a pytest command targeting the project directory.
What it assumes
Assumes a Python application with dependencies and unit tests organized in the project directory.
What you can do after
Apply pytest through GitLab CI to validate code and block downstream delivery stages when tests fail.
How it connects
Connects unit testing with GitLab CI, Docker image building, and Helm chart updates.

Also explained in

Compare how 11 books explain pytest →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning Platform Engineering. The section links point to the parts of the book it draws on.