# pytest — The Python Testing Framework That Scales > pytest makes it easy to write small tests, yet scales to support complex functional testing. Fixtures, parameterization, plugins, markers, and a rich assertion introspection system. The de facto testing standard for the Python ecosystem. ## Install Save the content below to `.claude/skills/` or append to your `CLAUDE.md`: ## Quick Use ```bash pip install pytest ``` ```python # test_calc.py def add(a, b): return a + b def test_add(): assert add(1, 2) == 3 def test_add_negative(): assert add(-1, -2) == -3 def test_add_strings(): assert add("hello ", "world") == "hello world" ``` ```bash pytest # Auto-discover and run tests pytest -v # Verbose pytest -x # Stop on first failure pytest -k "negative" # Filter by name pattern pytest --cov=mypackage # Coverage (needs pytest-cov) pytest -n auto # Parallel (needs pytest-xdist) ``` Fixtures: ```python import pytest @pytest.fixture def db_session(): session = create_session() yield session session.rollback() session.close() def test_create_user(db_session): user = User(name="William") db_session.add(user) db_session.flush() assert user.id is not None @pytest.mark.parametrize("a,b,expected", [(1,2,3), (0,0,0), (-1,1,0)]) def test_add_parametrized(a, b, expected): assert add(a, b) == expected ``` ## Intro pytest is the de facto testing framework for Python. Simple enough for small tests, powerful enough for complex functional testing. Features include fixtures (dependency injection), parametrization, markers, plugins, and an incredibly helpful assertion introspection system that shows you exactly what failed and why. - **Repo**: https://github.com/pytest-dev/pytest - **Stars**: 13K+ - **Language**: Python - **License**: MIT ## What pytest Does - **Auto-discovery** — finds `test_*.py` files and `test_*` functions - **Assertion introspection** — plain `assert` with detailed failure diffs - **Fixtures** — DI for test setup/teardown (function, class, module, session scope) - **Parametrize** — run same test with multiple inputs - **Markers** — `@pytest.mark.slow`, `@pytest.mark.skip`, custom - **Plugins** — 1200+ on PyPI (pytest-cov, pytest-xdist, pytest-mock, pytest-asyncio) - **Conftest** — shared fixtures across test directories - **Temporary directories** — `tmp_path` fixture - **Capture** — stdout/stderr capture per test - **Parallel** — via pytest-xdist ## Comparison | Framework | Style | Fixtures | Plugins | |---|---|---|---| | pytest | Function-based | DI (fixtures) | 1200+ | | unittest | Class-based | setUp/tearDown | Limited | | nose2 | Mixed | Limited | Some | | doctest | Inline | None | None | ## FAQ **Q: pytest vs unittest?** A: pytest is more concise (no class needed), with better assertions (no assertEqual) and a stronger fixture DI. The vast majority of new Python projects use pytest. **Q: How to test async code?** A: `pip install pytest-asyncio`, then use `@pytest.mark.asyncio async def test_xxx()` to await async functions. **Q: Fixture scope?** A: function (once per test), class, module, session (once for the entire test run). Larger scope means lower setup overhead but weaker isolation. ## Sources - Docs: https://docs.pytest.org - GitHub: https://github.com/pytest-dev/pytest - License: MIT --- Source: https://tokrepo.com/en/workflows/pytest-python-testing-framework-scales-42405aa1 Author: AI Open Source