Structure unit tests using pytest's parameterize decorator instead of manual loops or wrapper functions. This approach improves test readability, makes failure cases easier to identify, and allows individual test cases to be run separately.
Structure unit tests using pytest’s parameterize decorator instead of manual loops or wrapper functions. This approach improves test readability, makes failure cases easier to identify, and allows individual test cases to be run separately.
When implementing tests with multiple inputs or configurations:
@pytest.mark.parametrize()
decoratorsExample refactoring:
# Before - manual iteration
def test_operation():
test_cases = [
(5, 10, True),
(3, 7, False),
(0, 1, True)
]
for a, b, expected in test_cases:
result = operation(a, b)
assert result == expected
# After - pytest parameterization
@pytest.mark.parametrize('a,b,expected', [
(5, 10, True),
(3, 7, False),
(0, 1, True)
])
def test_operation(a, b, expected):
result = operation(a, b)
assert result == expected
This approach makes tests more maintainable, documents test cases more clearly, and provides better reporting when tests fail by clearly identifying which specific parameter combination caused the failure.
Enter the URL of a public GitHub repository