Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I've been using Test::Simple, Test::More, and the 'prove' command for writing/running test cases for my modules. But what is the Best Practice for testing end-user facing scripts? Should I write test cases which calls foo.pl via system()/open()/etc, feed it input, and see what comes out with regular expressions?
Assume for the sake of argument that foo.pl operates in a unix-y way (ie, not interactive, takes in some input from stdin, does magic, and writes output to stdout).
Re: How do you test end-user scripts?
by eyepopslikeamosquito (Bishop) on Sep 06, 2010 at 08:28 UTC
|
I endorse chromatic's comment.
When writing scripts, I typically abstract the work they do into CPAN-like modules
and unit test each module using Test::More and the prove command.
I strive to keep my script mainlines as short as is practicable.
There are many examples of this approach on the CPAN;
see, for example, the perltidy command, part of the Perl-Tidy distribution
and the perlcritic command, part of the Perl-Critic distribution.
See also:
Effective Perl Programming, 2nd edition: Item 91, "Write programs as modulinos for easy testing"
| [reply] [d/l] [select] |
|
I claim that isn't enough.
If you bring in your car in, would you be satisfied if they just tested the battery, the steering, the transmission, etc, but never actually tried to drive the car and see if it runs? All individual components of you car may work according to the spec, but if they aren't connected correctly, you will still have to walk.
Heck, for complete products, I'd say testing the final product is way more important than any unit testing.
| [reply] |
Re: How do you test end-user scripts?
by GrandFather (Saint) on Sep 06, 2010 at 01:32 UTC
|
That would amount to black box testing, which is fine if that is what you want. The white box technique is to require scripts so you can access individual subs that you want to test.
True laziness is hard work
| [reply] |
Re: How do you test end-user scripts?
by snoopy (Curate) on Sep 06, 2010 at 02:42 UTC
|
>>Assume for the sake of argument that foo.pl operates in a unix-y way (ie, not interactive, takes in some input from stdin, does magic, and writes output to stdout)
If you do decide to run your scripts, you might want to look at Test::Script::Run.
I'm using it to write tests that run on various Unixes.
I've also experimentally tried it on Windows; seems to work there as well. | [reply] |
Re: How do you test end-user scripts?
by chromatic (Archbishop) on Sep 06, 2010 at 06:46 UTC
|
You may not want to go to this much work, but I've had good experiences extracting modules from scripts and testing those modules directly.
| [reply] |
Re: How do you test end-user scripts?
by Khen1950fx (Canon) on Sep 06, 2010 at 07:07 UTC
|
perl -d:SmallProf test.pl
When its done, look in the Cwd for smallprof.out.
#!/usr/bin/perl
use strict;
use warnings;
use Test::Script;
use Test::More tests => 2;
script_compiles( 'blib/script/test.pl', 'Main script compiles' );
script_runs( 'blib/script/test.pl', 'Main script runs' );
| [reply] [d/l] [select] |
|
|