24 White Box Testing

WHITE BOX TESTING

  • Conducted by Chris Mills, Ph.D. at Florida State University.

WHITE BOX UNIT-TESTING TECHNIQUES

  • Focus Areas:

    • Statement Coverage

    • Branch Coverage

STATEMENT COVERAGE

  • Definition: 100% Statement coverage means running a set of test cases where every statement is executed at least once.

  • Industry Use: Commonly used in the industry; achieving 80-90% coverage is considered good.

  • Purpose: Helps identify executed and unexecuted statements due to blockages in the code.

  • Weakness: Fails to address coverage concerning branch statements which can lead to untested paths in the code.

PRINT SUM EXAMPLE

  • Test Case #1: a = 7, b = 14

  • Test Case #2: a = -5, b = -8

  • Method Implementation:

    void printSum(int a, int b) {
      int result = a + b;
      if (result > 0) {
        print("red", result);
      } else if (result < 0) {
        print("blue", result);
      }
    }

CONTROL FLOW STRUCTURE

  • Structure Overview:

    • IF Statements denote multiple branches based on expressions and conditions.

    • THEN / ELSE Statements include execution paths; illustrating control flow dependencies.

    • CASE Statements: indicates branching behavior in the code based on condition evaluations.

PRINT SUM BRANCH COVERAGE

  • Test Cases:

    • Test Case #1: a = 7, b = 14

    • Test Case #2: a = -5, b = -8

    • Test Case #3: a = -5, b = 5

  • Example Method:

    void printSum(int a, int b) {
      int result = a + b;
      if (result > 0) {
        print("red", result);
      } else if (result < 0) {
        print("blue", result);
      }
      [Else do Nothing]
    }

BRANCH COVERAGE

  • Definition: The process of running test cases where every branch is executed at least once, along with all statements.

  • Improved Functionality: Addresses weaknesses of statement coverage by considering the conditional flow of the code.

  • Control Flow Graph Use: Determining branches based on conditional statements visually represents the control flow of the program, making branch coverage a stronger criteria than statement coverage.

PRINT SUM BRANCH DETAILS

  • Flow of execution illustrated through test cases, highlighting true/false paths.

  • Execution Path:

    • Each part: F, T indicates whether the branch conditions were met.

    • Graphically representing execution paths enhances understanding of code flow and coverage.

  • Coverage Summary for Test Cases:

    1. a = 7, b = 14

    2. a = -5, b = -8

    3. a = -5, b = 5

  • Each test case explores varying conditions leading to different execution paths in the printSum method.