Unit Testing
Definition
A software testing method by which individual units of source code are tested to determine whether they are fit for use.
Deep Dive
Unit testing is a fundamental software testing method focused on verifying the smallest testable parts of an application, known as "units," in isolation. A unit is typically a function, method, or class designed to perform a specific task. The primary goal is to ensure that each component behaves exactly as intended, producing the correct output for a given input and handling edge cases gracefully, before integration with other parts of the system. This practice is crucial for catching defects early in the development lifecycle, significantly reducing the cost and effort of bug fixing later on.
Examples & Use Cases
- 1Testing a `calculateTax()` function to ensure it returns the correct tax amount for various income brackets
- 2Verifying that a `loginUser()` method correctly authenticates valid credentials and rejects invalid ones
- 3Checking if a `ShoppingCart` class accurately adds, removes, and calculates the total price of items.