Skip to content

Python · testing

test-loops-over-literal-cases

python:test-loops-over-literal-cases

Test loops over a literal case table — use `@pytest.mark.parametrize` so cases report separately.

Code
SARJ041
Default
error
Fix
none
Languages
python

Why

A loop collapses all cases into one test and stops at the first failing iteration.

Fix

Move the literal table into `@pytest.mark.parametrize` so every case has its own result.

Before / after

Executed by this rule’s unit tests.

Before

Cases hidden inside one test

tests/test_normalize.py · focus
def test_normalize():
    for value in ["a", "b"]:
        assert normalize(value) == value

After

Each case is a separate test

tests/test_normalize.py · focus
import pytest

@pytest.mark.parametrize("value", ["a", "b"])
def test_normalize(value):
    assert normalize(value) == value

Limits

  • Only literal iterables with at least two cases inside test functions are analyzed.
  • Loops using unittest or pytest subtest contexts are allowed.