A unit test is an automated check that exercises a single small piece of code, such as one function or class, in isolation and compares the actual result to the expected one. It passes or fails without human judgment.
Why it matters
Unit tests run in milliseconds, so you can run thousands on every commit through CI/CD. They catch regressions, meaning old behavior that breaks after a change, and they make refactoring safer because failing tests point directly at what broke. Tests also document intended behavior.
Example
def add_gst(price, rate=0.18):
return round(price * (1 + rate), 2)
def test_add_gst():
assert add_gst(100) == 118.0
assert add_gst(0) == 0.0
Running pytest executes test_add_gst and reports pass or fail. If someone changes the rounding, the test fails immediately.
Common mistakes
- Testing implementation details instead of behavior, so harmless refactors break tests.
- Depending on the network, a database or the clock, which makes tests slow and flaky.
- Treating high coverage as proof of quality; a test with no meaningful assertion still counts as covered.
Related
Get notified when this term updates
Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.
This page is general information written by our editorial team and updated when documentation changes. Servers and providers differ, so test changes safely first. See our editorial policy and disclaimer.