Developer & Data Platforms

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.

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

featureUnit testingIntegration testing
ScopeSingle function or componentSeveral parts working together
SpeedVery fastSlower
FindsLogic bugsConnection and data problems
QuantityManyFewer

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.