9 White Box Testing

WHITE BOX TESTING

 

White box testing is a testing technique, that examines the program structure and derives test data from the program logic/code. The other names of glass box testing are clear box testing,open box testing, logic driven testing or path driven testing or structural testing.White-box testing of software is predicated on close examination of procedural detail. Logical paths through the software are tested by providing test cases that exercise specific sets of conditions and/or loops. The “status of the program” may be examined at various points to determine  if  the  expected  or   asserted  status  corresponds  to  the  actual  status. It  inspects programmed behavior.

 

White box testing techniques

 

Statement coverage,   Branch coverage,   Path coverage,   Condition coverage  and Mutation testing

 

STATEMENT COVERAGE

 

Statement coverage is a white box test design technique which involves execution of all the executable statements in the source code at least once. It is used to calculate and measure the number of statements in the source code which can be executed given the requirements.

Using this technique we can check what the source code is expected to do and what it should not.It can also be used to check the quality of the code and the flow of different paths in the program.The main drawback of this technique is that we cannot test the false condition in it.

(Statement coverage = No of statements Executed/Total no of statements in the source code * 100)

Statement Coverage Criterion

 

–   Observing that a statement behaves properly for one input value

 

–    No guarantee that it will behave correctly for all input values

 

Example 1:

 

Read A

 

Read B

 

if A>B

 

Print “A is greater than B”

 

else

 

Print “B is greater than A”

 

endif

 

Set1 :If A =5, B =2

 

No of statements Executed: 5

 

Total no of statements in the source code: 7

 

Statement coverage =5/7*100 = 71.00 %

 

Set1 :If A =2, B =5

 

No of statements Executed: 6

 

Total no of statements in the source code: 7

 

Statement coverage =6/7*100 = 85.20 %

 

This is purely a white box testing method. It tests the software’s internal coding and infrastructure and so the programmer is the one who should take the initiative to do this. This technique is very suitable for drupal programmers and other programmers.

 

 

Example 2: Euclid’s GCD Algorithm

 

int f1(int x, int y){

 

while (x != y){

 

if (x>y) then

 

x=x-y;

 

else y=y-x;

}

 

return x;

}

By choosing the test set {(x=3,y=3),(x=4,y=3), (x=3,y=4)} all statements are executed at least once.

 

Branch Coverage

 

In the branch coverage-based testing strategy, test cases are designed to make each branch condition to assume true and false values in turn. Branch testing is also known as edge testing as in this testing scheme, each edge of a program’s control flow graph is traversed at least once. It is obvious that branch testing guarantees statement coverage and thus is a stronger testing strategy compared to the statement coverage-based testing.

 

 

Test cases are designed such that:

 

– Different branch conditions is given true and false values in turn.

 

Branch testing guarantees statement coverage:

 

– a stronger testing compared to the statement coverage-based testing.

 

Example: For Euclid’s GCD computation algorithm

 

Test cases for branch coverage can be:

 

{(x=3,y=3), (x=4,y=3), (x=3,y=4)}

 

 

CONDITION COVERAGE

 

In this structural testing, test cases are designed to make each component of a composite conditional expression to assume both true and false values. For example, in the conditional expression ((c1.and.c2).or.c3), the components c1, c2 and c3 are each made to assume both true and false values. Branch testing is probably the simplest condition testing strategy where only the compound conditions appearing in the different branch statements are made to assume the true and false values. Thus, condition testing is a stronger testing strategy than branch testing and branch testing is stronger testing strategy than the statement coverage-based testing. For a composite conditional expression of n components, for condition coverage, 2ⁿ test cases are required. Thus, for condition coverage, the number of test cases increases exponentially with the number of component conditions. Therefore, a condition coverage-based testing technique is practical only if n (the number of conditions) is small.

 

PATH COVERAGE

The path coverage-based testing strategy requires us to design test cases such that all linearly independent paths in the program are executed at least once. A linearly independent path can be defined in terms of the control flow graph (CFG) of a program.

 

Control Flow Graph (CFG)

 

A control flow graph describes the sequence in which the different instructions of a program get executed. In other words, a control flow graph describes how the control flows through the program. In order to draw the control flow graph of a program, all the statements of a program must be numbered first. The different numbered statements serve as nodes of the control flow graph. An edge from one node to another node exists if the execution of the statement representing the first node can result in the transfer of control to the other node. The CFG for any program can be easily drawn by knowing how to represent the sequence, selection, and iteration type of statements in the CFG. After all, a program is made up from these types of statements.

 

How To Draw Control Flow Graph?

 

Number all the statements of a program.

 

Numbered statements:

 

–  Represent nodes of the control flow graph.

 

An edge from one node to another node exists:

 

