Software Engineering basics - Structural Testing in Software Engineering

Structural Testing in Software Engineering

Definition

Structural Testing (also called White-Box Testing or Glass-Box Testing) is a testing technique that examines the internal structure, design, and code of a software program rather than its external functionality.

It ensures that all internal paths, logic, and data flows in the code work as intended.


Purpose of Structural Testing

  • To verify the internal logic and structure of the program.

  • To ensure complete code coverage (every statement, branch, and condition is tested).

  • To detect hidden errors in loops, conditions, and control flows.

  • To improve code reliability and maintainability.


Key Idea

Unlike black-box testing (which tests based on input-output behavior), structural testing checks how the output is produced — i.e., the actual implementation logic inside the code.


Activities Involved in Structural Testing

Step No. Activity Description
1 Understand Program Structure Study the source code or design to understand its control flow and logic.
2 Identify Testable Elements Identify statements, branches, loops, and decision points that must be tested.
3 Select Coverage Criteria Choose a coverage type such as statement, branch, condition, or path coverage.
4 Design Test Cases Create test cases that execute specific code structures (e.g., true/false conditions).
5 Execute Tests Run the test cases and observe which parts of the code are executed.
6 Measure Coverage Use coverage analysis tools to determine how much of the code was tested.
7 Analyze Results & Fix Errors Debug and correct logical or structural errors found during execution.

Types of Structural Testing

Type Description
Statement Coverage Ensures every statement in the code is executed at least once.
Branch Coverage Ensures every possible branch (true/false) of each decision point is executed.
Condition Coverage Ensures each Boolean condition is evaluated to both true and false.
Path Coverage Ensures all possible execution paths in the code are tested.
Loop Coverage Ensures loops are tested for zero, one, and multiple iterations.

Diagram: Structural Testing Process

Here’s a simple conceptual flow (text-based):

        ┌──────────────────────────┐
        │     Source Code           │
        └────────────┬──────────────┘
                     │
                     ▼
        ┌──────────────────────────┐
        │ Identify Control Flow     │
        └────────────┬──────────────┘
                     │
                     ▼
        ┌──────────────────────────┐
        │ Design Structural Tests   │
        │ (based on code logic)     │
        └────────────┬──────────────┘
                     │
                     ▼
        ┌──────────────────────────┐
        │ Execute Test Cases        │
        └────────────┬──────────────┘
                     │
                     ▼
        ┌──────────────────────────┐
        │ Measure Code Coverage     │
        └────────────┬──────────────┘
                     │
                     ▼
        ┌──────────────────────────┐
        │ Fix Logical Errors        │
        └──────────────────────────┘

Example

Let’s consider a simple code snippet:

if A > 0:
    print("Positive")
else:
    print("Non-positive")

To achieve branch coverage:

  • Test case 1: A = 5 → takes the “if” branch

  • Test case 2: A = -3 → takes the “else” branch

Thus, both possible branches of the decision are tested.


Advantages

  • Detects logic and structural errors early.

  • Ensures maximum code coverage.

  • Improves software quality by testing internal logic.

  • Useful for complex algorithms and critical systems.


Disadvantages

  • Requires access to source code.

  • Needs technical knowledge (programming and logic).

  • Time-consuming for large programs.

  • Not suitable for testing user interface or usability aspects.


Summary Table

Aspect Description
Testing Type White-box testing
Focus Internal code structure
Goal Verify all paths, branches, and logic
Main Tools Coverage analyzers (e.g., JaCoCo, Clover, Cobertura)
Coverage Types Statement, branch, path, condition