Skip to content
Sarj Code Standards

pytest-test-size-budget

Keep collected pytest tests within 15 statements, without branches or verifying loops.

Why

A test should read as one arrangement, one action and its observed outcome. Branches and loops make the test choose which statements run, so a case can silently assert nothing, and a long body usually checks several behaviors at once, so a failure no longer names the broken one (xUnit Test Patterns: Conditional Test Logic and Eager Test; Google's Software Engineering at Google: Don't Put Logic in Tests).

Fix

Split each branch or behavior into its own test, or use pytest.mark.parametrize for a table of inputs. Replace try/except with pytest.raises, collect values with a comprehension, and move repeated arrangement or polling into a fixture, factory or tested helper. Do not merge statements onto one line or hide them in an untested helper only to lower the count.

Examples

Before — flagged Split or parametrize instead of branching
tests/test_guard.py
import pytest
@pytest.mark.parametrize("allowed", [True, False])
def test_guard(allowed):
if allowed:
assert check("ok")
else:
with pytest.raises(ValueError):
check("bad")
After — preferred One straight-line test per outcome
tests/test_guard.py
import pytest
def test_guard_accepts_ok():
assert check("ok")
def test_guard_rejects_bad():
with pytest.raises(ValueError):
check("bad")