#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Testing Techniques

White Box Testing

What is White Box Testing?

White box testing (also called structural or glass-box testing) examines the internal code structure, logic, and paths of an application, rather than just its outputs.

Key Characteristics

  • Requires programming knowledge to design test cases based on the code itself.
  • Focuses on statement coverage, branch coverage, and path coverage.
  • Typically performed by developers during unit and integration testing.
  • Helps optimise code, remove dead code, and uncover hidden logical errors.

Common White Box Techniques

  • Statement coverage testing.
  • Branch/decision coverage testing.
  • Path coverage testing.
  • Loop testing.

Designing Tests from a Real Function

White box test design starts by reading the code and listing every decision. Consider this small Java method:

public String grade(int marks) {
    if (marks < 0 || marks > 100) {
        return "Invalid";
    }
    if (marks >= 40) {
        return "Pass";
    }
    return "Fail";
}

Now walk through it step by step:

  1. Draw the flow: there are two if decisions and three possible exits.
  2. Find the cyclomatic complexity: with two decisions, complexity is 2 + 1 = 3, so at least three independent paths exist.
  3. Pick inputs for each path: −5 returns "Invalid", 75 returns "Pass", and 20 returns "Fail".
  4. Check condition coverage: the first decision has two conditions joined by OR. The value −5 only makes the left side true, so add 150 to make the right side true as well.
  5. Add boundaries: 0, 39, 40 and 100 catch off-by-one mistakes such as writing > instead of >=.

Coverage Levels Compared

Coverage typeWhat must be exercisedStrength
StatementEvery line runs at least onceWeakest; misses untaken branches
Branch / decisionEvery true and false outcomeCatches missing else logic
ConditionEvery sub-condition true and falseFinds faulty compound conditions
PathEvery route through the codeStrongest, but explodes with loops

Full branch coverage always gives full statement coverage, but the reverse is not true. That is why teams usually set their target on branches rather than lines.

Measuring Coverage with Tools

You rarely calculate coverage by hand on real projects. Tools instrument the code while tests run and produce a report showing covered and missed lines and branches.

  • Java: JaCoCo, usually added as a Maven or Gradle plugin.
  • Python: coverage.py, often run through pytest-cov.
  • JavaScript: Istanbul, built into Jest via the --coverage flag.
mvn clean test jacoco:report
# open target/site/jacoco/index.html to see missed branches

Red or yellow markers in the report point straight to branches no test has taken, which become your next test cases.

Where White Box Testing Falls Short

Structural tests only check the code that exists. If a requirement was never implemented, no coverage tool will notice the gap. That is why white box work is paired with black box testing based on specifications.

  • High coverage does not mean correct behaviour; a test can execute a line without asserting anything useful.
  • Chasing a coverage percentage can produce shallow tests written only to turn lines green.
  • Tests tied tightly to internal structure may break during harmless refactoring.

Practical checklist: map decisions, compute complexity, cover every branch, split compound conditions, add boundary values, run a coverage tool, and make sure every test has a meaningful assertion.

Ready to master real-world software testing?

Learn manual and automation testing hands-on with mentor-led sessions.

Explore Course