Book a free call

White Box Testing: What It Is and When to Use It

White box testing checks software from the inside, using knowledge of the code to test paths and branches. Learn the main techniques, when it helps, and its limits.

Hertiv engineering teamReviewed by a senior engineer
7 min read
White Box Testing: What It Is and When to Use It
On this page

Short answer

White box testing is a way of testing software in which the tester knows the internal code and structure, and writes tests that exercise specific paths, branches and conditions in that code. It is used to find logic errors, check that each part of the code runs as intended, and measure how much of the code the tests actually cover.

What is white box testing?

White box testing is testing software with the code in view. The tester knows how the code is written, which paths it can take, and which conditions matter, and writes tests that drive the code through those paths. It is also called clear box testing or structural testing, because the structure of the code is part of the test.

A simple example makes the idea clear. A function that applies a discount might have one path for members, one for non-members, and one for an expired code. A white box test would run the function with inputs that cover each of those paths, and check that each one gives the right result. A tester who could not see the code might miss the expired-code path altogether.

White box testing is usually done by developers, or by testers who can read code, because it needs the source. It sits alongside other kinds of testing rather than replacing them: it checks the inside of the code, while other methods check what the user sees. Custom software development teams use it as part of a wider testing mix, not as the only check.

How is white box testing different from black box testing?

Black box testing checks software without looking at the code. The tester gives inputs, watches the outputs, and checks them against what the software should do. White box testing does the opposite: it starts from the code and checks that the code itself behaves as intended. The two methods find different kinds of faults, which is why teams use both.

AreaWhite box testingBlack box testing
What the tester seesThe code and its structureOnly inputs and outputs
What it findsLogic errors and untested pathsWrong behaviour from the user's view
Who usually does itDevelopers or code-reading testersTesters, often without code access
When it runsDuring development, near the codeAt any stage, including after release
Main limitCannot tell if a feature is missingCannot tell if a code path was missed

The limit in the last row matters. White box testing can show that every line of code runs, but it cannot show that a feature the user needs was never built. Black box testing can show that a feature is missing, but it cannot show that a rare code path was never run. Used together, each covers the other's blind spot.

What techniques does white box testing use?

The main white box techniques are named by what they measure: statement coverage, branch coverage and path coverage. Statement coverage asks whether each line of code ran during the tests. Branch coverage asks whether each decision, such as an if or a loop, was taken both ways. Path coverage asks whether each route through the code was taken, which is the strictest of the three.

In practice, teams usually aim for statement and branch coverage, and use path coverage only for small, important functions, because the number of paths grows fast as code gets larger. Other techniques include condition coverage, which checks each part of a compound condition, and mutation testing, in which small changes are made to the code to check that the tests catch them.

  • Statement coverage. Every line runs at least once. A useful baseline, but a weak one on its own.
  • Branch coverage. Every decision is taken both ways. Catches missing else branches and loop edges.
  • Path coverage. Every route through the code is taken. Thorough, but often impractical for large functions.
  • Condition coverage. Each part of a compound test is checked true and false.

Coverage numbers are a guide, not a proof. A test suite can cover every line and still miss a wrong calculation, if the test checks the wrong expected value. Coverage tells a team where the tests have not been, and that is its real value.

How do you run white box tests in practice?

White box testing usually starts with the smallest units of code: functions and methods. A developer writes a unit test for each function, giving it inputs that cover the paths the function can take, and checking the outputs. Those tests run every time the code changes, so a new change that breaks an old path is caught quickly.

  1. Pick the unit. Start with a function that has clear inputs, outputs and a few branches.
  2. List the paths. Read the code and note each branch, loop and error case.
  3. Write a test per path. Give each test an input that drives the code down one path, and check the result.
  4. Measure coverage. Run the tests with a coverage tool and look for lines and branches that never ran.
  5. Add tests for the gaps. Write tests for the uncovered paths, or simplify the code if a path cannot be reached.
  6. Run them on every change. Keep the tests in the same repository as the code, so they run with each build.

The tests should be kept close to the code they test, in the same repository, so that a change to the code and a change to the test travel together.

When a test fails, the team should treat it as a signal to check, not as a nuisance to delete. DevOps services teams often wire these tests into the build, so a failing test blocks a release until someone looks at it.

Example (hypothetical): measuring coverage on one function

Example (hypothetical): a function has 10 lines of code and three branches: a discount for members, a discount for a promo code, and an error for an expired code. The team writes tests for the member branch and the promo branch, but not for the expired code. The tests run 8 of the 10 lines, so statement coverage is 8 divided by 10, or 80%.

