test: parametrize two clean duplicated-test clusters#337
Merged
Conversation
Two clusters of near-identical tests that differ only by data collapse cleanly into one `@pytest.mark.parametrize`d test each — a genuine LOC reduction with every case preserved as a row carrying a readable id: - waterdata_utils_test: `_format_api_dates` — 12 one-line equality tests → 1. - waterdata_test: `TestNormalizeStrIterable` iterable-normalizes — 4 → 1 (same assertion, vary the container type: list / tuple / Series / ndarray). Deliberately NOT parametrized (reconsidered for readability): `_parse_retry_after` and the `_build_filter` / reject-type clusters. Those either encode distinct behaviors with explanatory docstrings, or didn't actually reduce LOC (long multi-line param tuples), so named tests read better there.
2d24b8b to
190bef3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Scope (revised after a readability pass)
Parametrization trades named tests for a data table, so it's only a win when it
reduces LOC and the cases are pure data-variations of one assertion with
no per-case rationale lost. Two clusters clear that bar:
_format_api_dates(waterdata_utils_test.py) — 12 one-line equality tests→ 1 (13 rows, explicit ids). Big, clean reduction.
TestNormalizeStrIterableiterable-normalizes (waterdata_test.py) — 4 → 1;same assertion, only the container type varies (list / tuple / Series / ndarray).
Net −16 lines, every case preserved, readable ids
(
test_format_api_dates[offset_to_utc],…[ndarray]). 89 tests pass; ruff/mypy clean.Deliberately NOT parametrized
After a second look I dropped three clusters I'd initially collapsed — they made
the tests harder to read:
_parse_retry_after— these were 4 distinct documented behaviors (Nonefallback, delta-seconds, clamp-negative, unparseable), each with a docstring
explaining the why. Flattening to bare tuples deleted that rationale.
_build_filter— the long CQL expected-strings forced multi-line tuples, soit didn't reduce LOC and read worse than 5 named one-line tests.
test names already documented each case.
Conventions used / proposed
ids(orpytest.param(..., id=...)for shortdata); keep the body to a single assertion.
🤖 Generated with Claude Code