http://www.perlmonks.org?node_id=1072921

tizatron has asked for the wisdom of the Perl Monks concerning the following question:

Greetings,

Someone passed me a perl script to look at and I quickly realized that I needed to get a test around it. I had never used the Test::More module and I wanted to get some feedback, any tips and some comments.

I watered down the script greatly. Essentially:

$ ./sample.pl my-dev-1 --- classes: - all - dev
The script accepts a hostname and spits out a list of classes. Here's the code:
#!/usr/bin/perl use strict; use warnings; use YAML qw( Dump ); use Getopt::Std; use constant { true => 1, false => 0 }; use Data::Dumper; my $deploy = 'common'; my $host = 'unknown'; my $fullhost = 'none'; my @classes = (); my %args = (); getopts( 'hr:', \%args ); sub usage { my $heredoc = <<EOF; Usage: sample.pl [-h] hostname [ -h ] print this help message EOF print $heredoc; return $heredoc; } # main if (@ARGV == 0) { print "no args\n"; } else { $fullhost = $ARGV[$#ARGV]; $fullhost =~ s/^.*:://; if ( $fullhost =~ /(\w+)-(.*)/ ) { ( $deploy, $host ) = ( $1, $2 ); } } if ( $args{h} ) { usage() } if ( $deploy =~ /^my$/ ) { # Pushing global classes push( @classes, 'all' ); if ( $host =~ /build-/ ) { push( @classes, 'build' ); } # Pushing Dev Classes if ( $host =~ /dev-/ ) { push( @classes, 'dev' ); } } else { push( @classes, 'base' ); } print Dump( { classes => \@classes, } );

So, now I want to wrap some cover and tests around this script. I wrote a little test.pl script as a first attempt.

#!/usr/bin/perl use warnings; use strict; use Test::More tests => 2; use Test::Output; require_ok( "./sample.pl"); is(&usage(), " Usage: sample.pl [-h] hostname [ -h ] print this help message ", 'check usage message'); done_testing();

Seems to run ok (no pun intended)

$ ./test.pl 1..2 no args --- classes: - base ok 1 - require './sample.pl'; Usage: sample.pl [-h] hostname [ -h ] print this help message ok 2 - check usage message

However, now I start to get the feeling that I am doing something wrong. I had to add a return() to the usage function to get that test to pass. Not sure if there is a better way to do that.

I tried the run the code coverage

---------------------------- ------ ------ ------ ------ ------ ------ ------
File                           stmt   bran   cond    sub    pod   time  total
---------------------------- ------ ------ ------ ------ ------ ------ ------
sample.pl                     100.0    n/a    n/a  100.0    n/a    3.1  100.0
test.pl                       100.0    n/a    n/a  100.0    n/a   96.9  100.0
Total                         100.0    n/a    n/a  100.0    n/a  100.0  100.0
---------------------------- ------ ------ ------ ------ ------ ------ ------

Ok - that really looks wrong. How can I have 100 percent code coverage?

This is where I decided to take a break and do some research and study and make sure I driving in the right direction.

So a couple of questions:

1. Quick review so far - what am missing? Is this the right setup? I understand that a lot of testing in perl works around modules. I just have a script to work with. My goal is to wrap some testing around what I have, and attempt to drive it to something more modular

2. How do I start testing the implied main?

Any thoughts or comments welcomed.