Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Fake test coverage

In this example we have a very simple module with two simple functions.

package MyMath;
use strict;
use warnings;

our $VERSION = '1.00';

sub add {
    $_[0] + $_[1];
}

sub multiply {
    $_[0] + $_[1];
}


1;

We have two tests. The multiply test check one case for which the multiply function happens to return the correct value. In the case of the add function we call the function, but for some reason we don’t compare the result.

use strict;
use warnings;

use Test::More;

use MyMath;

plan tests => 2;

subtest multiply => sub {
    my $result = MyMath::multiply(2, 2);
    is $result, 4;
};

subtest add => sub {
    my $result = MyMath::add(2, 3);
    #is $result, 5;
    ok 5;
};



We can generate the test coverage report using Devel::Cover and it will report 100% test coverage.

$ perl Makefile.PL
$ cover -test

------------------ ------ ------ ------ ------ ------ ------ ------
File                 stmt   bran   cond    sub    pod   time  total
------------------ ------ ------ ------ ------ ------ ------ ------
blib/lib/MyMath.pm  100.0    n/a    n/a  100.0    0.0  100.0   85.7
Total               100.0    n/a    n/a  100.0    0.0  100.0   85.7
------------------ ------ ------ ------ ------ ------ ------ ------
use strict;
use warnings;

use ExtUtils::MakeMaker;
WriteMakefile
(
  NAME           => 'MyMath',
  VERSION_FROM   => 'lib/MyMath.pm',
  LICENSE        => 'perl',
  PREREQ_PM      => {
         'Exporter'   => '0',
  },
  TEST_REQUIRES => {
         'Test::Simple'  => '1.00',
  },
);