Software Testing
Unit - 3
What is Unit Testing?
Unit Testing is a type of software testing where the smallest part (unit) of a program is
tested individually to check whether it works correctly.
A unit can be: A function
When is it Performed?
• A method
During the development phase
• A class
Before integration testing
• A module
Why is Unit Testing Important? Who Performs Unit Testing?
• Detects bugs early Mostly performed by Developers
• Reduces debugging cost
• Improves code quality
• Makes code easier to maintain
• Ensures each module works correctly
Example: Unit Testing of a Function with Test Cases
Test Case Input (a, b) Condition Expected Output
TC1 (5, 3) a>b 5
TC2 (2, 7) a<b 7
TC3 (4, 4) a=b 4
TC4 (-1, -5) negative numbers -1
What is Integration Testing?
Integration Testing is a level of testing where two or more modules are combined and
tested together to verify that they work correctly as a group.
It checks the interaction between modules.
Why Do We Need Integration Testing?
Even if individual units work correctly (unit testing passed),errors can occur when modules interact.
Common issues:
• Wrong data passing
• Interface mismatch
• Incorrect API calls
• Database connection errors
Example
Suppose we have:
1. Login Module
2. Database Module
Login module sends username/password
Database module verifies credentials
Even if both modules work individually, they may fail when connected.
Testing them together = Integration Testing
Types of Integration Testing
1. Big Bang Integration When Do We Use Big Bang?
All modules are integrated at once, Use Big Bang approach when:
Then the entire system is tested together. 1. Project is small
There is no step-by-step integration. 2. Modules are already completed
Difficult to find exact bug location 3. Interfaces are simple
4. Quick testing is needed
5. Time is limited
Not suitable for large, complex systems
Example of Big Bang Integration Testing
A simple Online Shopping System
Step 1: Individual Modules (Already Unit Tested)
Assume we have 4 modules:
1. Login Module
2️. Product Module
3. Cart Module
4. Payment Module
Each module is tested separately using Unit Testing.
Step 2: We connect ALL modules at once, instead of integrating step-by-step
Login → Product → Cart → Payment
All modules are integrated together in one go.
def login(user):
return True if login("Asha"):
if add_to_cart("Laptop"):
def add_to_cart(item):
return True if make_payment(50000):
print(confirm_order())
def make_payment(amount):
return True
def confirm_order(): All functions are tested together in one go.
return "Order Confirmed"
What is Incremental Integration?
Incremental Integration Testing is a type of integration testing where Modules are integrated
step-by-step (gradually) And tested after each integration.
Instead of connecting all modules at once (like Big Bang),
we integrate one module at a time.
Why Use Incremental Integration?
Easy to find defects
Low risk
Better debugging
Suitable for large systems
How It Works
Suppose we have 4 modules:
Step 1: Integrate M1 + M2
Login (M1) Test them.
Product (M2) Step 2: Add M3
Cart (M3) Test again.
Payment (M4) Step 3: Add M4
Test full system.
Errors can be found easily at each
step.
Types of Incremental Integration Testing
Modules are integrated step-by-step and tested progressively.
a) Top-Down Integration
b) Bottom-Up Integration
Top-Down Integration Testing
Top-Down Integration Testing is a method where testing starts from the top (main module)
and gradually integrates lower-level modules step by step until the whole system is tested.
How It Works
[Link] the main control module first.
[Link] lower modules that are not yet developed with stubs (temporary dummy modules).
[Link] replace stubs with real modules.
[Link] testing after each integration.
When to Use
• When system design is hierarchical What is a Stub?
• When top modules control program flow A stub is a small temporary
• When early validation of logic is required program that simulates the
behavior of a lower module.
Advantages
It returns fixed or simple values
• Early testing of system architecture
so testing can continue even if real
• Major logic errors found early
modules are missing.
• Useful when top modules are critical
Disadvantages
• Low-level modules tested late
• Need to create many stubs
• Stubs may not fully simulate real modules
Bottom-Up Integration Testing
Bottom-Up Integration Testing is an approach where testing starts from the lowest-level
modules and gradually moves upward by integrating higher-level modules step by step.
How It Works
• Test individual low-level modules.
What is a Driver?
• Combine related low-level modules into clusters.
A driver is a temporary program that
• Use a driver to simulate higher-level modules.
• Integrate the next higher module. simulates the behavior of a higher-
• Repeat until the whole system is integrated. level module that calls the module
being tested.
When to Use
• When low-level modules are complex or critical
• When utility components must be verified first
• When development is done bottom layer first
Advantages Disadvantages
• Low-level functionality tested early • Main system logic tested late
• Easier fault localization • Drivers must be created
• No need for stubs • System structure not validated early
• Good for systems with strong utility modules