Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 60 ยท Testing Fundamentals

Black-Box, White-Box and Grey-Box Testing

Compare black-box, white-box and grey-box testing: what the tester knows, which techniques each uses, strengths, limits and when to combine them.

You can test a vending machine in two ways. You can walk up, press buttons and see what drops out. Or you can open the back, look at the wiring and check that every switch does what the diagram says. Both find faults, but different ones. In software, these viewpoints are called black-box and white-box testing, with grey-box in between. The names describe one thing only: how much the tester knows about the inside of the thing under test.

This lesson defines the three, links each to the design techniques you will meet later, and shows a runnable example where the same function is tested from outside and from inside.

Black-box testing

In black-box testing, the tester treats the software as a closed box. Only the inputs and the outputs are visible, and tests come from requirements, specifications, user stories and common sense, never from the source code.

Typical techniques:

Strengths: it needs no programming knowledge, it takes the user's point of view, and it finds missing or wrong behavior relative to the requirements. It is also independent: the tester is not influenced by how the code was written.

Limits: you cannot know which paths of the code your tests exercised, so some paths may never run. You might also write many tests that all hit the same code.

Black-box testing is used at every level, but dominates system and acceptance testing (see Test Levels).

White-box testing

In white-box testing (also called glass-box, clear-box or structural testing), the tester sees and uses the internal structure: source code, branches, data flow, architecture. Tests are designed to exercise the code, and the measure of success is coverage of it.

Typical techniques are statement, decision and condition coverage, and path testing, explained in White-Box Testing and Code Coverage. A white-box tester may also review code, check error handling branches, look for dead code and examine how data moves through variables.

Strengths: it finds hidden mistakes in logic, reveals untested code and helps optimize it. Limits: it needs programming skills and access to the source, and it cannot discover missing features: if a requirement was forgotten in the code, code-based tests will not mention it either.

White-box testing is most common in unit testing and in parts of integration testing, usually by developers.

Grey-box testing

Grey-box testing blends the two. The tester does not have (or does not use) the full source code, but knows something about the internals: the database schema, the API contract, the architecture, the log format, or how sessions are stored. That partial knowledge is used to design smarter black-box tests.

Examples:

  • Knowing that the app caches the product list for five minutes, a tester updates a price and checks both the immediate page and the page after the cache expires.
  • Knowing the table structure, a tester submits a form and then verifies the row in the database (see API and Database Checks).
  • Reading an API specification to craft requests that break validation, then checking the server's log for the stack trace.
  • In security testing, a tester given a normal user account and the architecture diagram tries to reach other users' data.

Grey-box testing is natural for integration testing, web service and API testing, and security testing. It aims to get the realism of black-box with the sharper focus of white-box.

flowchart LR
  subgraph BB["Black box"]
    I1["Input"] --> X1["???"] --> O1["Output"]
  end
  subgraph GB["Grey box"]
    I2["Input"] --> X2["partly known:<br/>schema, API, logs"] --> O2["Output"]
  end
  subgraph WB["White box"]
    I3["Input"] --> X3["all code and<br/>structure visible"] --> O3["Output"]
  end

The same system, with decreasing mystery from left to right.

Side-by-side comparison

Black box Grey box White box
Knowledge of internals None Partial Full
Tests come from Requirements, behavior Requirements plus design knowledge Code and structure
Typical testers Testers, users, analysts Testers with technical skills Developers, SDETs
Main levels System, acceptance Integration, API, security Unit, some integration
Measure Requirement and risk coverage Both Code coverage
Finds Missing and wrong behavior Interface and data problems Logic mistakes, dead code
Misses Untested code paths Both gaps in part Missing features

None of these is better. A good strategy combines them: black-box tests answer "does it do what users need?", white-box coverage reports answer "which code did we never run?", and grey-box checks verify what happens behind the screen.

Try it: the same function from two angles

A shipping-fee function: orders under 50 pay 5; from 50 up to but not including 100 pay 2; 100 and above ship free; a negative total is invalid. The black-box tester has only that sentence. The white-box tester has the code and writes tests so that every branch runs.

JS Black-box and white-box tests of one function

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The white-box view reveals that the negative-total branch was not hit by these three inputs, even though the black-box list covered it. Conversely, a white-box tester who never read the requirement could happily cover every branch with a function that charged the wrong fee. Each view catches what the other cannot.

Try it: grey-box with a visible data store

Here the tester knows the app stores sessions in a map and expires them after 30 minutes. That knowledge shapes the test.

JS A grey-box expiry check

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the color-box naming (black, white, grey) describe?

    The terms describe visibility into the internals: none for black, full for white, partial for grey.
  2. 2. Which technique is typically black-box?

    Boundary value analysis is derived from the specification of inputs, without looking at code. The others use code structure.
  3. 3. A tester reads the API contract and checks the database row after submitting a form. This is mainly:

    Using partial knowledge of the design (API contract, schema) to design and verify tests is grey-box testing.
  4. 4. What can white-box testing NOT find on its own?

    Tests derived from code can only exercise what exists. Missing functionality is found by testing against requirements.

Summary

  • Black-box tests derive from behavior and requirements; white-box tests derive from code structure; grey-box tests use partial inside knowledge.
  • Black-box finds missing and wrong behavior but cannot measure code coverage; white-box finds logic mistakes but not missing features.
  • Grey-box suits integration, API and security testing.
  • Real projects combine all three views at different levels.

Next up: Alpha, Beta and User Acceptance Testing.