Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

How do you test end-user scripts?

by Anonymous Monk
on Sep 06, 2010 at 00:53 UTC ( #858958=perlquestion: print w/replies, xml ) Need Help??
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).

Replies are listed 'Best First'.
Re: How do you test end-user scripts?
by eyepopslikeamosquito (Chancellor) 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".

      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.

Re: How do you test end-user scripts?
by GrandFather (Sage) 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
Re: How do you test end-user scripts?
by snoopy (Deacon) 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.

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.

Re: How do you test end-user scripts?
by Khen1950fx (Canon) on Sep 06, 2010 at 07:07 UTC
    I wrote this test to see if my script would compile and run. I used Test::Script. Run it with Devel::SmallProf like this
    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' );

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://858958]
Approved by lidden
help
Chatterbox?
[1nickt]: This is not to suggest that Karl should remove his slogan. Just thoughts of mine... Trust me I believe I am very much more radical than most, and if I had a slogan for every gross injustice in my sig, there would be no more bytes!
Discipulus redskins .. well native american..
[1nickt]: So if I was to offer any editorial advice it would be to (1) make it endure time passing (2) make it positive (3) make it about you and your beliefs
[1nickt]: (Disicpulus The new best choice is maybe "First People"
[karlgoethebier]: Discipulus: This is called "Volksverhetzung" in German which means
[karlgoethebier]: incitement of the people and it is prohibited by law
[1nickt]: The best sign I have sign in a protest march was carried by a kid of about 10 years "If you build a wall my generation will tear it down."
[1nickt]: ... sign I have seen ...
[1nickt]: Now that one made me hopeful! First time not angry in a long time when I saw that kid.
[Discipulus]: i propend for removing: why? because we are so few that we must find i minimal common divisor, this is certainly Perl not our (anyway private) thougths. And i say this still wondering because i love a lot freedom of expression. And i say this not for roho

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2017-06-22 12:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (519 votes). Check out past polls.