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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.