Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Looking for help for unit tests and code coverage on an existing perl script

by eyepopslikeamosquito (Canon)
on Feb 01, 2014 at 09:55 UTC ( #1072954=note: print w/ replies, xml ) Need Help??


in reply to Looking for help for unit tests and code coverage on an existing perl script

I would extract a module from your script and unit test the module directly via Test::More and the prove command. I like to keep my script mainlines as short as is practicable, with all the heavy lifting done in (unit-tested) module/s. There are many examples of this general approach on the CPAN; see, for example, the perltidy command, part of the Perl-Tidy distribution and the perlcritic command, part of the Perl-Critic distribution.

As an alternative, and perhaps a bit less work, you could re-structure your script as a "modulino", that is, a script that can masquerade as a module for ease of testing. This approach is described at:

See also: How do you test end-user scripts?


Comment on Re: Looking for help for unit tests and code coverage on an existing perl script
Re^2: Looking for help for unit tests and code coverage on an existing perl script
by tizatron (Initiate) on Feb 03, 2014 at 23:22 UTC

    Good links there. Appreciate it.

    In this theme, I went ahead and tried to setup an env similar to the layout described in this link:

    StackOverFlow Complete build/unittest/codecoverage

    couple of things that I did:

    • Created the layout described. Made directories for 'lib' and 't'.
    • Copied my script to lib. Flipped the extension to pm. Added a package statement to the script. Also added: __PACKAGE__->main( @ARGV ) unless caller();
    • Wrapped the main block of code, what I called the implied main, in sub main{...}

    Then I was able to add a stanza to my test file.

    my $my_dev_1 = sample::main("my-dev-1"); is($my_dev_1, "--- classes: - all - dev ", "my dev 1 test");

    Not sure how I feel about the sample::main("my-dev-1"). Would appreciate some feedback on that to see if I am calling this correctly.

    That said, I was able to generate code cover from this setup.

    $ ./Build test
    $ ./Build testcover
    

    ---------------------------- ------ ------ ------ ------ ------ ------ ------
    File                           stmt   bran   cond    sub    pod   time  total
    ---------------------------- ------ ------ ------ ------ ------ ------ ------
    blib/lib/sample.pm             97.4   91.7    n/a  100.0    0.0  100.0   93.2
    Total                          97.4   91.7    n/a  100.0    0.0  100.0   93.2
    ---------------------------- ------ ------ ------ ------ ------ ------ ------
    

    That processes pumps out a cover.html file that kinda aligns more with what I was hoping to see. The changes to the original code are not significant, but they are changes, more than what I was hoping to make, perhaps digging in harder on Test::Output would help.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1072954]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2014-12-18 02:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (41 votes), past polls