What is White Box testing?

White-box testing is a software testing method that tests a software's internal structures or workings instead of its functionality.

White-box testing is done by testers who have internal knowledge of the system as well as programming skills. Testers read the software specifications and design test cases to execute different code-paths through the code. You cannot achieve this through black-box testing.

An analogy

Imagine you are commissioning the air conditioning of a large departmental store.

In black-box testing, you would start the air conditioning system and verify the temperatures at various spots inside the store are as expected at different points in time.

However, in white-box testing, the testers would check whether:

  • Every unit is performing as expected.
  • The workload s evenly distributed between the individual air conditioning units.
  • There is no coolant leakage inside the individual units.
  • The carbon-di-oxide levels in the store are in the normal range.

When is White Box Testing done?

You can do white-box testing at the unit, integration, and system levels of software testing.

Most teams use a code coverage tool as it is inexpensive and speeds up white-box testing. Your code coverage tool will monitor your test suite's execution and tell you how much of the statements, branches, functions, and lines were run as part of your tests.

White-box testing techniques

Statement Coverage

In this technique, test cases are written to ensure that every statement in the code is executed at least once.

In the code's flow-chart representation, the two arrows represent test cases required to cover all the statements.

Path Testing

The test cases in this technique ensure that every complete path in the program has been executed at least once. In the flow-chart representation of the code in the example below, the four arrows represent the test cases to cover all paths.

Condition Coverage

In this technique, all scenarios involving conditions are covered as shown in the following example:

function doSomething(int age, boolean hasParentalConsent)
{
    if (age < 18 AND hasParentalConsent == false)
    {
        error(' Must be an adult or parental consent is required');
    }
    ...
}

In this example, you will write the following test cases to cover the evaluation of the expression: age < 18 and hasParentalConsent == false:

  • age is 17, hasParentalConsent is false
  • age is 17, hasParentalConsent is true
  • age is 18, hasParentalConsent is false
  • age is 18, hasParentalConsent is true
  • age is 19, hasParentalConsent is false
  • age is 19, hasParentalConsent is true

Loop Testing

Loops (e.g., 'while' and 'for' statements) are central to many algorithms. Defects often occur at the beginning or end of a loop or when it is skipped altogether.

For simple loops with N iterations, test cases are designed that:

  • Bypass the loop entirely
  • Do a single pass
  • Do M passes, where M < N
  • Do N-1 passes
  • Do N passes

Advantages of White Box Testing

  • White-box testing can help detect inefficient code, dead code, and memory leaks.
  • White-box testing can identify missing scenarios and gaps in the software specifications.
  • White-box testing cases can be easily automated once test cases are in place.
  • White-box testing can start early in the testing process as UI is not required.

Disadvantages of White-Box Testing

  • White box testing is difficult and expensive.
  • The software specification is often not up-to-date, making white-box testing ineffective.
  • White-box testing often requires more skills than the original programmers who wrote the code as it requires a deeper level of understanding to design the test cases and analyze the results.