Software Engineering basics - Unit Testing in Software Engineering

Unit Testing in Software Engineering

Definition

Unit testing is a type of software testing where individual components (or units) of a program — such as functions, methods, or classes — are tested in isolation to ensure they work correctly.

Each unit is the smallest testable part of the software.


Purpose of Unit Testing

  • To verify the correctness of individual units.

  • To detect bugs early in the development cycle.

  • To simplify debugging — since tests isolate small parts of code.

  • To support code refactoring — ensuring functionality remains intact.


Unit Test Procedure

Step Description
1. Identify the Unit Select the smallest testable part of the software (e.g., function, method, or class).
2. Define Test Cases Prepare input data and expected results for that unit.
3. Develop Test Code Write test scripts using a unit testing framework (e.g., JUnit, NUnit, PyTest).
4. Execute Test Cases Run the test cases to verify that the unit behaves as expected.
5. Compare Results Compare actual output with expected output.
6. Fix Defects (if any) Debug and correct the code if tests fail.
7. Re-run Tests Re-execute tests to confirm the fix.
8. Record Results Document the outcome for reporting and regression testing.

Diagram: Unit Testing Procedure

Here’s a simple conceptual diagram:

           ┌────────────────────────────────┐
           │          Source Code           │
           └──────────────┬─────────────────┘
                          │
                          ▼
               ┌─────────────────────┐
               │ Identify Unit Under │
               │       Test          │
               └─────────┬───────────┘
                         │
                         ▼
              ┌──────────────────────┐
              │ Design Test Cases    │
              └─────────┬────────────┘
                        │
                        ▼
              ┌──────────────────────┐
              │ Write Unit Test Code │
              └─────────┬────────────┘
                        │
                        ▼
              ┌──────────────────────┐
              │ Execute Test Cases   │
              └─────────┬────────────┘
                        │
                        ▼
              ┌──────────────────────┐
              │ Compare Results      │
              └─────────┬────────────┘
                        │
         ┌──────────────┴──────────────┐
         │                             │
         ▼                             ▼
 ┌─────────────────┐          ┌──────────────────┐
 │ Tests Passed    │          │ Tests Failed     │
 └──────┬──────────┘          └────────┬─────────┘
        │                              │
        ▼                              ▼
 ┌──────────────┐              ┌──────────────────┐
 │ Record Result│              │ Debug and Fix Code│
 └──────────────┘              └──────────────────┘

Example

Let’s say you have a function:

def add(a, b):
    return a + b

A unit test for this in Python (using pytest) would be:

def test_add():
    assert add(2, 3) == 5
    assert add(-1, 1) == 0

Running this ensures the add() function works correctly for multiple inputs.


Common Tools for Unit Testing

  • Java: JUnit, TestNG

  • Python: PyTest, Unittest

  • C#: NUnit, MSTest

  • JavaScript: Jest, Mocha