Unit testing checks individual functions, methods or classes to verify they behave as expected. Developers use it to catch defects early, such as a discount calculation returning the wrong price. It does not replace integration or system testing.
Software applications contain multiple components responsible for different tasks. Even a small error in one component can affect the application’s overall functionality.
Unit testing helps developers identify these errors early by checking individual components before they are integrated. This article explains how unit testing works, its types, practical examples and commonly used tools.
What Is Unit Testing in Software Testing?
Unit testing verifies whether individual software components, such as functions, methods or classes, behave according to their requirements.
A unit is the smallest part of an application that can be tested independently. For example, a banking application may contain separate functions for calculating interest, checking account balances and validating transactions.
Developers test these functions individually using different inputs and expected results. They often isolate external dependencies to focus on the behaviour of the selected unit.
Unit testing is generally performed during development and is usually the first testing level among the different types of software testing. It helps establish confidence in individual components before integration testing begins.
Why Is Unit Testing Important?
Unit testing helps development teams identify problems before they affect other software components.
Its main benefits include:
- Early defect detection: Finds errors before different components are integrated.
- Easier debugging: Helps developers identify which function produces incorrect results.
- Safer code changes: Detects unexpected behaviour after modifications.
- Better maintainability: Encourages smaller, testable components.
- Faster feedback: Automated tests quickly identify failures during development.
For example, correcting a discount calculation before connecting it to the checkout system is generally easier than investigating the same defect after integration.
However, unit testing alone cannot guarantee that the entire application works correctly.
How Does Unit Testing Work?
Unit testing follows a structured process that checks individual components against expected behaviour.
Step 1: Identify the Unit
Select a specific function or method to test.
For example, choose a function that calculates the total amount after applying a discount.
Step 2: Create Test Cases
Identify different conditions the function should handle.
These might include valid discounts, zero discounts, maximum discounts and invalid values.
Step 3: Define Expected Results
Determine the correct output for each input before running the tests.
For example, applying a 10% discount to ₹1,000 should return ₹900.
Step 4: Execute Unit Tests
Run the selected tests manually or through a testing framework.
The framework executes the function and compares its output with the expected result.
Step 5: Analyse Results and Fix Defects
A passing test confirms that the observed result matches the expected result for that test case.
If a test fails, developers investigate the cause, correct the implementation and rerun the tests.
This process helps verify changes without repeatedly testing the entire application.
Types of Unit Testing
Unit testing can be performed manually or automatically.
Manual Unit Testing
Manual unit testing involves checking individual components without using an automated test runner.
A developer might execute a function with selected inputs and inspect its output.
This approach can be useful for quick checks, but it becomes time-consuming when tests require frequent repetition.
Automated Unit Testing
Automated unit testing uses scripts and frameworks to execute predefined tests.
For example, a developer can create multiple tests for a calculation function and run them automatically whenever the code changes.
Automated tests are repeatable, faster to execute at scale and suitable for continuous integration workflows.
Most development teams prefer automated unit tests for ongoing code verification.
Unit Testing Examples
Practical examples make it easier to understand what unit testing checks.
Example 1: E-commerce Discount Calculation
Suppose an online store has a function that calculates the final price after a discount.
The original product price is ₹1,000.
A developer creates the following test cases:
| Test case | Discount | Expected result |
| Standard discount | 10% | ₹900 |
| No discount | 0% | ₹1,000 |
| Full discount | 100% | ₹0 |
| Invalid discount | 120% | Validation error |
The last case assumes the function is designed to reject discounts above 100%.
Each test checks whether the calculation function behaves correctly under different conditions.
It does not verify the shopping cart, payment gateway or complete checkout journey.
Example 2: Unit Testing in Python
Consider a simple Python function that adds two numbers.
def add_numbers(a, b):
return a + b
We can test this function using pytest:
def test_add_numbers():
assert add_numbers(5, 3) == 8
assert add_numbers(-2, 2) == 0
assert add_numbers(0, 0) == 0
Here, assert checks whether the actual output matches the expected value.
If every assertion succeeds, the test passes. If any assertion fails, pytest reports a failure.
The examples cover positive numbers, negative numbers and zero.
Popular Unit Testing Tools and Frameworks
Developers use testing frameworks to organise tests, execute assertions and identify failures.
Common frameworks include:
- JUnit: A Java testing framework commonly used for unit testing.
- pytest: A Python framework supporting simple assertions, fixtures and automated tests.
- Jest: A JavaScript testing framework used for functions and application components.
- NUnit: A .NET testing framework commonly used with C# applications.
The appropriate framework depends on the programming language, project requirements and development environment.
These tools can also support automated execution within continuous integration pipelines.
Unit Testing vs Integration Testing
Unit testing and integration testing examine different aspects of software behaviour.
Unit testing verifies whether an individual component works correctly. Integration testing checks whether multiple components communicate and operate correctly together.
| Feature | Unit Testing | Integration Testing |
| Testing scope | Individual components | Connected components |
| Main objective | Verify isolated behaviour | Verify component interactions |
| Dependencies | Usually controlled or isolated | Multiple real or simulated components |
| Example | Test discount calculation | Test discount and order services |
For example, a discount function might pass every unit test but fail when the order service sends an unexpected value.
An integration test can reveal that interaction problem.
Both testing levels are necessary because successful unit tests do not guarantee successful integration.
Learn more in our Integration Testing guide.
Advantages and Limitations of Unit Testing
Unit testing offers several advantages, but understanding its limitations is equally important.
Advantages
- Detects defects during early development.
- Makes code changes and refactoring safer.
- Simplifies debugging by testing smaller components.
- Supports repeatable automated checks.
- Provides feedback before software integration.
Limitations
- Cannot verify complete user journeys.
- May miss communication failures between components.
- Requires maintenance when requirements or implementation details change.
- Cannot fully reproduce real databases, networks or external services when dependencies are simulated.
Therefore, unit testing should complement integration and system testing rather than replace them.
Unit Testing Best Practices
Effective unit tests should be understandable, reliable and easy to maintain.
Follow these practices:
- Keep tests independent: Each test should run without depending on another test’s results.
- Use descriptive names: Clearly indicate which behaviour or condition is being tested.
- Cover different inputs: Include positive, negative, invalid and boundary cases.
- Make meaningful assertions: Check expected behaviour rather than simply executing code.
- Run tests regularly: Execute unit tests after code changes and during continuous integration.
A useful technique is Arrange, Act, Assert.
First, prepare the required inputs. Next, execute the function. Finally, compare the actual result with the expected outcome.
This structure makes tests easier to read and troubleshoot.
Final Thoughts
Unit testing helps developers identify defects early, maintain reliable components and make safer code changes. However, it works best alongside other testing levels.
Understanding these fundamentals is valuable for aspiring testers. Explore Tester Academy’s practical manual testing course to strengthen your software testing knowledge.
Frequently Asked Questions
What is unit testing with an example?
Unit testing checks individual software components. For example, testing whether a calculator function returns 10 when adding 4 and 6 is a unit test.
Who performs unit testing?
Developers usually perform unit testing because it requires knowledge of the application’s code. Test automation engineers may also contribute to unit-level tests.
Is unit testing manual or automated?
Unit testing can be manual or automated. However, automated unit testing is generally preferred because tests can run repeatedly and produce consistent feedback.
Does unit testing require coding?
Automated unit testing generally requires programming knowledge. Manual QA testers can understand unit testing concepts without writing code, although developers typically create the tests.
What is the difference between unit testing and system testing?
Unit testing checks individual functions or components. System testing evaluates the behaviour of an integrated application against its specified requirements.
Related guides
Stay in the loop
Get new batch dates on WhatsApp. One message when a batch opens, nothing else.
By subscribing you agree to receive WhatsApp messages from Tester Academy. Read our Privacy policy.

