Unit testing
Also known as: unit tests, unit test
Unit testing is the practice of writing small automated tests that check individual pieces of code, such as a function that calculates tax, to confirm they produce the right results.
Tests run in seconds, often every time code changes, catching bugs before they reach users. While unit tests are the foundation of software quality, they only check pieces in isolation, so integration and end-to-end tests are still needed to confirm everything works together.
Unit testing, explained at three levels
At home
Unit testing is like checking each ingredient before baking a cake. If the eggs are bad, you find out before ruining the whole cake.
At work
Unit tests catch bugs early, when they're quickest and cheapest to fix.
At home
A unit test might check that a function adding sales tax turns $100 into $113 in Ontario. If someone breaks the function later, the test fails.
At work
Popular frameworks include pytest, JUnit, Jest, and xUnit. Tests run in CI pipelines, and code coverage shows how much code they exercise.
At home
Writing tests feels slow at first but saves time on any project you return to. In my opinion, even a few tests for tricky parts are worth it.
At work
Focus tests on behaviour, not implementation details, keep them fast and independent, fix flaky tests promptly, and review AI-generated tests carefully, since they can pass without checking anything meaningful.
Real-world examples
At home
Fatima wrote a budgeting script with unit tests. When she changed how it rounded numbers, a failing test caught a bug immediately.
At work
Thomas leads a Gatineau payroll software team. Unit tests for every tax calculation catch errors before each payroll update ships.
How it compares
| feature | Unit testing | Integration testing |
|---|---|---|
| Scope | Single function or component | Several parts working together |
| Speed | Very fast | Slower |
| Finds | Logic bugs | Connection and data problems |
| Quantity | Many | Fewer |
Unit tests check pieces. Integration tests check how pieces fit.
For analysts
I'd turn business rules from requirements directly into unit test cases. It proves calculations like tax or discounts match what the business actually specified.
History & origin
Programmers tested code from the earliest days, but automated unit testing frameworks took off with Kent Beck's SUnit for the Smalltalk language around 1994. Kent Beck and Erich Gamma created JUnit for Java in 1997, which inspired similar tools for nearly every language. Extreme Programming and test-driven development, promoted by Beck around 2000 and in a 2002 book, encouraged writing tests before code. Continuous integration tools then ran tests automatically on every change. In the 2020s, AI coding assistants began generating unit tests, though their quality varies. In my view, unit tests are the cheapest insurance a software team can buy.