Write tests for core transformations and edge cases before extending a script. pytest makes this practical with simple test discovery, readable assertions, and fixtures that keep test data setup consistent.
- Test pure functions first because they are easiest to verify.
- Use fixtures to share setup without duplicating test code.
- Turn bug reports into regression tests before fixing logic.
Create useful tests for data workflows
Focus on deterministic functions such as parse_amount, normalize_channel, and aggregate_weekly_kpis. For each function, test normal inputs, boundary values, and invalid input handling.
Readable assertion messages help isolate failures quickly, especially when comparing DataFrame outputs where shape and column names both matter.
Debug failing cases systematically
When a test fails, reproduce with the smallest input that still fails. Inspect intermediate values and fix the root cause instead of updating expected outputs to match wrong behavior.
- Use pytest -k to run only related tests during iteration.
- Add temporary logging around transformation boundaries, then remove noise after resolution.
- Keep tests independent so one failure does not hide others.
Add a regression test for malformed amount strings
A production bug showed amount values like 1,299.00 causing parse failures in one data source.
- Write a failing pytest test for parse_amount with input '1,299.00'.
- Update parsing logic to strip separators safely before conversion.
- Rerun the focused test and full test file to confirm no regressions.
Common mistakes
- Testing only happy paths and missing malformed data cases.
- Sharing mutable global test state across test functions.
- Ignoring flaky tests instead of removing nondeterministic dependencies.
- Fixing expected outputs to match broken logic without root-cause analysis.
Try one
Why should a real bug be converted into a test case before or with the fix?
It creates a regression guard so the same failure is caught automatically in future changes.
Sources
- Python documentationOfficial reference for Python syntax, standard library modules, and tutorials.
- pandas documentationOfficial reference for pandas DataFrame operations, indexing, and analysis APIs.
- pytest documentationOfficial guide for writing tests, fixtures, and test discovery with pytest.