Statement Coverage, Branch Coverage, Condition Coverage Tutorials For Software Testers
Statement Coverage, Branch Coverage, Condition Coverage Tutorials For Software Testers
TECHNIQUES
The 3 categories are:
1) Specification based or Black box testing.
2) Structured-based or White-Box Techniques.
3) Experienced Based Techniques.
Component level: The structure is that of the code itself, i.e., statements, decisions
or branches.
Integration level: The structure may be a call three (a diagram in which modules call
other modules).
System level: the structure may be a menu structure, business process or webpage
structure.
Statement coverage:
-> Does not ensure coverage of all functionality
->
The objective if the statement testing is to show that the executable statements
within a program have been executed at least once. An executable statement can be
described as a line of program source code that will carry out some type of action.
For example:
If all statements in a program have been executed by a set of tests then 100%
statement coverage has been achieved. However, if only half of the statement have
been executed by a set of tests then 50% statement coverage has been achieved.
The aim is to achieve the maximum amount of statement coverage with the
minimum number of test cases.
If our component consists of three lines of code we will execute all with one test
case, thus achieving 100% statement coverage. There is only one way we can
execute the code - starting at line number 1 and finishing at line number 3.
Program code becomes tough when logic is introduced. It is likely what a component
will have to carry out different actions depending upon circumstances at the time of
execution. In the code example shown, the component will do different things
depending on whether the age input is less than 17 or if it is 17 and above. With the
statement testing we have to determine the routes through the code we need to take
in order to execute the statements and the input required to get us there!
In this example, the statement will be executed if the age is less than 17, so we
would create a test case accordingly.
>> For more complex logic we could use control flow graphing
>> Control flow graphs consists of nodes, edges and regions
Control flow graphs describes the logic structure if the software programs - it is a
method by which flows through the program logic are charted, using the code itself
rather than the program specification.
Each flow graph has nodes and edges - The nodes represent computational
statements or expressions, and the edges represent transfer of control between the
nodes. Together the nodes and edges encompass an area known as a region.
In the diagram, the structure represents an 'If Then Else Endif' costurct. Nodes are
shown for the 'If' and the 'Endif'. Edges are shown for the 'Then' (the true path) and
the 'Else (the false path). The region is the area enclosed by the nodes and the
edges.
There are 4 basic structures that are used withn control-flow graphong.
The 'DoWhile' structure will execute a section of code whilst a field or indicator is set
to a certain value. For example,
The 'Do until' structure will execute a section of code until a field or indicator is set to
a certain value. Foe example,
The 'Go To' structure will divert the program execution to the program section in
question. For example
>> we can introduce extra nodes to indicate where the executable statements are
>> And we can see the path we need to travel to execute the statement in the code.
What we can do is introduce extra nodes to indicate where the statements occur in
the program code.
Now in our example we can see that we need to answer 'yes' to the question being
posed to traverse the code and execute the statement on line 2.
Program logic can be a lot more complicated than the examples I have given so far!
In the source code shown here, we have executable statements associated with each
outcome of the question being asked. We have to display an error message if the
age is less than 17(answering 'yes' to the question), and we have display 'customer
OK' if we answer 'No'.
We can only traverse the code only once with a given test; therefore we require two
tests to achieve 100% statement coverage.
Therefore we will need two tests that answer 'yes' to 'if fuel tank empty'
* Fuel tank empty AND petrol engine ( to execute line 3)
* Fuel tanl empty AND NOT petrol engine( to execute line 5)
one further test will be required where we answer 'no' to 'if fuel tank empty' to
enable us to execute the statement at line 8.
>>And this will be the last example for statement coverage.. we will then go for
decision coverage.
>> We will need 2 tests to achieve 100% statement coverage.
In this example, we have two separate questions that are being asked.
Our 2 tests achieve 100% statement coverage, but equally we could have had 2
tests with:
* A coffee drinker who doesn’t want cream
* A no-coffee drinker who wants cream
If we were being asked to achieve 100% statement coverage, and if all statements
were of equal importance, it wouldn’t matter which set if tests we choose.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Checking your calculation values:
Decision Coverage :
>>
The objective of decision coverage testing is to show all the decisions within a
component have been executed at least once.
A decision can be described as a line of source code that asks a question.
For example:
If all decisions within a component have exercised by a given set of tests then 100%
decision coverage has been achieved. However if only half of the decisions have been
taken with a given set of tests then you have only achieved 50% decision coverage.
Again, as with statement testing, the aim is to achieve the maximum amount of
coverage with the minimum number of tests.
>> Decision testing derives test cases to execute specific decision outcomes,
normally to increase decision coverage.
>> Decision testing is a form of control flow testing as it generates a specific flow of
control through the decision points.
If we are required to carry out decision testing, the amount of decision coverage
required for a component should be stated in the test requirements in the test plan.
We should aim to achieve atleast the minimum coverage requirements with our test
cases. If 100% decision coverage is not required, then we need to determine which
areas of the component are more important to test by this method.
Decision testing can be considered as the next logical progression from statement
testing in that we are not much concerned with testing every statement but the true
and false outcomes from every decision.
As we saw in our earlier examples of the statement testing, not every decision
outcome has a statement (or statements) to execute.
If we achieve 100% decision coverage, we would have executed every outcome of
every decision, regardless of whether there were associated statements or not..
>> we will need 2 tests to achieve 100% decision coverage & also 2 tests to achieve
100% statement coverage.
This example would still result in two tests, as there is one decision therefore 2
outcomes to tests.
However, we would need two tests to achieve 100% statement coverage, as there
are statements with each outcome of the decision.
So, in this instance, statement and decision testing would give us the same number
of tests. Note that if 100% coverage is required, statement testing can give us the
same number of tests as decision testing, BUT NEVER MORE!
>> As more statements are added, the tests for decision coverage are the same:
>> 3 tests to achieve 100% decision coverage, and 2 tests to achieve 100%
statement coverage.
We have now introduced a statement that is associated with 'No' outcome of the
decision on line 2.
This change affects the number of tests required to achieve 100% statement
coverage, but does NOT alter the number of tests required to achieved 100%
decision coverage - it is still three!
Finally, we have statements associated with each outcome of each decision - the
number of tests to achieve 100% statement coverage and 100% decision coverage
are now the same.
>> We will need 2 tests to achieve 100% decision coverage and 100% statement
coverage.
We looked at this example of the "if Then Else' structure when considering statement
testing.
As the decisions are separate questions we only need two tests to achieve 100%
decision coverage( the same as the number required for statement coverage).
You may have thought that four tests were required - exercising the four different
routes through the code, but remember, with decision testing our concern is to
exercise each outcome of each decision at least once - as long as we have answered
'Yes' and 'No' to each decision we have satisfied the requirements of the technique.
The tests we have illustrated would need the following input conditions:
* Coffee drinker wanting cream.
* Non Coffee drinker not wanting cream (but milk).
Unlike the 'If Then Else', we can loop around the 'Do While' structure, which means
that we exercise different routes through the code with one test.
As in the above diagram, if we set 'p' with an initial value '4', the first time through
the code will :
* Go from line 1 to line 2
* Answer 'Yes' to the 'If' on line 2 ( if p<5)
* Execute the statement in line 3 (p=p*2, so p now equals 8)
* Go from line 3, through line 4 to line 5
* Execute the statement on line 5 ( which adds 1 to 'p', making it`s value '9')
* Execute the statement on line 6, which takes it back up to line 1.
Again we execute the code, with the value of 'P' now '9'
* GO from line1 to line2
* Answer 'NO' to the 'if' on line 2 (If p>5)
* Go from line 4 to line 5
* Execute the statement on line 5(which adds 1 to 'p', making it’s value '10')
* Execute the statement on line 6, which takes it back up to line 1.
The control flow structures we showed earlier also depicted a 'Do Until' structure.
To reiterate, the 'Do Until' structure will execute a section of code until a field or
indicator is set to a certain value. For example,
Again we execute the code, with the value of 'A' now '31'
* Go from line 1 to line 2
* Answer 'No' to the 'If' on line 2 (If A < 20)
* Go from line 2, through line 4 to line 5
* Execute the statement on line 5( which adds 1 to 'A', making it`s value '32')
* Execute the statement on line 6, which exits the structure('A' is greater than 31)
END...