PY-TEST0011 · conditional_test_branch_count
Limit conditional branches owned by one collected pytest test.
This is a deterministic rule for python. Read its implementation.
Definition
Section titled “Definition”Count if statements, conditional expressions, match statements, and comprehension filters
owned directly by each test collected through pytest’s default Python conventions. Nested
functions and classes start independent scopes. Return the branch count for this test. A
separate policy can compare the value with a project ceiling such as two.
Evidence
Section titled “Evidence”Evidence records the complete test range and measured branch count. The metric is syntax based and reproducible. It does not ask a model whether a branch is readable. The value is the largest branch count any one collected test owns.
Exceptions
Section titled “Exceptions”Assertions, exception contexts, and boolean expressions are not control-flow branches under this rule. Parametrize input variants instead of selecting expected behavior inside a test. Two branches can leave room for bounded invariant checks. A deliberately algorithmic test can use a different policy or be omitted by provider selection. The production function conditional rule remains separate.
Examples
Section titled “Examples”def test_status(client, authenticated): response = client.get("/account") if authenticated: assert response.status_code == 200 else: assert response.status_code == 401@pytest.mark.parametrize(("authenticated", "status"), [(True, 200), (False, 401)])def test_status(client, authenticated, status): assert client.get("/account").status_code == statusReferences
Section titled “References”- Cites “pytest documentation”, parametrization. Open reference
- Cites “pytest documentation”, anatomy of a test. Open reference
- Cites “pytest documentation”, default collection conventions. Open reference