Is there a need or how to write a test on main.pl which is not module and run it using Perl Devel cover

82 Views Asked by At

There are lots of material talking about writing coverage on module but how if I want to write a test for command runnable perl script (main.pl)?

Is there a need to write test for main.pl or I just need to write test for module will do?

Let's say I have these two scripts.

command runnable script

main.pl

import Halo;
&main;
sub main() {
    my $a = 2;
    my $b = 3;
    my $c = Halo.add($a, $b);
    print "a + b = $c\n";
}

==============================================

Perl module

Halo.pm

package Halo;
sub add() {
    my ($class, $a, $b) = @_;
    return $a + $b;
}
1;

==============================================

Run in command line:
perl main.pl

2

There are 2 best solutions below

0
Diab Jerius On BEST ANSWER

I suggest you research "unit" and "end-to-end" testing to understand the benefits of each. I would recommend that you do both. Testing your main script is easily done with "modulinos", allowing you to fairly painlessly tie into the existing Perl testing ecosystem.

0
pjcj On

From the synopsis: https://metacpan.org/pod/Devel::Cover#SYNOPSIS

$ perl -I. -MDevel::Cover main.pl
$ cover

You'll also need to change your code a bit. You'll need "use" instead of "import" and "Halo->add" instead of "Halo.add". (These changes are nothing to do with Devel::Cover.)