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

G'day tizatron,

Given that you're really just testing output from your script, Test::Output seems like a good choice; however, although you've loaded that module, you haven't used any of its functionality.

When I read "return $heredoc;", I wondered what this was for (it seemed pointless). I do see that you've questioned this yourself: "I had to add a return() to the usage function to get that test to pass ...". If your tests identify logic errors (or similar) in the code you're testing, then do fix the code; however, don't add questionable code just to make the tests pass.

Here's an example of how you might go about interspersing Test::More and Test::Output tests.

Sample production script (pm_script_testing.pl):

#!/usr/bin/env perl use strict; use warnings; usage() unless @ARGV; print scalar(@ARGV), ": @ARGV\n"; sub usage { warn "Argument required!\n"; exit; }

Sample test script (pm_script_testing.t):

#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 4; use Test::Output; stderr_is { qx{perl -c pm_script_testing.pl} } "pm_script_testing.pl syntax OK\n", 'Test Syntax'; stderr_is { qx{pm_script_testing.pl} } "Argument required!\n", 'Test Zero Arguments'; is(qx{pm_script_testing.pl 123}, "1: 123\n", 'Test One Argument'); stdout_is { system qw{pm_script_testing.pl 123 qwe} } "2: 123 qwe\n", 'Test Two Arguments';

Output:

$ pm_script_testing.t 1..4 ok 1 - Test Syntax ok 2 - Test Zero Arguments ok 3 - Test One Argument ok 4 - Test Two Arguments

If you haven't done so already, you may benefit from reading Test::Tutorial.

-- Ken

Replies are listed 'Best First'.
Re^2: Looking for help for unit tests and code coverage on an existing perl script
by tizatron (Novice) on Feb 03, 2014 at 22:55 UTC
    Ken,

    Your points on Test::Output are well taken. I was experimenting with that module and did get some results via sterr_is and stdout_is.

    The 'is' statement only evaluates a return, so your patterns using Test::Output are much more useful in this context.

    What I really need out of the test suite is code coverage. I pulled down your code and ran the tests. I tried to run something like:

    $ perl -MDevel::Cover pm_script_testing.pl $ cover
    I was able to spit out some code coverage numbers. I am going to try spend some time on this path and see I can get it to fly on my test script. But I need to dig in a little more and see if Test::Output aligns with Devel::Cover like I hope it does.