After this lesson, you will be able to: Test Python code with pytest, share setup with fixtures, cover many cases with parametrize, and mock dependencies with unittest.mock.
Python is the other language LastWrite students ship, so testing it well matters. pytest is the de facto standard. This lesson covers the runner, fixtures, parametrization, and mocking, mapping each idea back to what you just learned in JavaScript.
pytest discovers test_*.py files and test_* functions; assertions use plain assert. Fixtures provide reusable setup.
# discount.pydef apply_discount(price, code):if code == 'HALF':return price / 2if code == 'TENOFF':return max(0, price - 10)return price# test_discount.pyimport pytestfrom discount import apply_discountdef test_halves_for_half():assert apply_discount(40, 'HALF') == 20def test_never_below_zero():assert apply_discount(5, 'TENOFF') == 0# A fixture: reusable setup injected by name@pytest.fixturedef sample_cart():return [{'id': 'a', 'price': 10}, {'id': 'b', 'price': 5}]def test_cart_total(sample_cart):assert sum(item['price'] for item in sample_cart) == 15# Install: pip install pytest pytest-cov# Run: pytest Coverage: pytest --cov
parametrize runs the same test body across a table of inputs and expected outputs. It replaces copy-pasted near-identical tests.
import pytestfrom discount import apply_discount@pytest.mark.parametrize('price, code, expected', [(40, 'HALF', 20),(5, 'TENOFF', 0),(40, 'NOPE', 40),(100, 'TENOFF', 90),])def test_apply_discount(price, code, expected):assert apply_discount(price, code) == expected# Each row is reported as its own test, so a failure tells you exactly# which input broke.
Patch a dependency at its boundary so the test stays offline and deterministic. This is the Python equivalent of vi.mock.
from unittest.mock import patch, MagicMockfrom weather import get_weather_summary# Patch where the name is USED, not where it's defined.@patch('weather.requests.get')def test_summary(mock_get):mock_get.return_value = MagicMock(status_code=200,json=lambda: {'tempC': 21, 'condition': 'Clear'},)assert get_weather_summary('London') == 'London: 21C, Clear'mock_get.assert_called_once()
Pick the best answer.
Sign in and purchase access to unlock this lesson.