Skip to content
Streak:0Loading your progress…Sign in

Module 5 · Lesson 2 of 2

pytest & the modern toolchain

Tests as plain functions, fixtures, parametrize — and uv/ruff/pyproject. ~25 min

Lesson notes

pytest: plain asserts, fixtures by argument name, parametrize
import pytest
from app.pricing import apply_discount

@pytest.fixture
def cart():
    return {"items": [1000, 2500]}

@pytest.mark.parametrize("code,expected", [("TEN", 3150), ("NONE", 3500)])
def test_apply_discount(cart, code, expected):
    assert apply_discount(cart, code) == expected

def test_rejects_negative():
    with pytest.raises(ValueError, match="negative"):
        apply_discount({"items": [-1]}, "TEN")
A 2026 project setup
uv init my-service && cd my-service      # creates pyproject.toml + .python-version
uv add fastapi pydantic httpx              # runtime deps -> uv.lock
uv add --dev pytest ruff mypy              # dev deps
uv run ruff check . && uv run ruff format --check .
uv run mypy src && uv run pytest

Mapping to what you know: pyproject.toml ≈ package.json/pubspec.yaml, uv.lock ≈ package-lock.json, uv sync ≈ npm ci, ruff ≈ ESLint + Prettier, mypy/pyright ≈ tsc. Virtual environments (.venv) isolate dependencies per project — like node_modules, but for the interpreter too.

Practice

  1. 1. Write the tests yourself

    medium25 XP

    slugify(title) is given. Write run_tests() that asserts at least: lowercase, spaces→hyphens, punctuation removed, repeated separators collapsed, and leading/trailing hyphens stripped. The grader runs your tests against the correct implementation and against 3 buggy mutants — your tests must pass on the real one and fail on every mutant.

    Ctrl/⌘ + Enter runs the tests · Esc then Tab leaves the editor

  2. 2. Reproducible installs

    Quizeasy5 XP
    In CI, which command gives you exactly the locked dependency versions for a uv project?