I could actually execute the script, but that isn't much of a "unit" test anymore

Why not? As you said, it's a thin wrapper around your library, and I assume you've got plenty of tests for that, so you've got that covered. The script is an interface between the command line and the library, and that's the "unit" you're testing here - IMHO actually executing Perl is the only "real" way to test it. In any case, don't be too strict with yourself regarding the formal definitions of "unit" and "integration" tests here - while certainly important in some contexts (e.g. large projects with multiple devs), IMHO the far more important metrics here are whether you're testing everything, as in code coverage and so on.

I have various command-line tools that I test, see e.g. t/ in htools, although in that particular repository I haven't tested them on Windows. For that, you could take a look at the tests for my Badge::Simple command-line tool, or perhaps even more closely related to your question, this test for my Shell::Tools. Here's a short example for your script, assuming it's in a bin subdir and the tests in a t subdir, and if the script actually had a Synopsis POD. Running prove on this works on Linux and on Windows.

#!/usr/bin/env perl use warnings; use strict; use FindBin; use File::Spec::Functions qw/catfile updir/; use Capture::Tiny 'capture'; use Config; use Test::More tests => 2; # Unit Under Test my $UUT = catfile($FindBin::Bin, updir, 'bin', 'foo.pl'); subtest '--version' => sub { plan tests => 3; my ($out,$err,$exit) = capture { system $Config{perlpath}, $UUT, '--version' }; is $exit, 0, 'perl ok'; is $out, "my_script version 1.00\n\n", 'stdout message correct'; is $err, '', 'no stderr'; }; subtest '--help' => sub { plan tests => 3; my ($out,$err,$exit) = capture { system $Config{perlpath}, $UUT, '--help' }; is $exit, 0, 'perl ok'; like $out, qr/\bUsage:\s+foo\.pl FOO BAR\b/si, 'stdout message correct'; is $err, '', 'no stderr'; };

(Note: I've often used $^X instead of Config's perlpath without problems, but the CPAN Authors FAQ recommends the latter.)


In reply to Re: Portably unit testing scripts by haukex
in thread Portably unit testing scripts by wanna_code_perl

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, details, 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, summary, 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.