QuickDevGuide

Home › Python

Testing with pytest — Python

pytest is the de facto Python test runner: plain assert statements, automatic test discovery (test.py, test functions) and powerful fixtures. Essentials Feature Usage Assertions assert result == 42 — pytest shows a detailed diff Expected errors with pytest.raises(ValueError, match="positive"): Parametrize @pytest.mark.parametrize("a,b,expected", [...]) Fixtures @pytest.fixture — reusable set-up injected by argument name Built-in fixtures tmppath, monkeypatch, capsys Mocking unittest.mock.patch / pytest-mock Coverage pytest --cov=myapp (pytest-cov) Commands pytest · pytest -k "discount" (filter) · pytest -x (stop at first failure) · pytest -vv (verbose) · pytest --lf (re-run last failures).

Key Takeaways