Unit Testing
A software testing practice where individual units of code are tested in isolation to verify they behave correctly and meet their specifications.
Also known as: Unit Tests, Unit Test
Category: Software Development
Tags: testing, software-engineering, quality, methodologies
Explanation
Unit testing is a software testing practice where individual units of code, typically functions, methods, or classes, are tested in isolation to verify they produce the correct output for given inputs and behave according to their specification.
A unit test follows the Arrange-Act-Assert (AAA) pattern: arrange the preconditions and inputs, act by calling the code under test, and assert that the results match expectations. Good unit tests are fast (milliseconds), isolated (no shared state or external dependencies), repeatable (same result every run), self-validating (pass or fail automatically), and timely (written close to the code they test). These properties are sometimes remembered by the acronym FIRST (Fast, Isolated, Repeatable, Self-validating, Timely).
Unit tests serve multiple purposes beyond catching bugs. They act as living documentation, showing how code is intended to be used. They provide a safety net for refactoring, giving developers confidence to change internal implementations without breaking behavior. They expose design problems: code that is hard to test in isolation often has excessive coupling or unclear responsibilities.
The definition of a 'unit' varies by testing philosophy. In the classical (Detroit-school) approach, a unit may span multiple classes as long as they are fast and deterministic. In the mockist (London-school) approach, a unit is strictly a single class, with all collaborators replaced by test doubles. Both approaches have trade-offs: classical tests are more resilient to refactoring but may have larger failure blast radius, while mockist tests are more focused but more coupled to implementation.
Common unit testing frameworks include JUnit and TestNG (Java), pytest and unittest (Python), Jest, Vitest, and Mocha (JavaScript/TypeScript), xUnit.net and NUnit (.NET), and Google Test (C++). These frameworks provide test runners, assertion libraries, setup/teardown hooks, and test discovery mechanisms.
Unit testing is a cornerstone of Test-Driven Development (TDD), where tests are written before the production code. The testing pyramid model suggests that unit tests should form the largest layer of a test suite, with fewer integration tests and even fewer end-to-end tests above them.
Related Concepts
← Back to all concepts