Unit Testing
What is Unit Testing?
Unit testing checks small pieces of code, such as single functions or classes, in isolation by feeding known inputs and verifying the results. The tests run automatically, often on every commit, and form the base of most testing strategies.
Updated November 2026 · 3 min read
Unit testing is the practice of testing the smallest testable parts of a program, usually individual functions, methods or classes, in isolation from the rest of the system. Each unit test sets up a known input, runs the code and checks that the output or behavior matches what is expected. Developers write unit tests alongside the code and run them automatically, often on every commit, so that mistakes are caught within minutes rather than in later testing or in production.
Unit tests are fast and cheap to run, which is why they form the base of most testing strategies. They do not prove that the whole system works, since that needs integration and end-to-end tests, but they give developers confidence to change code without breaking what already works.
Characteristics of a good unit test
- Isolated. It tests one unit, replacing databases, networks and other components with test doubles such as stubs and mocks.
- Fast. It runs in milliseconds, so the whole suite can run constantly.
- Deterministic. It gives the same result every time. Tests that fail randomly teach people to ignore failures, which undermines the whole suite.
- Focused. It checks one behavior, and its name says what that behavior is.
- Readable. It follows a clear pattern, commonly arrange, act, assert: set up the inputs, call the code, check the result.
Unit testing frameworks
Most modern frameworks descend from the xUnit family. Kent Beck wrote SUnit for Smalltalk, and he and Erich Gamma then created JUnit for Java. Common frameworks today include JUnit for Java, pytest and unittest for Python, Jest and Vitest for JavaScript and TypeScript, NUnit and xUnit.net for .NET, RSpec for Ruby and the built-in testing package for Go. They all provide a way to define tests, make assertions, group tests and report results, and they integrate with continuous integration pipelines.
Unit testing vs. integration and end-to-end testing
The test pyramid, popularized by Mike Cohn in Succeeding with Agile, suggests many fast unit tests at the base, fewer integration tests in the middle and a small number of slow end-to-end tests at the top.
| Level | What it checks | Speed | Typical number |
|---|---|---|---|
| Unit | One function or class in isolation | Milliseconds | Many (hundreds to thousands) |
| Integration | Components working together, such as code and a database | Seconds | Fewer |
| End-to-end | A full user journey through the running system | Seconds to minutes | A small set of critical paths |
How to write unit tests
- Identify the behavior. Decide what the unit should do for normal inputs, edge cases and invalid inputs.
- Write one test per behavior. Name it after the expected behavior, not the method.
- Isolate dependencies. Replace slow or external dependencies with test doubles.
- Run the tests often. Locally while coding and automatically in the CI pipeline.
- Keep tests up to date. When behavior changes on purpose, update the tests in the same change.
Some teams write the test before the code, a practice called test-driven development.
An example
Consider a hypothetical billing function that calculates a prorated charge when a customer upgrades mid-month: the monthly price multiplied by the days remaining, divided by the days in the month. For a $30 plan, the developer writes unit tests for these cases:
- Upgrade with 20 of 30 days remaining: expect $30 x 20 / 30 = $20.00.
- Upgrade on the first day of a 30-day month (30 days remaining): expect the full $30.00.
- Upgrade with 1 day remaining in a 30-day month: expect $1.00.
- Upgrade with 14 of 28 days remaining in February: expect $15.00.
- A negative number of days remaining: expect an error.
The February test fails on the first run because the function assumes every month has 30 days, which would have returned $14.00. The bug is fixed before the code is merged, and the test stays in the suite to stop it from returning. The example is illustrative.
Common pitfalls
- Chasing coverage numbers. High line coverage with weak assertions gives false confidence. Test behaviors that matter.
- Testing implementation details. Tests tied to internal structure break on every refactor. Test inputs and outputs.
- Over-mocking. When everything is mocked, tests pass while the real system fails. Balance with integration tests.
- Tolerating flaky tests. Fix or remove them quickly, or people stop trusting the suite.
- Treating unit tests as the whole strategy. They cannot catch problems in how components fit together or how users experience the product.
Unit testing is the foundation of an automated testing strategy and a core practice in quality assurance. It complements functional testing and code review.
Frequently asked questions
What is the difference between unit testing and integration testing?
Unit tests check one function or class in isolation, replacing databases and networks with test doubles, and run in milliseconds. Integration tests check components working together, such as code and a database, and take seconds. The test pyramid suggests many unit tests, fewer integration tests and a small set of end-to-end tests for key user journeys.
What makes a good unit test?
A good unit test is isolated from external dependencies, runs in milliseconds, gives the same result every time and checks one behavior, with a name that says what that behavior is. It typically follows arrange, act, assert: set up the inputs, call the code, then check the result. Tests that fail randomly should be fixed or removed quickly.
What frameworks are used for unit testing?
Most unit testing frameworks descend from the xUnit family that began with Kent Beck's SUnit and JUnit. Common choices include JUnit for Java, pytest and unittest for Python, Jest and Vitest for JavaScript and TypeScript, NUnit and xUnit.net for .NET, RSpec for Ruby and Go's built-in testing package, all of which plug into continuous integration pipelines.
Related terms
Automated Testing Strategy
Deciding which tests to automate, at what level and when they run.
Quality Assurance (QA)
Process-level work that prevents defects instead of only catching them.
Functional Testing
Checking that software produces the expected results for given inputs.
Usability Testing
Watching real users try tasks to find where a design trips them up.