The two lines that never run are the error branch for the expired code. The team adds one test that passes an expired code and checks that the function returns the error. That test runs the last 2 lines, so the total becomes 10 divided by 10, or 100% statement coverage.

The arithmetic is simple, but the point is the habit: coverage shows the team which lines were never run, and the missing test is written for those lines.

Branch coverage for the same function needs more care. Each branch must be taken both ways, so the team also checks the case where the member test fails and the case where the promo test fails. If a branch is only ever taken one way, the test suite has a gap, even if every line ran. That is why teams look at branch coverage as well as statement coverage.

What are the common mistakes?

The most common mistake is treating coverage as a target instead of a guide. A team that aims for a high coverage number can end up writing tests that run code without checking it properly, which raises the number and adds little safety. A test that runs a line but does not check the result is a weak test, whatever the coverage report says.

  • Chasing a coverage number. A high number with weak checks is worse than a lower number with strong checks.
  • Testing the code, not the need. White box tests can miss a missing feature, because the code never had it.
  • Writing brittle tests. Tests that depend on the exact shape of the code break on every refactor, even when behaviour is unchanged.
  • Skipping the error paths. Happy-path tests are easy; the error and edge cases are where faults hide.
  • Leaving tests out of the build. Tests that do not run on every change go stale, and stale tests mislead.

Another mistake is to use white box testing alone and skip the user's view. A function can pass every white box test and still be wrong for the user, if the requirement was misunderstood. That is why teams pair white box tests with tests that check behaviour from the outside, and with a review of the requirements before the code is written.

When should you use white box testing, and when should you not?

White box testing is most useful for code where a fault would be costly or hard to find from the outside: payment logic, access control, data handling and the core rules of a product. In those places, testing each path is worth the effort, because a missed branch can mean a wrong charge, a security hole or lost data.

It is less useful for checking design, wording or how a feature feels to use, because those are questions about the outside of the product, not the code.

It is also a poor fit for code that changes every day, because the tests have to change with it. In that case, a smaller set of white box tests on the stable core, plus broader tests from the outside, is a better mix.

A practical rule is to use white box testing where the code is stable and the cost of a fault is high, and to use black box testing where the question is what the user sees.

Most teams end up with both, and the mix changes as the product grows. Cybersecurity services work follows the same split: code-level checks find logic faults, while checks from the outside find what an attacker would see.

Key takeaways

  • White box testing checks code from the inside, using knowledge of its paths and branches.
  • Statement, branch and path coverage measure how much of the code the tests actually ran.
  • Coverage is a guide to untested code, not proof that the code is correct.
  • White box tests miss missing features; black box tests miss untested paths, so teams need both.
  • Use white box testing most where the code is stable and a fault would be costly.

Frequently asked questions

What is the difference between white box and black box testing?

White box testing uses knowledge of the code to test its internal paths and branches. Black box testing checks inputs and outputs without looking at the code. White box finds logic errors; black box finds wrong behaviour from the user's view.

Do I need to know how to code to do white box testing?

Usually, yes. White box testing needs the tester to read the code, understand its branches and write tests for them. That is why developers often write white box tests, while testers who do not read code focus on black box testing.

What is a good coverage target?

There is no single right number. Coverage is a guide that shows which lines and branches the tests never ran. A team should aim for strong checks on important code, rather than chasing a high number with weak tests.

Is unit testing the same as white box testing?

Not quite. Unit testing is testing small units of code, and it is often done as white box testing, because the tester knows the code. But a unit test can also be written from the outside, without looking at the code, so the two ideas overlap rather than match exactly.

Can white box testing find security faults?

It can find some, such as a missing check on a branch that grants access. But it cannot find a missing security feature, or a fault that only shows from the outside. Security work needs both code-level checks and testing from an attacker's view.

When should white box testing be skipped?

Skip it, or keep it light, for code that changes every day and for questions about design or wording. In those cases, tests from the outside give more value for the effort, and white box tests can be added once the code settles.

Found this useful? Share it.
Hertiv engineering team

Written from projects we build for clients: software products in production. Every post is reviewed by a senior engineer before publishing and updated when practice changes.

About HertivHow we work

Need help testing a product properly?

A senior engineer replies within one business day. Or see our Custom Software Development service.

Book a free call

Keep reading

All posts

Tell us what you want to build

A senior engineer reads every request and replies within one business day with questions, a rough range, or a call link.

  • NDA on request before any details
  • You own all code, models and IP
  • US, UK and EU time-zone overlap

Your email app should open with your message ready to send. If it does not, write to [email protected].

Prefer email? Write to [email protected]
Estimate costBook a call