4.1. White-Box Testing

4.1.1. Logic-Coverage Testing

White-box testing is concerned with the degree to which test cases exercise or cover the logic (source code) of the program. As we saw in Chapter 2, the ultimate white-box test is the execution of every path in the program, but complete path testing is not a realistic goal for a program with loops.

If you back completely away from path testing, it may seem that a worthy goal would be to execute every statement in the program at least once. Unfortunately, this is a weak criterion for a reasonable white-box test. This concept is illustrated in Figure 4.1 Assume that Figure 4.1 represents a small program to be tested. The equivalent Java code snippet follows:

public void foo(int a, int b, int x) {
public void foo(int a, int b, int x) {
    if (a>1 && b==0) {
        x=x/a;
    }
    if (a==2 || x>1) {
        x=x+1;
    }
}

You could execute every statement by writing a single test case that traverses path ace. That is, by setting A=2, B=0, and X=3 at point a, every statement would be executed once (actually, X could be assigned any value).

Unfortunately, this criterion is a rather poor one. For instance, perhaps the first decision should be an or rather than an and. If so, this error would go undetected. Perhaps the second decision should have stated X>0; this error would not be detected. Also, there is a path through the program in which X goes unchanged (the path abd). If this were an error, it would go undetected. In other words, the statement-coverage criterion ...

Get The Art of Software Testing, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.