Saturday, April 25, 2009

Explain Statement Coverage.

The main purpose of Statement Coverage is to ensure that each statement has been tested atleast once. It measures the percentage of statements executed.



An Example of Statement Coverage

A vending machine dispenses either hot or cold drinks. If you choose a hot drink (e.g. tea or coffee), it asks if you want milk (and adds milk if required), then it asks if you want sugar (and adds sugar if required), then your drink is dispensed.

a. Draw a control flow diagram for this example. (Hint: regard the selection of the type of drink as one statement.)

b. Given the following tests, what is the statement coverage achieved? What is the decision coverage achieved?

Test 1: Cold drink
Test 2: Hot drink with milk and sugar

c. What additional tests would be needed to achieve 100% statement coverage? What additional tests would be needed to achieve 100% decision coverage?

The control flow diagram is shown in Figure 4.8. Note that drawing a control diagram here illustrates that structural testing can also be applied to the structure of general processes, not just to computer algorithms. Flowcharts are generally easier to understand than text when you are trying to describe the results of decisions taken on later events.



On Figure 4.9, we can see the route that Tests 1 and 2 have taken through our control flow graph. Test 1 has gone straight down the left-hand side to select a cold drink. Test 2 has gone to the right at each opportunity, adding both milk and sugar to a hot drink.



Every statement (represented by a box on the diagram) has been covered by our two tests, so we have 100% statement coverage.

We have not taken the No exit from either the 'milk?' or 'sugar?' decisions, so there are two decision outcomes that we have not tested yet. We did test both of the outcomes from the 'hot or cold?' decision, so we have covered four out of six decision outcomes. Decision coverage is 4/6 or 67% with the two tests.

No additional tests are needed to achieve statement coverage, as we already have 100% coverage of the statements.

One additional test is needed to achieve 100% decision coverage:

Test 3: Hot drink, no milk, no sugar

This test will cover both of the 'No' decision outcomes from the milk and sugar decisions, so we will now have 100% decision coverage. [Source of example]

Few interesting resources on Statement Coverage

Examples of Statement, Branch and Path coverages

Coverage Examples

Below PDF includes - Legitimate Uses of Statement Coverage Analysis, Pitfalls of Using a Coverage Analyzer and much more.

Coverage Analysis

What is wrong with Statement Coverage - drawbacks of Statement Coverage

Statement Coverage

Nice Explanation of Statement Coverage

Statement Coverage

Statement Coverage and its strengths and weaknesses

Statement Coverage

Also See:

Decision Coverage

Branch Testing

Path Testing

Basis Path Testing