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

TUTORIAL

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

VERSION

162       Version 0.65 - 8th August 2009
163

LICENCE

165       Copyright 2001-2009, Paul Johnson (pjcj@cpan.org)
166
167       This software is free.  It is licensed under the same terms as Perl
168       itself.
169
170       The latest version of this software should be available from my
171       homepage: http://www.pjcj.net
172
173
174
175perl v5.10.1                      2009-08-08         Devel::Cover::Tutorial(3)
Impressum