Key takeaways
Measure branch coverage, not just line coverage, because one test can run every line of a function and still skip a branch.
Branch coverage needs both outcomes of every decision tested, and the outcome nobody tested is where bugs hide.
Have the developers who write the code write its white-box tests, in the same pull request.
They know its branches and edge cases best, and unit tests run in seconds on every change.
Use the coverage report to find untested code, not as a score to push to 100%.
The paths through a program multiply without end, and a line counts as covered when it runs, even if no test checks what it did.
Design tests from the code's structure, but assert on what the code returns, so refactoring doesn't break them.
Tests that check internal details have to be rewritten whenever the code changes shape, even when its behavior hasn't.
Run mutation testing on your most critical code to find tests that run code without checking it.
A mutant that survives means the tests would still pass if that code were wrong.
White-box testing is software testing designed from knowledge of the code's internals, where the person writing the tests reads the source and picks inputs that run its statements, branches, conditions, and paths. It's also called clear-box, glass-box, or structural testing, and developers do most of it, as unit tests for the code they write.
The point is to check the code's logic from the inside, including error handling and edge cases that users rarely reach and that a tester working from the outside can't see. Teams measure it with code coverage, the share of the code's statements or branches that ran during the tests, which shows what the tests ran but not whether they checked the right things.
What is white-box testing?
White-box testing means testing a piece of code with its source open in front of you. Instead of starting from what the software should do for a user, the tester starts from how the code is written (its functions, decisions, loops, and the data that moves through them) and writes a test for each route the code can take. The box is the program, and white means the tester can see inside it.
Software testing is often sorted by how much of the code the person testing can see. Black-box testing checks what software does from the outside, feeding in inputs and checking the outputs, without looking at the code, and most functional testing works that way. Gray-box testing sits in between, using some knowledge of the internals to design sharper functional or integration tests.
Who does white-box testing, and at which levels?
Developers do most white-box testing, on the code they write, because they're the ones who know it. It happens mostly at the unit level, sometimes in integration tests, and without running the code at all in code review and static analysis. Security teams use the term for a related job, a security test with full access to the code.
Developers write white-box unit tests
A unit test checks one function, class, or component on its own, in milliseconds, with anything it depends on replaced by a stand-in. The developer who wrote the function knows its decisions and edge cases, so they write a test for each one, usually in the same pull request as the code. Most of the code coverage numbers a team tracks come from its unit tests.
Integration tests can be white-box or black-box
Integration tests check that two or more parts of an app work together, such as a service and its database. They're white-box when they're designed from how the parts connect in the code (which module calls which, with what data, and what happens when a call fails), and black-box when they only check that the right outputs come back from the right inputs.
Code review and static analysis are static white-box testing
Static white-box testing examines the code without running it. In code review, walkthroughs, and inspections, people read the change looking for mistakes. Static analysis tools, such as linters, type checkers, and security scanners, flag suspicious code automatically. It's the earliest white-box check there is, since it works on code before anyone has written a test for it.
Security teams run white-box security tests
In security, white-box testing means an assessment where the testers get the source code, architecture, and credentials, as in a white-box penetration test or a scan of the source code for vulnerabilities. It's the same idea, applied to finding security holes instead of logic errors.
System testing, end-to-end tests, and user acceptance testing are almost always black-box. They check the whole app from the outside, the way a user would, and the people running them don't need to read its code.
What are the white-box testing techniques?
The main white-box testing techniques are statement coverage, branch coverage, condition coverage, path coverage, loop testing, data-flow testing, and mutation testing. The first four are coverage criteria, each stricter than the one before, and the other three check loops, variables, and the tests themselves.
Most of them work from the code's control flow, the routes execution can take through a function. Drawn as a control flow graph, each block of statements that always runs together is a box, and each way execution can move between them (the two outcomes of an if, a loop going round again) is an arrow. Each coverage criterion asks the tests to run more of that graph.
| Technique | What the tests have to do | What it catches |
|---|---|---|
| Statement coverage | Run every statement at least once | Code that no test runs at all |
| Branch coverage | Take both outcomes of every decision | Bugs in an outcome no test took, such as a missing else |
| Condition coverage | Make each true-or-false part of a decision come out both ways | A wrong check hidden inside a compound condition |
| Path coverage | Run every route through a function from start to finish | Bugs that appear only when decisions combine in a certain order |
| Loop testing | Run each loop zero times, once, many times, and at its limits | Off-by-one errors and loops that never end |
| Data-flow testing | Follow each variable from where it's set to where it's used | Values read before they're set, or overwritten before they're read |
| Mutation testing | Fail when the code is changed on purpose | Tests that run code without checking its results |
Statement coverage
Statement coverage requires every statement in the code to run at least once, and coverage tools often report it as line coverage. It's the weakest of the criteria, since a test can run every line of a function without ever trying the other outcome of a decision in it. It's still useful for spotting code that no test reaches at all.
Branch coverage
Branch coverage, also called decision coverage, requires every decision in the code to come out both ways, so each if is tested both true and false and each case of a switch runs. Taking every branch runs every statement, so branch coverage includes statement coverage, and it also catches bugs in the outcome a test never took, like an if with no else that leaves a variable unset. It's the coverage number most worth tracking.
Condition coverage
Condition coverage looks inside each decision. In if (isAdmin || isOwner), each of the two conditions has to come out true in one test and false in another. That can still miss a branch. Tests with an admin who isn't the owner and an owner who isn't an admin cover both conditions both ways, but the decision comes out true both times, so the code for everyone else never runs. Condition/decision coverage closes that gap by requiring both. Modified condition/decision coverage (MC/DC) goes further, with tests that show each condition can flip the decision on its own, and DO-178C, the standard for airborne software, requires it for software whose failure would be catastrophic.
Path coverage
Path coverage requires every route through a function, from start to finish, to run. It's the strongest criterion and rarely practical. The number of paths doubles with each decision in a row (10 if statements in sequence make 1,024 paths), and a loop makes it endless, since every extra pass is another path, so no suite covers every path through a real program. Basis path testing is the practical version. It tests a set of independent paths whose count is the function's cyclomatic complexity, the number of decisions plus one, so those 10 if statements need 11 tests instead of 1,024.
Loop testing
Loop testing checks each loop at the counts where loops tend to break. For a loop that runs up to some limit, test it skipped entirely, run once, run twice, run a typical number of times, and run one below, at, and one past the limit. Nested loops get the same treatment one at a time, starting with the innermost while the outer loops run their minimum. It catches off-by-one errors, crashes on empty input, and loops that never end.
Data-flow testing
Data-flow testing follows each variable from the places it's set (its definitions) to the places it's read (its uses), and writes tests that run each route from a definition to a use. It catches what branch coverage can miss on a particular route through the code, such as a variable read before anything sets it, or a value overwritten before anything reads it.
Mutation testing
Mutation testing checks the tests instead of the code. A mutation testing tool makes small changes to the code, one at a time (flipping < to <=, swapping + for -, deleting a line), and runs the tests against each changed version, called a mutant. If a test fails, the mutant is killed. If every test passes, the mutant survives, which means the tests ran that code without checking what it does. The share of mutants killed, the mutation score, says more about how good the tests are than coverage does. It's slow, though, since the suite runs once for every mutant, so teams save it for their most critical code.
White-box testing example: one test runs every line and still misses a bug
Here's a function that greets a user by their nickname.
function greeting(user) {
let name;
if (user.nickname) {
name = user.nickname;
}
return "Hi, " + name.trim();
}
A white-box tester reads the function and sees one decision, the if, with two outcomes. A test for a user with a nickname runs every statement, so it reaches 100% statement coverage, and it passes.
// Test 1: a user with a nickname runs every statement.
assert.equal(greeting({ nickname: " Sam " }), "Hi, Sam");
Branch coverage counts the if as two branches, and Test 1 took only one of them, so branch coverage is at 50%. Covering the other branch takes a test for a user without a nickname, and that test fails, because name never gets set and the function crashes when it calls .trim() on it.
// Test 2: a user without a nickname takes the false branch.
assert.equal(greeting({}), "Hi, there");
// TypeError: Cannot read properties of undefined (reading 'trim')
Starting name at "there" fixes the bug, and both tests pass. The crash lived in the outcome of the if that has no code of its own, which statement coverage never counts and branch coverage does.
What white-box testing catches, and what it misses
White-box testing catches logic errors, untested code, and dead code early, close to the change, and it points straight at the code that's wrong. It misses anything that isn't in the code, like a requirement nobody built, and it shares the blind spots of the developer who wrote the code.
Strengths of white-box testing
- It tests the code users rarely reach: Error handling, edge cases, and unusual branches get tested on purpose, because the tester can see they're there.
- It runs early and fast: Unit tests run in milliseconds and don't need a working interface, so they can run on every change.
- It points to the cause: A failing unit test names the function and the input that broke it, so the fix starts where the bug is.
- It finds dead code: Code that no input can reach shows up as uncovered, and it can be deleted.
- Its coverage can be measured: A coverage tool reports exactly which statements and branches ran, so the gaps are visible.
Limits of white-box testing
- It needs the code and someone who can read it: Without access to the source, or the skills to program, nobody can design tests from it.
- It can't find what isn't there: A requirement nobody built has no code to cover, so white-box tests can't notice it's missing.
- It shares the author's blind spots: A developer who misread the requirement writes the code and its tests from the same misreading, and both agree with each other.
- Tests tied to the code break when the code changes: Tests that check internal details fail after a refactor that didn't change what the code does.
- High coverage can mislead: A line counts as covered when it runs, even if no assertion checks what it did.
- Exhaustive testing is impossible: The paths through a program have no end, so white-box testing picks which routes to test instead of testing them all.
How do you do white-box testing well?
Do white-box testing well by testing every outcome of every decision in the code you change, asserting on what the code returns, and using branch coverage to find what's missing rather than to hit a number. Then check the tests themselves with mutation testing where the code matters most, and run them on every change.
1. List the decisions in the code
Read the function and note each decision, compound condition, and loop, plus each place it can fail, such as a call that can throw an error or an input that can be empty. Those are the outcomes the tests need to reach. For a complicated function, sketching its control flow graph makes the routes easier to count.
2. Write a test for each outcome
Give each decision a test for each way it can go, each loop a test at zero, one, and many passes, and each input a test at its boundaries, such as an empty list, zero, or the longest value allowed. Test the error paths on purpose, since nothing else in a normal run exercises them.
3. Assert on outputs, not internals
Design the tests from the code's structure, but check what the function returns, what it saves, and what it sends to other parts of the system, not which private helper it used along the way. Then a refactor that keeps the behavior keeps the tests passing, and a failing test means the behavior changed.
4. Measure branch coverage and read the gaps
Run the tests with a coverage tool for your language, and look at branch coverage as well as line coverage. Check how the tool counts branches, since some don't count an if with no else as two branches and would show the greeting function above as fully covered after Test 1. Then read the uncovered code. Each gap is either a test to write or dead code to delete. A coverage minimum that blocks a merge when coverage drops keeps it from sliding, but don't make 100% the goal, because chasing the last few percent produces tests written to run lines rather than to check behavior.
5. Check the tests with mutation testing
On the code where a bug would hurt most, such as the code that handles money or permissions, run a mutation testing tool and look at the mutants that survive. Each one points to an assertion to add or tighten.
6. Run the tests on every change
Run unit tests, static analysis, and code review on every pull request, as part of continuous testing, so a broken branch gets caught while the change is still fresh. Unit tests take seconds, so they never need to wait for a nightly run, and every test you add keeps checking for a regression on every change after that.
White-box tests can't show that a user can finish a flow
White-box tests check each function, class, or service against what its code is meant to do, usually one piece at a time with the rest replaced by stand-ins. They can't show whether a user can finish a flow, such as signing up or exporting a report, when the frontend, backend services, database, and third-party services run together in a real browser or on a real phone. That takes black-box end-to-end tests, which run each flow through the app's real interface, the way a user would.
QA Wolf's AI agents write and maintain those end-to-end tests, in open-source Playwright for web apps and Appium for mobile and desktop apps, and white-box testing stays with the developers who own the code. Sign up and tell the agent "build me a test plan," and you'll get one, ready for you to choose which flows get tests first. When an agent needs an answer, such as whether a change was intended, it asks you. QA Wolf measures test coverage as the share of your app's practical user flows with a passing end-to-end test, not the share of lines that ran. From QA Wolf's docs and engineering posts:
- The agent maps your app's user flows: It explores your app, maps its end-to-end workflows by feature or product area, and writes each one up as an Arrange-Act-Assert outline.
- It writes tests as code you own: The tests are standard Playwright for web apps and Appium for iOS, Android, Windows, and macOS apps, so your team can read, change, and version them like any other code.
- Every test runs at once: Each test gets its own browser, device, or VM, so the suite finishes in about the time of its longest test.
- Every failure gets investigated: QA Wolf's AI sorts each failure into a product bug, an out-of-date test, or a flake (a test that passes and fails with no change to the code), and real bugs are reported to you with a recording, logs, and steps.
- The agents keep the tests working: When your product changes, they update the tests it breaks, with unlimited repairs.
Start now and ask the agent to build a test plan for your app, starting with your most critical flows, or talk to sales.
Frequently asked questions
What is white-box testing?
White-box testing is software testing designed from knowledge of the code's internals, where the tester reads the source and writes tests that run its statements, branches, conditions, and paths. It's also called clear-box, glass-box, or structural testing. Developers do most of it, as unit tests, and measure it with code coverage.
Who performs white-box testing?
Developers perform most white-box testing, on the code they write, since they know its logic and can write its unit tests in the same pull request. QA engineers who can program write some white-box tests too, and security teams run white-box security tests with full access to the source code.
Which levels of testing use white-box testing?
White-box testing happens mostly at the unit level, where each function or component is tested on its own, and in integration tests designed from how the parts connect in the code. Code review and static analysis are white-box testing too, done without running the code. System, end-to-end, and acceptance tests are almost always black-box.
Is unit testing white-box testing?
Usually, yes. Developers write unit tests for their own code and design them to run each branch and edge case of a function, which makes them white-box tests. A unit test written only from a function's documented inputs and outputs, without reading its code, is a black-box test.
What is an example of white-box testing?
A function with one
ifstatement is the classic example. A test that takes the true branch can run every line of the function, reaching 100% statement coverage, while a bug sits on the false branch, such as a variable that never gets set. White-box testing reads the code, sees the second branch, and adds a test for it, which is what branch coverage requires.What is static white-box testing?
Static white-box testing examines the code without running it, through code review, walkthroughs, inspections, and static analysis tools such as linters and type checkers. It finds mistakes before any test runs, when they're cheapest to fix, including code no input can reach and patterns known to cause bugs. Dynamic white-box testing runs the code, with tests designed to cover its branches and paths.
What is the difference between white-box testing and debugging?
White-box testing looks for failures, with tests designed from the code to run its branches and edge cases. Debugging starts from a failure someone has already found, traces it back through the code to its cause, and fixes it. A failing white-box test is often where debugging starts.
What is white-box penetration testing?
White-box penetration testing is a security test where the testers get full information about the system, including its source code, architecture, and credentials, before they try to break in. It lets them check every part of the code for vulnerabilities, while a black-box penetration test, which starts with no inside knowledge, shows what an outside attacker could find.