Good afternoon, Monks -

I am trying to put together my first-ever test of a standalone script. I've been looking at the last chapter of the O'Reilly Perl Testing: A Developer's Notebook as well as some of the standard testing documentation (Test::Tutorial, etc). However, though I've gotten a start, I'm not sure where to go from here.

The script to be tested expects two parameters, the name of an input file and the name of an output file (so it is a filter, in a way).

As Perl Testing suggests I have added the following line to the script:

main( @ARGV ) unless caller( );

... and recast the first part of the program as a sub called main. I then created a test script that looks like this:

#!/usr/bin/perl use warnings; use strict; use Test::More tests => 5; ok( require( 'myScript' ), 'loaded file okay' ) or exit; throws_ok { main( ) } qr/Usage:/, 'main( ) should give a usage error without any arguments'; throws_ok { main( 'bad command' ) } qr/Unknown command 'bad command'/, '... or with a bad command given';

When I run this against the script 'myScript', I get the following output:

./test_myScript.t 1..5 ok 1 - loaded file okay parameter `files' not set parameter `output' not set # Looks like you planned 5 tests but only ran 1.

... which is fine, but what I'd like to do is set up some tests which confirm that the script is doing its thing correctly, e.g. tests that a dummy file (provided as part of the testing suite or created by an intermediate script) passed to the script correctly creates a dummy output file, or what have you.

So really the question is how, in that testing script, do I go about passing in those parameters to the main() routine in myScript?

In the main() routine, input and output params are coded as $param{files} and $param{output}, respectively, e.g. with Getopt::Long.

Thanks in advance for any pointers, or pointers to tutorials that have more info on testing standalone scripts.

GB


In reply to testing a standalone script with parameters by chexmix

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.