Project · Beginner · about 10 hours
Put a safety net under legacy code
Write characterization tests for an untested order-pricing module using real order data, then refactor it without changing behaviour.
Skills:Git and GitHub, Python, Automated testing
Part of: Software Engineer (Backend)
Data and licences
- Olist Brazilian E-Commerce Public Dataset (Kaggle) ↗CC BY-NC-SA 4.0 (non-commercial use, share alike)
Brief
Most engineers spend more time changing existing code than writing new code. The safe way to change untested code is to first write tests that record what it does today, then refactor. Real data makes these tests realistic.
What to build
Write a single 150-line Python function that computes an order's total from Olist order items and payments: item prices, freight, instalment interest and a few special cases. Make it messy on purpose (long conditionals, repeated logic, magic numbers). Commit it.
Pick 500 real orders. Record the function's output for each as a snapshot, and write a test that compares the function with the snapshot.
Add unit tests for edge cases you discover: orders with no payments, many instalments, zero freight, rounding.
Refactor in small steps (extract function, rename, remove duplication), running the tests after each one and committing only on green.
Where you find behaviour that looks wrong, decide: fix it (and update the snapshot with a clear commit) or document it.
Deliverables
Repository with the original module, the tests, the refactored module and
NOTES.md
How it is graded
Reviewers read the commit history as a story and run the tests at several commits. Every commit should be green, and no behaviour should change without a commit that says so.
Stretch
Add property-based tests with Hypothesis (for example, totals are never negative and adding an item never lowers the total).
Deliverables
- A deliberately messy pricing module (provided in your own words from the brief) under version control
- Characterization tests that pin current behaviour on real Olist orders, plus unit tests for edge cases
- A refactored module with the same outputs, delivered as a series of small commits
- A short write-up of the bugs you found and whether you fixed or documented them
How a reviewer grades it
| Criterion | Weight | What good looks like |
|---|---|---|
| Characterization tests capture current behaviour before any change | 30% | |
| Refactoring happens in small, green steps with meaningful commit messages | 25% | |
| Edge cases are identified and tested (rounding, zero, missing values) | 25% | |
| Honest write-up that separates bugs from intended behaviour | 20% |