Unit testing
Unit testing, also known as component testing, is a software testing methodology in which individual units or components of a codebase are tested in isolation from the rest of the system. A "unit" typically refers to the smallest testable piece of software, commonly a single function, method, or class. The goal is to verify that each unit behaves correctly according to its specification before it is integrated with other parts of the application.
Because units are tested in isolation, external dependencies such as databases, network services, or other modules are often replaced with mocks, stubs, or fakes to keep tests fast and deterministic. Dependency injection is the structural basis for this isolation. By receiving collaborators through abstractions, the unit under test can be handed stand-ins in place of its real dependencies.
Unit testing is most commonly performed by developers rather than dedicated testers. It is a foundational practice in methodologies such as test-driven development (TDD) and extreme programming (XP).
A comprehensive unit test suite serves as a safety net during refactoring and ongoing development, providing rapid feedback when a code change inadvertently breaks existing behavior.
While unit testing cannot catch integration defects — those arising from the interaction between components, which are the target of integration testing — it remains one of the most cost-effective forms of testing, since it catches bugs very early in the software development cycle.