Meaning
Represents a parametrized test case that is expected to fail (xfail), used to verify that invalid input is properly rejected.
Primary Function
Define a test case marked as expected to fail, providing specific input parameters and an identifier for test reporting.
Communicative Purpose
Indicate to developers and test runners that this test case is anticipated to fail, often used to document known invalid input handling or track outstanding bugs.
Pattern
pytest.param(<arg1>, <arg2>, id='<identifier>', marks=pytest.mark.xfail)
Core Structure
pytest.param(arg1, arg2, id='identifier', marks=pytest.mark.xfail)
Função primária
Define a test case marked as expected to fail, providing specific input parameters and an identifier for test reporting.
Propósito comunicativo
Indicate to developers and test runners that this test case is anticipated to fail, often used to document known invalid input handling or track outstanding bugs.
Situações de gatilho
When writing tests for validation or error‑handling logic, to record known failing cases, or to track bugs that are expected to fail until fixed.
Contextos
Inside pytest test functions marked with @pytest.mark.parametrize, within test modules that validate error conditions or invalid inputs.
Padrão
pytest.param(<arg1>, <arg2>, id='<identifier>', marks=pytest.mark.xfail)
Estrutura central
pytest.param(arg1, arg2, id='identifier', marks=pytest.mark.xfail)
Slots de substituição
arg1: any test input, arg2: any test input or None, id: string, marks: pytest mark (e.g., xfail)
Colocados típicos
- pytest.mark
- param
- xfail
- id
- parametrize
- test
- fixture
Substituições comuns
- arg1 and arg2 can be any test data (e.g.
- invalid input
- None
- empty string)
- id can be any descriptive string
- marks can be pytest.mark.skip or other marks.
Erros comuns
Forgetting to import pytest → NameError when pytest is not defined; Using wrong mark (e.g., skip instead of xfail) → test is skipped instead of expected failure; Omitting the id → test reports lack identifier, making reports harder to read; Providing incorrect number of arguments to test function → TypeError due to argument mismatch; Applying xfail when test actually passes → test reported as XPASS (unexpected pass) indicating a false expectation.
Similar / contraste
regular pytest.param without xfail: expected to pass; pytest.param marked with skip: test is omitted; @pytest.mark.xfail on test function: applies xfail to all cases in that test.
Interferências
Coming from Python's unittest: may expect skip instead of xfail → use pytest.mark.xfail for expected failures; Coming from JUnit: may use @Ignore instead of @Test(expected=…) → use pytest.mark.xfail.
Família do chunk
- pytest-param-xfail
Nuance
Do not use xfail for tests that are expected to pass, as it masks real failures; No measurable performance impact, but overuse can obscure test suite clarity; Boundary: xfail only marks the test as expected to fail; if the test passes unexpectedly, it is reported as XPASS, indicating a false expectation.
Efeito pragmático
Communicates to readers and test runners that this test case documents a known invalid input scenario, encouraging proper error handling and future fix tracking.
Dica de memória
Using pytest.param with xfail is like placing a 'wet floor' sign: it warns everyone that the area is known to be slippery, so they proceed cautiously without being surprised by a fall.
Nota
This example shows an invalid input 'invalid' paired with None, marked as expected to fail and labeled 'invalid_input'.
Upgrade path
Progress to using multiple xfail cases within a single parametrize, or apply @pytest.mark.xfail at the test function level for broader expectations.
Log in to save chunks.