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=xml produces coverage.xml that 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)