What if your code could check itself every time you change it?
Why Unit testing in Testing Fundamentals? - Purpose & Use Cases
Imagine you just wrote a small piece of code to calculate discounts for a shopping app. Every time you change something, you open the app and try different prices manually to see if the discount works correctly.
This manual checking is slow and tiring. You might forget some price cases or make mistakes while testing. If the code changes often, you waste a lot of time repeating the same checks, and bugs can sneak in unnoticed.
Unit testing lets you write small automatic checks for each part of your code. These tests run quickly and tell you immediately if something breaks, so you don't have to test everything by hand every time.
Run app, enter price 100, check discount manually Run app, enter price 200, check discount manually
assert calculate_discount(100) == 10 assert calculate_discount(200) == 20
Unit testing makes your code safer and saves time by catching errors early with automatic checks.
A developer changes a function to add a new feature. Thanks to unit tests, they quickly see if the old discount calculations still work without opening the app and clicking around.
Manual testing is slow and error-prone.
Unit tests automate checks for small code parts.
They help catch bugs early and speed up development.