Write maintainable, well-structured tests that are easy to understand and extend. Tests should remain simple and focused on their specific validation purpose.
Write maintainable, well-structured tests that are easy to understand and extend. Tests should remain simple and focused on their specific validation purpose.
Three key practices to follow:
# Instead of multiple similar test functions:
def test_unique_counts_on_bool_only_true() -> None:
s = pl.Series("bool", [True, True, True])
expected = pl.Series("bool", [3], dtype=pl.UInt32)
assert_series_equal(s.unique_counts(), expected)
def test_unique_counts_on_bool_only_false() -> None:
s = pl.Series("bool", [False, False, False])
expected = pl.Series("bool", [3], dtype=pl.UInt32)
assert_series_equal(s.unique_counts(), expected)
# Prefer a single parametrized test:
@pytest.mark.parametrize(
("input", "expected"),
[
([True, True, True], [3]),
([False, False, False], [3]),
([True, False, False, True, True], [3, 2]),
]
)
def test_unique_counts_bool(input: list[bool], expected: list[int]):
assert_series_equal(
pl.Series("bool", input).unique_counts(),
pl.Series("bool", expected, dtype=pl.UInt32)
)
Avoid complex logic in test cases. Each test should be straightforward and focused on specific validation. If a test contains intricate logic, consider refactoring into multiple simpler tests.
Use appropriate test fixtures and environments:
tmp_path
fixture for file operations instead of hardcoded pathsio.BytesIO()
over disk operations when possible for better performanceEnter the URL of a public GitHub repository