{
    "content": [
        {
            "type": "text",
            "text": "# Devel::Cover::Tutorial (perldoc)\n\n## NAME\n\nDevel::Cover::Tutorial - An introduction to code coverage\n\n## Sections\n\n- **NAME**\n- **VERSION**\n- **TUTORIAL**\n- **LICENCE**\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "Devel::Cover::Tutorial",
        "section": "",
        "mode": "perldoc",
        "summary": "Devel::Cover::Tutorial - An introduction to code coverage",
        "synopsis": null,
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [],
        "see_also": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "VERSION",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "TUTORIAL",
                "lines": 128,
                "subsections": []
            },
            {
                "name": "LICENCE",
                "lines": 6,
                "subsections": []
            }
        ],
        "sections": {
            "NAME": {
                "content": "Devel::Cover::Tutorial - An introduction to code coverage\n",
                "subsections": []
            },
            "VERSION": {
                "content": "version 1.36\n",
                "subsections": []
            },
            "TUTORIAL": {
                "content": "Here's part of a message I sent to perl-qa about code coverage metrics.\n\n1.0 Introduction\nIt is wise to remember the following quote from Dijkstra, who said:\n\nTesting never proves the absence of faults, it only shows their presence.\n\nIn particular, code coverage is just one weapon in the software engineer's testing arsenal.\n\nAny discussion of code coverage metrics is hampered by the fact that many authors use different\nterms to describe the same kind of coverage. Here, I shall provide only a brief introduction to\nsome of the most common metrics.\n\n2.0 Metrics\n2.1 Statement coverage\nThis is the most basic form of code coverage. A statement is covered if it is executed. Note\nthat statement != line of code. Multiple statements on a single line can confuse issues - the\nreporting if nothing else.\n\nWhere there are sequences of statements without branches it is not necessary to count the\nexecution of every statement, just one will suffice, but people often like the count of every\nline to be reported, especially in summary statistics. However it is not clear to me that this\nis actually useful.\n\nThis type of coverage is fairly weak in that even with 100% statement coverage there may still\nbe serious problems in a program which could be discovered through other types of metric.\n\nIt can be quite difficult to achieve 100% statement coverage. There may be sections of code\ndesigned to deal with error conditions, or rarely occurring events such as a signal received\nduring a certain section of code. There may also be code that should never be executed:\n\nif ($param > 20) {\ndie \"This should never happen!\";\n}\n\nIt can be useful to mark such code in some way and flag an error if it is executed.\n\nStatement coverage, or something very similar, can be called statement execution, line, block,\nbasic block or segment coverage. I tend to favour block coverage which does not attempt to\nextend its results to each statement.\n\n2.2 Branch coverage\nThe goal of branch coverage is to ensure that whenever a program can jump, it jumps to all\npossible destinations. The most simple example is a complete if statement:\n\nif ($x) {\nprint \"a\";\n} else {\nprint \"b\";\n}\n\nIn such a simple example statement coverage is as powerful, but branch coverage should also\nallow for the case where the else part is missing:\n\nif ($x) {\nprint \"a\";\n}\n\nFull coverage is only achieved here if $x is true on one occasion and false on another.\n\n100% branch coverage implies 100% statement coverage.\n\nBranch coverage is also called decision or all edges coverage.\n\n2.3 Path coverage\nThere are classes of errors that branch coverage cannot detect, such as:\n\n$h = undef;\nif ($x) {\n$h = { a => 1 };\n} if ($y) {\nprint $h->{a};\n}\n\n100% branch coverage can be achieved by setting ($x, $y) to (1, 1) and then to (0, 0). But if we\nhave (0, 1) then things go bang.\n\nThe purpose of path coverage is to ensure that all paths through the program are taken. In any\nreasonably sized program there will be an enormous number of paths through the program and so in\npractice the paths can be limited to a single subroutine, if the subroutine is not too big, or\nsimply to two consecutive branches.\n\nIn the above example there are four paths which correspond to the truth table for $x and $y. To\nachieve 100% path coverage they must all be taken. Note that missing elses count as paths.\n\nIn some cases it may be impossible to achieve 100% path coverage:\n\na if $x;\nb;\nc if $x;\n\n50% path coverage is the best you can get here.\n\nLoops also contribute to paths, and pose their own problems which I'll ignore for now.\n\n100% path coverage implies 100% branch coverage.\n\nPath coverage and some of its close cousins, are also known as predicate, basis path and LCSAJ\n(Linear Code Sequence and Jump) coverage.\n\n2.4 Expression coverage\nWhen a boolean expression is evaluated it can be useful to ensure that all the terms in the\nexpression are exercised. For example:\n\na if $x || $y\n\nThe expression should be exercised with ($x, $y) set to (0, 0) (required for branch coverage),\n(0, 1) and (1, 0) (to ensure that $x and $y are independent) and possibly with (1, 1).\n\nExpression coverage gets complicated, and difficult to achieve, as the expression gets\ncomplicated.\n\nExpressions which are not directly a part of a branching construct should also be covered:\n\n$z = $x || $y;\na if $z;\n\nExpression coverage is also known as condition, condition-decision and multiple decision\ncoverage.\n\n3.0 Other considerations\nIn order to get people to actually use code coverage it needs to be simple to use. It should\nalso be simple to understand the results and to rectify any problems thrown up. Finally, if the\noverhead is too great it won't get used either.\n\nSo there's a basic tutorial on code coverage, or at least my version of it. Typing a few of\nthese terms into google will probably provide a basis for future research.\n",
                "subsections": []
            },
            "LICENCE": {
                "content": "Copyright 2001-2019, Paul Johnson (paul@pjcj.net)\n\nThis software is free. It is licensed under the same terms as Perl itself.\n\nThe latest version of this software should be available from my homepage: http://www.pjcj.net\n",
                "subsections": []
            }
        }
    }
}