
What Is Unit Testing?
Unit testing is the practice of testing the smallest testable parts of a software system, such as individual functions, methods, or classes, to verify that each works correctly in isolation.
A unit is typically a single piece of logic with a clear input and output. Unit tests confirm that this logic behaves as expected under normal conditions, edge cases, and error scenarios.
| Why It Matters | Who Performs It | When It Occurs |
|---|---|---|
|
|
|
Typical Unit Testing Techniques
- White-Box Testing
- Tests are designed with full knowledge of the internal code structure.
- Purpose: Ensure all logical paths and conditions are exercised.
- Techniques include:
- Statement coverage
- Branch coverage
- Path coverage
- Equivalence Partitioning
- Input data is divided into valid and invalid groups (partitions), and one value from each group is tested.
- Purpose: Reduce the number of test cases while maintaining coverage.
- Boundary Value Analysis
- Focuses on values at the edges of acceptable input ranges.
- Purpose: Catch defects that often occur at limits (e.g., minimums and maximums).
- Error Handling & Exception Testing
- Verifies how the unit behaves when invalid data, exceptions, or failures occur.
- Purpose: Ensure graceful handling of errors and predictable system behavior.
- Mocking and Stubbing
- External dependencies (databases, APIs, services) are replaced with simulated objects.
- Purpose: Allow the unit to be tested in isolation, without relying on other components.
Key Takeaways
- Unit testing ensures individual pieces of code work correctly before they are combined into a complete system.
- Strong unit testing dramatically reduces downstream defects and creates a stable foundation for all other testing activities.