Skip to main content
  1. Resources/
  2. Study Materials/
  3. Information & Communication Technology Engineering/
  4. ICT Semester 5/
  5. Software Engineering (4353202)/

3 mins· ·
Milav Dabgar
Author
Milav Dabgar
Experienced lecturer in the electrical and electronic manufacturing industry. Skilled in Embedded Systems, Image Processing, Data Science, MATLAB, Python, STM32. Strong education professional with a Master’s degree in Communication Systems Engineering from L.D. College of Engineering - Ahmedabad.
Lecture 40: White Box Testing - Statement & Branch Coverage

Lecture 40: White Box Testing - Statement & Branch Coverage

Unit 5: Software Coding and Testing (4353202)

Lecture Agenda

  • Recap of Black Box Testing (BVA)
  • What is White Box Testing?
  • Why White Box Testing?
  • Code Coverage Metrics
  • Statement Coverage
  • Branch Coverage
  • Key Takeaways

Recap of Black Box Testing (BVA)

Boundary Value Analysis (BVA) is a black box testing technique that focuses on testing values at the extreme ends of input ranges (Min, Min+1, Max-1, Max, Min-1, Max+1) to find defects, often complementing Equivalence Partitioning.

What is White Box Testing?

White Box Testing (also known as Clear Box Testing, Glass Box Testing, or Structural Testing) is a software testing method that examines the internal structure, design, and implementation of the system being tested.

The tester has knowledge of the internal workings of the code, including its logic, paths, and data structures.

Why White Box Testing?

  • Thoroughness: Ensures that all independent paths within a module have been exercised.
  • Early Defect Detection: Helps in finding logical errors, design flaws, and coding mistakes early.
  • Code Optimization: Can identify dead code or inefficient sections.
  • Security Vulnerabilities: Helps uncover security flaws in the code logic.
  • Completeness: Provides a measure of how much of the code has been tested.

Code Coverage Metrics

Code coverage metrics are used in white box testing to measure the percentage of code that has been executed by a test suite. They provide an indication of the thoroughness of testing.

  • Statement Coverage: Measures if every executable statement has been executed.
  • Branch Coverage: Measures if every branch (true/false) of decision points has been executed.
  • Path Coverage: Measures if every possible path through the code has been executed.
  • Condition Coverage: Measures if every boolean sub-expression has been evaluated to both true and false.

Statement Coverage

Statement Coverage ensures that every executable statement in the source code has been executed at least once during testing.

Formula: (Number of Executed Statements / Total Number of Statements) * 100%

public void example(int x) {
    if (x > 0) { // Statement 1
        System.out.println("Positive"); // Statement 2
    } else {
        System.out.println("Non-positive"); // Statement 3
    }
    System.out.println("Done"); // Statement 4
}

Test Case: `example(5)` executes Statements 1, 2, 4. (75% coverage)

Test Case: `example(-2)` executes Statements 1, 3, 4. (75% coverage)

To achieve 100% Statement Coverage: Need both `example(5)` and `example(-2)`.

Branch Coverage

Branch Coverage (also known as Decision Coverage) ensures that every branch (true/false) of every decision point (e.g., `if`, `else`, `switch`, `loops`) in the code has been executed at least once.

Formula: (Number of Executed Branches / Total Number of Branches) * 100%

public void checkEligibility(int age) {
    if (age >= 18) { // Decision Point
        System.out.println("Eligible to vote"); // Branch 1 (True)
    } else {
        System.out.println("Not eligible to vote"); // Branch 2 (False)
    }
}

To achieve 100% Branch Coverage:

  • Test Case 1: `checkEligibility(20)` (executes Branch 1 - True)
  • Test Case 2: `checkEligibility(16)` (executes Branch 2 - False)

Key Takeaways

  • **White Box Testing** involves knowing the internal structure of the code.
  • **Code Coverage Metrics** measure the thoroughness of white box testing.
  • **Statement Coverage** ensures every line of code is executed.
  • **Branch Coverage** ensures every decision path (true/false) is exercised.
  • Branch coverage is stronger than statement coverage.

Next Lecture

Topic: White Box Testing - Path & Condition Coverage

Q & A

Questions & Discussion