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 |