–  If execution of the statement representing the first node can result in transfer of control to the other node.

 

Example

 

int f1(int x,int y){

 

while (x != y){

 

if (x>y) then

 

x=x-y;

 

else y=y-x;

}

return x;

}

Example CFG

 

A path through a program is a node and edge sequence from the starting node to a terminal node of the control flow graph of a program. There can be more than one terminal node in a program. Writing test cases to cover all the paths of a typical program is impractical. For this reason, the path-coverage testing does not require coverage of all paths but only coverage of linearly independent paths.

 

Independent Path

 

A linearly independent path is any path through the program that introduces at least one new edge that is not included in any other linearly independent paths. If a path has one new node compared to all other linearly independent paths, then the path is also linearly independent. This is because, any path having a new node automatically implies that it has a new edge. Thus, a path that is sub path of another path is not considered to be a linearly independent path.

 

McCabe’s Cyclomatic Metric

 

For more complicated programs it is not easy to determine the number of independent paths of the program. McCabe’s cyclomatic complexity defines an upper bound for the number of linearly independent paths through a program. Also, the McCabe’s cyclomatic complexity is very simple to compute. Thus, the McCabe’s cyclomatic complexity metric provides a practical way of determining the maximum number of linearly independent paths in a program. Though the McCabe’s metric does not directly identify the linearly independent paths, but it informs approximately how many paths to look for. There are two different ways to compute the cyclomatic complexity. The answers computed by the three methods are guaranteed to agree.

    Method 1:

 

Given a control flow graph G of a program, the cyclomatic complexity V(G) can be computed as: V(G) = E – N + 2 where N is the number of nodes of the control flow graph and E is the number of edges in the control flow graph.

 

For the CFG of example shown in fig.1, E=7 and N=6. Therefore, the cyclomatic complexity = 7-6+2 = 3.

 

Method 2:

 

An alternative way of computing the cyclomatic complexity of a program from an inspection of its control flow graph is as follows: V(G) = Total number of bounded areas + 1 In the program’s control flow graph G, any region enclosed by nodes and edges can be called as a bounded area. This is an easy way to determine the McCabe’s cyclomatic complexity. But, what if the graph G is not planar, i.e. however you draw the graph, two or more edges intersect? Actually, it can be shown that structured programs always yield planar graphs. But, presence of GOTO’s can easily add intersecting edges. Therefore, for non-structured programs, this way of computing the McCabe’s cyclomatic complexity cannot be used. The number of bounded areas increases with the number of decision paths and loops. Therefore, the McCabe’s metric provides a quantitative measure of testing difficulty and the ultimate reliability.

 

For the CFG example shown in fig.1, from a visual examination of the CFG the number of bounded areas is 2. Therefore the cyclomatic complexity, computing with this method is also 2+1 = 3. This method provides a very easy way of computing the cyclomatic complexity of CFGs, just from a visual examination of the CFG. On the other hand, the other method of computing CFGs is more amenable to automation, i.e. it can be easily coded into a program which can be used to determine the cyclomatic complexities of arbitrary CFGs.

An upper bound:

 

–   For the number of linearly independent paths of a program

 

Provides a practical way of determining:

 

–  The maximum number of linearly independent paths in a program. In this example the cyclomatic complexity value is 3. Therefore, 3 test cases are sufficient to test this program.

 

MUTATION TESTING

 

In mutation testing, the software is first tested by using an initial test suite built up from the different white box testing strategies. After the initial testing is complete, mutation testing is taken up. The idea behind mutation testing is to make few arbitrary changes to a program at a time. Each time the program is changed, it is called as a mutated program and the change effected is called as a mutant. A mutated program is tested against the full test suite of the program. If there exists at least one test case in the test suite for which a mutant gives an incorrect result, then the mutant is said to be dead. If a mutant remains alive even after all the test cases have been exhausted, the test data is enhanced to kill the mutant. The process of generation and killing of mutants can be automated by predefining a set of primitive changes that can be applied to the program. These primitive changes can be alterations such as changing an arithmetic operator, changing the value of a constant, changing a data type, etc. A major disadvantage of the mutation-based testing approach is that it is computationally very expensive, since a large number of possible mutants can be generated. Since mutation testing generates a large number of mutants and requires us to check each mutant with the full test suite, it is not suitable for manual testing. Mutation testing should be used in conjunction of some testing tool which would run all the test cases automatically.

 

CONCLUSION

  •  White Box Testing is performed to detect errors with respect to execution of complete program.
  •  Statement coverage and condition coverage are useful to test all lines of the program.
  • Path coverage tests all possible paths in the program execution.
  • Cyclomatic Complexity is useful for finding the paths.