Unit test
YOO-nit test
An automated test that verifies a small, isolated piece of code behaves correctly.
A unit test is an automated test that verifies a small piece of code works as expected. The "unit" is typically a function or a small module. The test calls the code with specific inputs and checks that the outputs match expectations. Unit tests run fast (milliseconds each) because they do not touch databases, APIs, or file systems.
The value of unit tests is fast feedback. You change a function, run the tests, and know within seconds if you broke something. A good test suite has hundreds or thousands of unit tests that run in under a minute. This speed enables developers to run tests constantly while coding, not just before committing.
The best unit tests verify behavior, not implementation. A test that checks "given an order with three items, the total is $45.97" is testing behavior. A test that checks "the calculateTotal function calls the taxService.getTaxRate method" is testing implementation. The first test survives refactoring. The second breaks every time you change the internal structure, even if the behavior is still correct.
Examples
A developer writes unit tests for a pricing function.
The function calculates order totals with quantity discounts. The developer writes tests: single item ($10), five items with 10% discount ($45), twenty items with 20% discount ($160), zero items ($0), and negative quantities (should throw an error). Five tests, each running in 2 milliseconds. Every edge case is covered. When the function is refactored later, the tests verify the behavior is preserved.
A team uses unit tests to catch a regression.
A developer changes the date parsing logic to handle a new format. The change accidentally breaks parsing for the old format. The unit test 'should parse ISO 8601 dates' fails immediately. The developer sees the failure, adjusts the code to handle both formats, and all tests pass. Total time from introducing the bug to fixing it: 3 minutes. Without the test, this bug would have reached production.
A team rewrites their test suite to focus on behavior.
The old test suite has 600 tests. Half of them test internal methods directly, mock every dependency, and break on every refactor. The team rewrites the tests to call only public APIs and assert on outputs. The test count drops to 400, but coverage stays at 90% because the behavior tests exercise the internal methods through the public interface. Refactoring no longer requires updating dozens of tests.
In practice
Read more on the blog
Frequently asked questions
What is the difference between unit tests and integration tests?
Unit tests verify isolated pieces of code without external dependencies. They are fast and focused. Integration tests verify that multiple components work together correctly, often involving databases, APIs, or file systems. They are slower but catch problems that unit tests cannot, like incorrect SQL queries or API contract violations. You need both. Unit tests for fast feedback on logic, integration tests for confidence in the full system.
How many unit tests should a project have?
There is no magic number. The right question is: does your test suite give you confidence to ship? A pricing module might need 50 unit tests to cover all the business rules. A simple utility function might need 5. Aim for tests that cover every business behavior and edge case. If you are afraid to change code because you might break something, you need more tests. If you are spending hours maintaining tests, you might be testing implementation details instead of behavior.
Related terms
An automated test that verifies multiple components or services work correctly together.
A metric measuring what percentage of source code is executed by automated tests.
Continuous integration and continuous deployment: automating code testing and delivery to production.
Restructuring existing code without changing its external behavior to improve readability and maintainability.
The accumulated cost of shortcuts and deferred work in a codebase that slows future development.

Want the complete playbook?
Picks and Shovels is the definitive guide to developer marketing. Amazon #1 bestseller with practical strategies from 30 years of marketing to developers.