1Devel::Cover::Tutorial(U3s)er Contributed Perl DocumentatDieovnel::Cover::Tutorial(3)
2
3
4

NAME

6       Devel::Cover::Tutorial - An introduction to code coverage
7

VERSION

9       version 1.03
10

TUTORIAL

12       Here's part of a message I sent to perl-qa about code coverage metrics.
13
14   1.0 Introduction
15       It is wise to remember the following quote from Dijkstra, who said:
16
17         Testing never proves the absence of faults, it only shows their presence.
18
19       In particular, code coverage is just one weapon in the software
20       engineer's testing arsenal.
21
22       Any discussion of code coverage metrics is hampered by the fact that
23       many authors use different terms to describe the same kind of coverage.
24       Here, I shall provide only a brief introduction to some of the most
25       common metrics.
26
27   2.0 Metrics
28   2.1 Statement coverage
29       This is the most basic form of code coverage.  A statement is covered
30       if it is executed.  Note that statement != line of code.  Multiple
31       statements on a single line can confuse issues - the reporting if
32       nothing else.
33
34       Where there are sequences of statements without branches it is not
35       necessary to count the execution of every statement, just one will
36       suffice, but people often like the count of every line to be reported,
37       especially in summary statistics.  However it is not clear to me that
38       this is actually useful.
39
40       This type of coverage is fairly weak in that even with 100% statement
41       coverage there may still be serious problems in a program which could
42       be discovered through other types of metric.
43
44       It can be quite difficult to achieve 100% statement coverage.  There
45       may be sections of code designed to deal with error conditions, or
46       rarely occurring events such as a signal received during a certain
47       section of code.  There may also be code that should never be executed:
48
49         if ($param > 20)
50         {
51           die "This should never happen!";
52         }
53
54       It can be useful to mark such code in some way and flag an error if it
55       is executed.
56
57       Statement coverage, or something very similar, can be called statement
58       execution, line, block, basic block or segment coverage.  I tend to
59       favour block coverage which does not attempt to extend its results to
60       each statement.
61
62   2.2 Branch coverage
63       The goal of branch coverage is to ensure that whenever a program can
64       jump, it jumps to all possible destinations.  The most simple example
65       is a complete if statement:
66
67         if ($x)
68         {
69           print "a";
70         }
71         else
72         {
73           print "b";
74         }
75
76       In such a simple example statement coverage is as powerful, but branch
77       coverage should also allow for the case where the else part is missing:
78
79         if ($x)
80         {
81           print "a";
82         }
83
84       Full coverage is only achieved here if $x is true on one occasion and
85       false on another.
86
87       100% branch coverage implies 100% statement coverage.
88
89       Branch coverage is also called decision or all edges coverage.
90
91   2.3 Path coverage
92       There are classes of errors that branch coverage cannot detect, such
93       as:
94
95         $h = undef;
96         if ($x)
97         {
98           $h = { a => 1 };
99         }
100         if ($y)
101         {
102           print $h->{a};
103         }
104
105       100% branch coverage can be achieved by setting ($x, $y) to (1, 1) and
106       then to (0, 0).  But if we have (0, 1) then things go bang.
107
108       The purpose of path coverage is to ensure that all paths through the
109       program are taken.  In any reasonably sized program there will be an
110       enormous number of paths through the program and so in practice the
111       paths can be limited to a single subroutine, if the subroutine is not
112       too big, or simply to two consecutive branches.
113
114       In the above example there are four paths which correspond to the truth
115       table for $x and $y.  To achieve 100% path coverage they must all be
116       taken.  Note that missing elses count as paths.
117
118       In some cases it may be impossible to achieve 100% path coverage:
119
120         a if $x;
121         b;
122         c if $x;
123
124       50% path coverage is the best you can get here.
125
126       Loops also contribute to paths, and pose their own problems which I'll
127       ignore for now.
128
129       100% path coverage implies 100% branch coverage.
130
131       Path coverage and some of its close cousins, are also known as
132       predicate, basis path and LCSAJ (Linear Code Sequence and Jump)
133       coverage.
134
135   2.4 Expression coverage
136       When a boolean expression is evaluated it can be useful to ensure that
137       all the terms in the expression are exercised.  For example:
138
139         a if $x || $y
140
141       The expression should be exercised with ($x, $y) set to (0, 0)
142       (required for branch coverage), (0, 1) and (1, 0) (to ensure that $x
143       and $y are independent) and possibly with (1, 1).
144
145       Expression coverage gets complicated, and difficult to achieve, as the
146       expression gets complicated.
147
148       Expressions which are not directly a part of a branching construct
149       should also be covered:
150
151         $z = $x || $y;
152         a if $z;
153
154       Expression coverage is also known as condition, condition-decision and
155       multiple decision coverage.
156
157   3.0 Other considerations
158       In order to get people to actually use code coverage it needs to be
159       simple to use.  It should also be simple to understand the results and
160       to rectify any problems thrown up.  Finally, if the overhead is too
161       great it won't get used either.
162
163       So there's a basic tutorial on code coverage, or at least my version of
164       it.  Typing a few of these terms into google will probably provide a
165       basis for future research.
166

LICENCE

168       Copyright 2001-2013, Paul Johnson (paul@pjcj.net)
169
170       This software is free.  It is licensed under the same terms as Perl
171       itself.
172
173       The latest version of this software should be available from my
174       homepage: http://www.pjcj.net
175
176
177
178perl v5.16.3                      2013-05-20         Devel::Cover::Tutorial(3)
Impressum