Control Structure testing.
Control structure testing is a group of white-box testing methods.
- 1.0 Branch Testing
- 1.1 Condition Testing
- 1.2 Data Flow Testing
- 1.3 Loop Testing
1.0 Branch Testing
- also called Decision Testing
- definition: "For every decision, each branch needs to be
executed
at least once."
- shortcoming - ignores implicit paths that result from compound
conditionals.
- Treats a compound conditional as a single statement. (We count
each branch taken out of the decision, regardless which condition lead
to the branch.)
- This example has two branches to be executed:
IF ( a equals b) THEN
statement 1
ELSE
statement 2
END IF
- This examples also has just two branches to be executed, despite
the compound conditional:
IF ( a equals b AND c less than d ) THEN
statement 1
ELSE
statement 2
END IF
- This example has four branches to be executed:
IF ( a equals b) THEN
statement 1
ELSE
IF ( c equals d) THEN
statement 2
ELSE
statement 3
END IF
END IF
- Obvious decision statements are
if, for, while, switch
.
- Subtle decisions are return
boolean expression,
ternary
expressions, try-catch.
- For this course you don't need to write test cases for
IOException and OutOfMemory
exception.
- See this problem
and
solution.
1.1 Condition Testing
Condition testing is a test construction method that focuses on
exercising the logical conditions in a program module.
Errors in conditions can be due to:
- Boolean operator error
- Boolean variable error
- Boolean parenthesis error
- Relational operator error
- Arithmetic expression error
definition: "For a compound condition C, the true and false branches of
C and every simple condition in C need to be executed at least
once."
Multiple-condition testing requires that all true-false combinations
of simple conditions be exercised at least once. Therefore, all
statements, branches, and conditions are necessarily covered.
1.2 Data Flow Testing
Selects test paths according to the location of definitions and use of
variables. This is a somewhat sophisticated technique and is not
practical for extensive use. Its use should be targeted to
modules
with nested if and loop statements.
1.3 Loop Testing
Loops are fundamental to many algorithms and need thorough testing.
There are four different classes of loops: simple,
concatenated, nested, and unstructured.
Examples:
Create a set of tests that force the following situations:
- Simple Loops, where n is the maximum number of
allowable passes through the loop.
- Skip loop entirely
- Only one pass through loop
- Two passes through loop
- m passes through loop where m<n.
- (n-1), n, and (n+1) passes through the loop.
- Nested Loops
- Start with inner loop. Set all other loops to minimum values.
- Conduct simple loop testing on inner loop.
- Work outwards
- Continue until all loops tested.
- Concatenated Loops
- If independent loops, use simple loop testing.
- If dependent, treat as nested loops.
- Unstructured loops
public class loopdemo
{
private int[] numbers = {5,-3,8,-12,4,1,-20,6,2,10};
/** Compute total of numItems positive numbers in the array
* @param numItems how many items to total, maximum of 10.
*/
public int findTotal(int numItems)
{
int total = 0;
if (numItems <= 10)
{
for (int count=0; count < numItems; count = count + 1)
{
if (numbers[count] > 0)
{
total = total + numbers[count];
}
}
}
return total;
}
}
public void testOne()
{
loopdemo app = new loopdemo();
assertEquals(0, app.findTotal(0));
assertEquals(5, app.findTotal(1));
assertEquals(5, app.findTotal(2));
assertEquals(17, app.findTotal(5));
assertEquals(26, app.findTotal(9));
assertEquals(36, app.findTotal(10));
assertEquals(0, app.findTotal(11));
}
Reference: Pressman, R. Software Engineering. Fifth
Edition.