Unit Testing (original) (raw)

Last Updated : 19 May, 2026

Unit Testing is a software testing method in which individual units or components of a software application (such as functions, methods, or classes) are tested in isolation to verify that they work correctly as expected.

Real-World Examples

Unit testing is widely used across real-world applications to verify critical business logic and ensure reliable system behavior:

Workflow of Unit Testing

The unit testing workflow includes the following step by step process:

1. Create Test Case

Writing the actual unit test cases for a specific function or method before review.

2. Review Test Case

Peer or senior review of the created test cases for quality and completeness.

3. Baseline Test Case

Officially approving and freezing the reviewed test cases as the standard version.

4. Execute Test Case

Running the baselined test cases to verify the code behavior and generate results.

Types of Unit Testing

Unit testing can be performed manually or automatically:

1. Manual Unit Testing

Manual Testing involves developers testing individual code units manually without using automation tools. It is not commonly used for large-scale testing, but it can still be useful during debugging and initial verification of code logic.

2. Automated Unit Testing

Automated Unit Testing checks software functionality automatically using testing tools and frameworks, reducing manual effort and improving accuracy. Developers write small test cases to validate individual functions automatically during development. These test cases verify whether the function behaves as expected under different conditions.

Architecture of Unit Testing

Unit Testing Architecture defines the structured approach used to design and organize unit tests, ensuring that individual components are tested in isolation for correctness and reliability.

It forms the base of the Testing Pyramid (Unit -> Integration -> End-to-End) and is the most frequently used due to its speed and efficiency.

Components of Unit Testing Architecture

**1. Test Isolation & Dependencies: Each unit is tested in complete isolation from external systems such as databases, APIs, and file systems. To achieve this, mocks, stubs, or fakes are used to simulate dependencies so that only the internal logic of the unit is tested.

**2. AAA Pattern (Arrange–Act–Assert): A widely used structure for writing clean and readable unit tests:

This architecture focuses on modular design and isolated testing of individual components using structured approaches like the AAA pattern. Since unit tests run quickly (often in milliseconds), they provide rapid feedback to developers.

It is commonly supported by frameworks such as JUnit (Java), pytest (Python), xUnit (.NET), and Jest (JavaScript).

Unit Testing Strategies

To create effective unit tests the following techniques are commonly used:

Unit Testing Techniques

There are 3 types of Unit Testing Techniques. They are as follows:

Characteristics of Unit Testing

A good unit test ensures reliable and maintainable software by validating individual code units effectively. The key characteristics of a unit test are:

Benefits of Unit Testing

Unit testing helps ensure reliable software by validating individual components of an application in isolation.

Limitations of Unit Testing

Unit testing is an important part of software testing, but it has certain limitations that affect its scope and effectiveness.

Implementation Example

The following example demonstrates Java code with appropriate unit test cases.

**Step 1. Create the Calculator class.

Java `

public class Calculator {

// Method to add two numbers
public int add(int a, int b) {
    return a + b;
}

// Method to subtract two numbers
public int subtract(int a, int b) {
    return a - b;
}

}

`

**Step 2. Create the TestNG test class.

Java `

package com.example.tests;

import org.testng.Assert; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test;

public class CalculatorTest {

private Calculator calculator;

// This method runs before each test method
@BeforeMethod
public void setUp() {
    calculator = new Calculator();
}

// Test for the 'add' method
@Test
public void testAdd() {
    int result = calculator.add(5, 3);
    // Assert that the result of 5 + 3 is 8
    Assert.assertEquals(result, 8, "Addition result is incorrect");
}

// Test for the 'subtract' method
@Test
public void testSubtract() {
    int result = calculator.subtract(5, 3);
    // Assert that the result of 5 - 3 is 2
    Assert.assertEquals(result, 2, "Subtraction result is incorrect");
}

}

`

**Output:

unit-testing-example-output

Unit Testing Example Output

Best Practices for Unit Testing

Following best practices ensures that unit tests are effective, reliable, and easy to maintain over time.

Following are some of the unit testing tools: