How to measure test coverage with Devel::Cover in Perl?
Question
How to measure test coverage with Devel::Cover in Perl?
Measuring test coverage in Perl with Devel::Cover is a powerful way to understand which parts of your code are exercised by your tests and which are not. Devel::Cover is a widely-used code coverage tool that tracks statement, branch, condition, and pod coverage. It helps identify untested code paths, improving test quality and reliability.
How Devel::Cover Works
Devel::Cover works by running your Perl code under a coverage analysis layer that intercepts and records executed lines and branches. After running your tests, it generates a detailed HTML report summarizing coverage statistics.
Installing Devel::Cover
Install Devel::Cover from CPAN if you don't already have it:
cpan Devel::Cover
or using cpanm if you prefer:
cpanm Devel::Cover
Basic Usage
The simplest way to measure coverage is to run your test scripts (or any Perl scripts) via cover command that comes with Devel::Cover. For example, if you have a test script t/test.pl, run:
cover -test t/test.pl
This runs the test script with Devel::Cover enabled and then generates a coverage report in the cover_db directory, including HTML output you can open in a browser.
Using Devel::Cover in Practice
Usually, you'll run your entire test suite, e.g., via:
cover -test t/*.t
Or if you don't have test scripts, run your main program:
cover ./myscript.pl
After running, open the HTML report:
firefox cover_db/index.html
to explore coverage details.
Example: Measuring Coverage of a Simple Perl Module
Here is a minimal example of creating a module, a test script, and measuring coverage:
#!/usr/bin/perl
use strict;
use warnings;
# A simple module in the same file for demonstration
package My::Adder;
sub add {
my ($x, $y) = @_;
return $x + $y;
}
1;
package main;
# Basic test using Test::Simple (comes with Perl core from 5.8+)
use Test::Simple tests => 2;
ok(My::Adder::add(1, 2) == 3, 'Add 1 + 2');
ok(My::Adder::add(-1, 2) == 1, 'Add -1 + 2');
# This script can be saved as t/test_adder.t and run with Devel::Cover like this:
# cover -test t/test_adder.t
# The coverage report will show which lines and branches were executed.
Important Perl Concepts & Tips
- Context and sigils: Coverage is reported for statements, so understanding scalar
$, array@, and hash%context helps interpret which lines are truly hit. - TMTOWTDI (There's More Than One Way To Do It): Different coding styles can affect coverage—for example, using ternary expressions or explicit
if/elseblocks affects branch coverage. - Version notes:
Devel::Coverworks well with Perl 5.10+ but supports older versions too. Some advanced coverage types (condition, subroutine) are best supported in modern Perls. - Common Pitfalls: Running coverage on code that uses
evalor dynamic loading can lead to incomplete data. - Make sure your tests actually run all the code paths — just importing code doesn’t cover branches or conditions.
Summary
To measure test coverage with Devel::Cover:
- Write your tests normally using Test::Simple, Test::More, or similar.
- Run tests under coverage:
cover -test t/*.t - View coverage reports in
cover_db/index.html. - Analyze the report and add tests to cover uncovered lines or branches.
This approach helps improve Perl test quality by exposing untested code parts, leveraging Perl's rich testing ecosystem and Devel::Cover's powerful analysis.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 64ms
1..2
ok 1 - Add 1 + 2
ok 2 - Add -1 + 2
(empty)Was this helpful?
Related Questions
- How to use Test::Deep for complex data structure comparison?
- How to test Mojolicious applications in Perl?
- How to test HTTP responses with Test::WWW::Mechanize in Perl?
- How to use prove command to run Perl tests?
- How to test database code with Test::mysqld in Perl?
- How to use Test::Fatal for exception testing in Perl?