Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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.


In reply to Looking for help for unit tests and code coverage on an existing perl script by tizatron

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (6)
    As of 2020-04-05 01:46 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      The most amusing oxymoron is:
















      Results (33 votes). Check out past polls.

      Notices?