Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

testing a standalone script with parameters

by chexmix (Hermit)
on Mar 03, 2009 at 19:58 UTC ( [id://747874]=perlquestion: print w/replies, xml ) Need Help??

chexmix has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: testing a standalone script with parameters
by GrandFather (Saint) on Mar 04, 2009 at 00:47 UTC

    throws_ok takes a code ref as the first parameter. Try:

    throws_ok (sub {main ()}, qr/Usage:/, 'main () should give a usage err +or without any arguments');

    True laziness is hard work
Re: testing a standalone script with parameters
by zwon (Abbot) on Mar 03, 2009 at 20:13 UTC

    You forget use Test::Exception. It contains throws_ok.

    Update: BTW, there's also require_ok in Test::More

      Ah, thanks. Test::Exception was in the script but the module wasn't present in my work environment, so I commented it out.

      I'll have to install it.

        Hmm. That doesn't seem to have changed the output any. And I am still mainly interested in how to get those parameters passed in.

        I hope I am asking the question correctly and/or clearly. :^)

Re: testing a standalone script with parameters
by andreas1234567 (Vicar) on Mar 04, 2009 at 06:15 UTC
    So really the question is how, in that testing script, do I go about passing in those parameters to the main() routine in myScript?
    Set @ARGV before you call main(), e.g. @ARGV = ('--files=foo','--output=1')
    --
    No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://747874]
Approved by Perlbotics
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-25 06:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found