Structure Based or White Box techniques
White-box test design techniques (also called structural or structure- based techniques) are used to derive test cases from an analysis of the program(code). This article is devoted to understanding how to derive test cases from a program and also derive coverage metrics for tests undertake.
Deriving test case after analysis or understanding programs is white box testing. (In contrast to black box testing where test cases are designed from specification).
Test cases involve assigning values to the variables and measuring what lines of code get executed. In the exam, questions will be asked on how many test cases will be needed to ensure every line of code/decisions are executed or given some values, what % of lines/decisions are executed.
Test cases from code are similar to black box test cases, but think test data here – like what values you will provide to run through the code. The emphasis here is what lines will be executed with the data you provide. And the minimum number of test cases to achieve this.
During Black Box testing, depending on the tester’s experience, after a full round of System testing, the lines of code covered varies between 30% and 70%. So that leaves a lot of lines of code untested. Coverage metrics help understand what lines are not covered and help us design test cases to increase the coverage. There are certain tools available to measure the lines of code covered when tests are run.
The techniques to derive test cases:(ISTQB Foundation syllabus)
- Statements testing /coverage
- Decision Testing/coverage
Others include Condition coverage, Modified Decision coverage, Branch Condition Combination Coverage & LCSAJ, Path coverage etc which are beyond the cope of this article.
Statement coverage: If the test case executes every line of code in the program, it is called 100% statement coverage.
Decision coverage: If the test cases execute both the decisions, it is called 100% decision coverage.
Statement Coverage = Number of executable statements executed x 100 Total number of executable statements
Decision Coverage = Number of decisions exercised x 100 Total number of decisions
A program code can be sequential where statements execute one after another. Execution of code can also be based on a condition being true or false.
Sequential or Linear statements:
Example of a sequential code, where the program is executed line after line.
1 READ A 2 READ B 3 Sum = A+ B
Test cases for Sequential statements are simple, assign values to A & B.
For eg, test case /data : A=1, B=2, will run through statements 1,2,3 or all the lines of this program. In this case, one test case is needed for 100% Statement coverage.
Selection or Decisions:
In this case the computer has to decide if a condition is true or false. If it is true the computer takes one route, and if it is false the computer takes a different route.
IF (condition is true) Do this ELSE (condition is False) Do something else END IF
1 IF Age > 16 2 Process License application 3 ELSE 4 Decline the License application process 5 END IF
Test cases: Age =18 tests lines 1,2,5 and Age =14 tests lines 1,3,4,5.
So, 2 test cases are needed to execute every line of code. And 2 test cases to execute the True and False conditions. So 2 test cases are needed for 100% Statement and Decision coverage.
There may not always be an ELSE part, as below:
1 IF Age >16 2 THEN 3 "Process License Application" 4 ENDIF
Test cases for the above, assigning Age =18 tests lines 1,2,3,4, so only one test case is needed to execute every line of code.
But 2 test cases are needed to test the decisions: 1 to execute the True (Age > 16) and False(Age < 16). Why?
Because for Age <16 , we need to ensure the program does not do anything, it needs to go to the ENDIF. Hence Decision coverage is stronger than Statement coverage.
100% Decision coverage ensures 100% Statement coverage. The vice versa is not true.
Consider the following:
1 IF (Age > 16) and (Gender = Female) 2 Process License application 3 ELSE 4 Decline the License application process 5 END IF
1 IF (Age > 16) or (Gender = Female) 2 Process License application 3 ELSE 4 Decline the License application process 5 END IF
Assign (Age =18 and Gender=Female) to test lines 1,2,5 and (Age=14, Gender = Male) to test lines 1,3,4,5. So, 2 test cases are needed to execute every line of code(100% Statement coverage). And 2 test cases to test both the decisions – true and false (100% Decision Coverage).
In the above examples, we see why Decision coverage is inadequate, all conditions are not fully tested:
Age >16 & Female, Age > 16 & Male, Age <16 and Female and Age <16 & Male will cover all the combinations. Hence the need for other stronger metrics.
Nested Ifs or multiple IFs within an IF:
1 IF ( Age > 17) 2 IF Age <50 3 Print Age is between 17 and 50 4 ELSE IF ( Age > 50) 5 Print Age is greater than 50 6 END IF 7 ELSE 8 Print Age is less than 17. 9 END IF
Test cases for the above, assigning Age =16, Age =51 and Age =29 (3 test cases) will test every line/statement and every decision..
The nested If can also be expressed as a CASE structure.
Case Age > 50: Report “Age is greater than 50” Case Grade > 17: Report “Age is between 17 and 50” Default: Report “Age is less than 17” End Case
Again, assigning Age =16, Age =51 and Age =29 (3 test cases) will test every line and every decision..
Iterations or loops:
When an action needs to repeated until the Condition becomes False, loops are used. There are 2 types of loops, While -Do and Repeat Until
DO WHILE condition sequence ENDWHILE
The loop is entered only if the condition is true. The “sequence” is performed for each iteration. At the conclusion of each iteration, the condition is evaluated and the loop continues as long as the condition is true.
WHILE ( Shopping Trolley not empty) DO Add Cost of Item to Total. Print the item name and item cost END DO Print TotalCost
Test cases for Do-While – the loop is entered if the condition is true, so one test case that has a true value is enough to test all the lines of code and the decision.
There is another variation of the loop structure known as a REPEAT UNTIL loop.
This loop is similar to the WHILE loop except that the test is performed at the bottom of the loop instead of at the top. Two keywords, REPEAT and UNTIL are used. The general form is:
REPEAT sequence UNTIL condition
Again, only one test case is sufficient to test all lines of code for both Statement and Decision Coverage.