Meaning
Decorates a test function to run it multiple times with different argument combinations, where each case is wrapped in pytest.param() with a custom id for clear identification in test reports. It solves the problem of undifferentiated parametrized test names that make it hard to pinpoint which specific input caused a failure. You reach for this when you need named, traceable test cases rather than opaque numeric indices.
Primary Function
Test parametrization
Communicative Purpose
Enables running a single test function against multiple named input-output pairs so each case is individually identifiable in CI reports and failure diagnostics.
Pattern
@pytest.mark.parametrize(argnames, [pytest.param(argval1, argval2, id=test_id)])
Core Structure
@pytest.mark.parametrize(..., [pytest.param(..., id=...)])
Função primária
Test parametrization
Propósito comunicativo
Enables running a single test function against multiple named input-output pairs so each case is individually identifiable in CI reports and failure diagnostics.
Situações de gatilho
Unit testing: verifying a pure function against several input-output pairs with descriptive names. CI pipelines: needing human-readable test node names to quickly locate which parametrized case failed. Regression testing: adding a specifically-named edge case to an existing parametrized test suite.
Contextos
pytest test suites, Python unit testing, CI/CD test reporting, TDD workflows
Padrão
@pytest.mark.parametrize(argnames, [pytest.param(argval1, argval2, id=test_id)])
Estrutura central
@pytest.mark.parametrize(..., [pytest.param(..., id=...)])
Slots de substituição
argnames: comma-separated string of parameter names, argval1: first test input value, argval2: second test input value (add more positional args for more parameters), test_id: string identifier for the individual test case
Colocados típicos
- pytest.fixture
- pytest.raises
- pytest.approx
- assert
- pytest.mark.xfail
Substituições comuns
- Plain tuple list without pytest.param: simpler but no custom IDs — failure output shows opaque indices. ids= keyword on parametrize: assigns names to all cases at once but couples name list to value list ordering. hypothesis.strategies: property-based alternative generating many cases automatically without explicit enumeration.
Erros comuns
1. Mismatching number of values in pytest.param with argnames count: causes ValueError at collection time. 2. Forgetting the list brackets around pytest.param calls: decorator expects an iterable, causes TypeError. 3. Duplicate id strings across cases: pytest silently overwrites earlier cases, only the last duplicate runs. 4. Passing id as a positional argument instead of keyword: shifts value mapping and causes confusing test failures.
Similar / contraste
pytest.mark.parametrize with plain tuples: no per-case ID control. unittest.TestCase.subTest: stdlib alternative where subtests do not produce independent test nodes. pytest.mark.parametrize with indirect=True: delegates argument resolution to fixtures rather than injecting raw values.
Interferências
Coming from unittest: may try to use subTest for parametrization — pytest.param creates fully independent test nodes with separate pass/fail status, unlike subTest which shares a single node. Coming from JUnit: may expect @ParameterizedTest method naming — pytest uses the id string directly in the node name.
Família do chunk
- pytest.mark.parametrize
- pytest.param
- pytest.mark.fixture
- pytest.mark.xfail
- pytest.mark.skip
- pytest.raises
Nuance
1. When NOT to use: single-case tests, quick ad-hoc scripts, or when test IDs add no diagnostic value. 2. Each pytest.param creates a separate test item at collection time; very large param sets (thousands) noticeably slow collection. 3. pytest.param also accepts a marks keyword for per-case markers like skip or xfail, which plain tuples cannot do.
Efeito pragmático
Produces individually named test cases in CI reports, making it trivial to identify which specific input combination caused a failure without digging through assertion output or log files.
Dica de memória
Like giving each guest at a dinner party a name tag — pytest.param lets you label every test case so you know exactly who misbehaved when something goes wrong.
Nota
pytest.param() also accepts a marks keyword argument to apply markers such as pytest.mark.skip or pytest.mark.xfail to individual cases without affecting the rest of the parametrized set.
Upgrade path
pytest.param with marks= keyword for per-case skip/xfail, then hypothesis for property-based testing with automatic case generation
Log in to save chunks.