18.6. Coverage Testing
We can also test our testing. In a perfect world, we'd test our program with every possible input and environment so that our program follows every particular path. That's certainly what the Professor would do. In reality, we tend to be more like Gilligan, though.
We won't go too deeply into the theory and practice of coverage tests, but there are many things that we can test. Statement coverage tells us how many of the statements in our code we execute during a run. Branch coverage tells us how many of the decision paths we actually follow. Path coverage, expression coverage, and other sorts of coverage exist too. The best starting point is the Devel::Coverage::Tutorial documentation.
The module comes with a program named cover, which handles most of the things that we need to do. For any particular program we want to measure, we simply load the Devel::Cover module when we run it.
$ perl -MDevel::Cover yourprog args $ cover
After we finish running the program, Devel::Cover leaves behind a file with all of the information it collected. Running the cover command turns that information into a set of HTML pages. When we look at the coverage.html page, we see a summary of our project along with its coverage statistics. The links on that page drill down into the individual files to see their coverage statistics too.
If we want to test a distribution, we just do this at the same time that we run our test suite. First, we get rid of any previous coverage results by passing cover the -delete option. After that, we have to run our make test and get Devel::Cover to do its magic at the same time. We can set the testing harness switches with the HARNESS_PERL_SWITCHES environment variable, in which we put the -MDevel::Cover we used on the command line previously. Now every time the test harness wants to invoke Perl (which is for every test file), it also loads Devel::Cover. As that script runs, it adds to the coverage database (which is why we deleted the database before we started). Finally, once we've finished going through the tests, we call cover again to turn the results into something we can read.
$ cover -delete $ HARNESS_PERL_SWITCHES=-MDevel::Cover make test $ cover
Everything shows up in a directory named cover_db in the current working directory. The coverage database shows up in cover_db, and the top-level page shows up in cover_db/coverage.html.