About

3.2 Black Box vs. White Box Testing

Theory: Black Box vs. White Box Testing

Every test design technique in this course belongs to one of two families, depending on a single question: do you look inside the program or not? This lesson introduces both families, shows why neither can test everything, and explains the order the professor insists on.

The Vending Machine

Imagine you are asked to test a vending machine. There are two ways to go about it:

These are exactly the two testing strategies.

Black box?inputoutputinput-output / data-drivenWhite boxlogic-driven / transparent
Black box: you see only what goes in and what comes out. White box: you see the code and every path inside it.

Can Either One Test Everything?

No. Each family hits its own wall, and the two walls have names.

Black box: exhaustive input testing

To be certain from the outside, you would have to try every possible input. This is exhaustive input testing, and it is impossible: remember the calculator in 2.1, where two three-digit numbers and four operators already gave 4,000,000 tests.

It is even worse than that, because the order of inputs matters too. Think of the power button on a TV remote. Pressing it turns the TV on or off, depending on what happened before. So it is not enough to try every input once; you would have to try every sequence of inputs, and the number of sequences has no end.

White box: exhaustive path testing

To be certain from the inside, you would have to follow every path through the code. This is exhaustive path testing, and it is also impossible, because loops repeat paths again and again: a loop that can run 1, 2, 3, … times creates a new path for every number of repetitions.

And even if you could test every path, you would still not be done. Every path could work exactly as the code says, and the user could still be unhappy because the code does the wrong thing, or misses something the user needed. That is bug definition 5 from 1.6. Path testing only checks the code that exists; it cannot find code that should exist but does not.

Black boxWhite box
You seeOnly the interface (input and output)The code and its paths
Other namesInput-output driven, data-drivenLogic-driven, transparent box
Tests come fromThe specificationThe program’s logic
The impossible idealExhaustive input testingExhaustive path testing
Why it is impossibleToo many inputs, and input order mattersLoops multiply the paths; even all paths can leave bug definition 5

Which One First?

Black box first. The reason is bias.

When you read the code, you start to think like the programmer, and the programmer believes the program works. You see how the code handles a case, and you unconsciously test that it does what it does, instead of testing what it should do. That bias leaks into your tests.

So the professor’s order is:

  1. Start with black box tests while your mind is still free of the code.
  2. When you run out of ideas, open the box and add white box tests for the paths and conditions you have not reached.

Where does unit testing fit?

Unit testing can be either one. If you test a method only through its inputs and outputs, it is black box. If you read its code and design tests for its branches, it is white box. The level of testing (unit, integration, system) and the strategy (black or white box) are separate choices.

Exam radar: Order: black box first, then white box. Reason: bias. Knowing the code makes you think like the developer.

Black box is limited by exhaustive input testing. White box is limited by exhaustive path testing.

Common mistake: writing “white box is better because it sees more”. Seeing the code is exactly what brings the developer’s bias, and white box testing cannot find missing features at all. The two strategies complement each other; neither replaces the other.


Worked Examples

Exercise 1

Why do we usually do black box testing before white box testing?

Show solution ↓
Solution

Because of bias. Seeing the code makes the tester think like the developer, and the developer is biased toward believing the program works. A tester who has read the code tends to test what the code does rather than what it should do.

So we start with black box tests while the mind is still free, and only when we run out of ideas do we open the box and add white box tests.

Exercise 2

Name the impossible ideal that limits each strategy, and give one reason it is impossible.

Show solution ↓
Solution
  • Black box is limited by exhaustive input testing: trying every possible input. It is impossible because the input space is huge (the calculator already needs 4,000,000 tests), and because the order of inputs matters too, as with the TV remote’s power button that turns the TV on or off depending on what happened before.
  • White box is limited by exhaustive path testing: following every path through the code. It is impossible because loops repeat paths again and again, creating a new path for every number of repetitions.
Exercise 3

A team claims: “We tested every path through our code, so the program has no bugs left.” Explain why this claim is wrong even if every path really was tested.

Show solution ↓
Solution

Path testing checks only the code that exists. Every path can behave exactly as written and the program can still be wrong:

  • a requirement may never have been coded at all, so there is no path for it to test;
  • the code may do what the programmer meant, but not what the user needed.

In both cases the user is still unhappy, which is bug definition 5 (1.6). So even complete path testing cannot show the program has no bugs; it only shows the existing paths behave as coded.

Exercise 4

For each test idea, say whether it is black box or white box:

  1. Read the specification of a login page and test a wrong password three times.
  2. Read the code and notice an else branch that no test reaches, then write a test for it.
  3. Call sqrt(16) and check the result is 4, without opening the method.
  4. Choose inputs so that a while loop runs zero times, once, and many times.
Show solution ↓
Solution
  1. Black box: designed from the specification, through the interface.
  2. White box: designed from the code’s logic (a branch).
  3. Black box: a unit test through inputs and outputs only. This shows that unit testing can be black box.
  4. White box: designed from the loop structure, which you can only see in the code.
Exercise 5

Is unit testing black box or white box testing? Explain.

Show solution ↓
Solution

It can be either. Unit testing is a level of testing (one small unit, such as a method or class), while black box and white box are strategies.

  • If the tests use only the unit’s inputs and outputs, the unit testing is black box.
  • If the tester reads the unit’s code and designs tests for its branches and conditions, it is white box.
Exercise 6

A media player has one Play/Pause button. A tester checks that pressing it once starts the music, and concludes the button is fully tested. Using the idea of exhaustive input testing, explain what is missing.

Show solution ↓
Solution

The button’s effect depends on what happened before, exactly like the TV remote’s power button: when music is playing it should pause, and when it is paused it should play. So the input “press the button” does not have one expected result; the expected result depends on the earlier sequence of inputs.

A fuller test set must try the button in different states, at least: pressed while stopped (→ play), pressed while playing (→ pause), pressed while paused (→ play again). This is why exhaustive input testing is even more impossible than counting inputs suggests: the order of inputs matters too.