{
    "slug": "py_testing_pytest",
    "term": "Testing with pytest",
    "category": "python",
    "difficulty": "intermediate",
    "short": "pytest is Python's dominant testing framework — its fixture system, parametrize decorator, and plugin ecosystem make it more powerful and less verbose than unittest.",
    "long": "pytest discovers tests by naming convention (test_*.py, *_test.py), uses plain assert statements with introspective failure messages, and provides fixtures via dependency injection (function parameters). Key features: @pytest.fixture for reusable setup/teardown, @pytest.mark.parametrize for data-driven tests, conftest.py for shared fixtures, and marks for grouping tests. The plugin ecosystem (pytest-cov, pytest-mock, pytest-asyncio) covers virtually every testing need.",
    "aliases": [
        "pytest",
        "Python testing",
        "conftest"
    ],
    "tags": [
        "python",
        "testing",
        "pytest"
    ],
    "misconception": "pytest fixtures are complicated — the dependency injection pattern (fixture as function parameter) is simpler than unittest setUp/tearDown once understood.",
    "why_it_matters": "pytest's failure messages show the actual vs expected values and the expression that failed — far more actionable than unittest's 'AssertionError: False is not True'.",
    "common_mistakes": [
        "Not using fixtures for shared setup — copy-pasting setup code across tests instead of extracting a fixture.",
        "Fixtures with too wide a scope — session-scoped fixtures with mutable state bleed between tests.",
        "Not using parametrize for data-driven tests — writing the same test five times with different inputs.",
        "Not using conftest.py for shared fixtures — defining fixtures in every test file instead of once."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "py_error_handling",
        "test_driven_development",
        "test_fixture_setup",
        "mutation_testing"
    ],
    "prerequisites": [
        "unit_testing",
        "py_type_hints",
        "py_error_handling"
    ],
    "refs": [
        "https://docs.pytest.org/en/stable/"
    ],
    "bad_code": "# unittest style — verbose:\nimport unittest\n\nclass TestUserService(unittest.TestCase):\n    def setUp(self):\n        self.db = create_test_db()\n        self.service = UserService(self.db)\n\n    def test_create_user_success(self):\n        result = self.service.create({'email': 'a@b.com', 'name': 'Alice'})\n        self.assertEqual(result['email'], 'a@b.com')  # Less informative failure",
    "good_code": "# pytest style — concise, with fixtures:\nimport pytest\n\n@pytest.fixture\ndef user_service():\n    db = create_test_db()\n    yield UserService(db)\n    db.rollback()  # Teardown\n\n@pytest.mark.parametrize('email,name', [\n    ('alice@example.com', 'Alice'),\n    ('bob@example.com', 'Bob'),\n])\ndef test_create_user(user_service, email, name):\n    result = user_service.create({'email': email, 'name': name})\n    assert result['email'] == email  # pytest shows actual vs expected on failure",
    "quick_fix": "Use pytest fixtures for setup/teardown and parametrize for data-driven tests — it's simpler than unittest with less boilerplate; install pytest-cov for coverage and pytest-mock for mocking",
    "severity": "medium",
    "effort": "medium",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/py_testing_pytest",
        "html_url": "https://codeclaritylab.com/glossary/py_testing_pytest",
        "json_url": "https://codeclaritylab.com/glossary/py_testing_pytest.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Testing with pytest](https://codeclaritylab.com/glossary/py_testing_pytest) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/py_testing_pytest"
            }
        }
    }
}