Interview Prep/Build Tools (Java, Node.js, Python, .NET)/Python builds (pip, venv, requirements.txt)
How do you run Python tests and linting in a CI pipeline?
IntermediateHands-onPython builds (pip, venv, requirements.txt)
Answer
Short answer
Install the app plus dev dependencies, run a linter (ruff/flake8), optionally a type checker (mypy), then pytest with coverage and a JUnit XML report the CI tool can display. Cache pip downloads to keep it fast.
GitHub Actions example
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env: { POSTGRES_PASSWORD: test }
ports: ["5432:5432"]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with: { python-version: '3.12', cache: pip }
- run: pip install -r requirements.txt -r requirements-dev.txt
- run: ruff check .
- run: mypy src/
- run: pytest -q --cov=src --cov-report=xml --junitxml=junit.xml
env: { DATABASE_URL: postgresql://postgres:test@localhost:5432/postgres }
Tips
--cov-report=xmlproducescoverage.xmlthat SonarQube reads.- Use a real database as a service container for integration tests.
- Fail the build on lint errors so style issues never reach main.
What to say in the interview
"CI installs pinned app and dev requirements with pip caching, runs ruff and mypy, then pytest with coverage XML for SonarQube and JUnit XML for the test report. Integration tests use service containers like Postgres."
All Build Tools (Java, Node.js, Python, .NET) questions Roadmap: Python apps: pip, venv & requirements.txt
More on Python builds (pip, venv, requirements.txt)
How do you install dependencies and run a Python application?BeginnerWhat is a Python virtual environment and why do we need it?BeginnerWhat is requirements.txt and how do you pin versions?BeginnerHow do you run a Flask application in development and in production?BeginnerWhat is gunicorn and how many workers should you run?BeginnerWhat is the difference between WSGI and ASGI?Intermediate