BaldManTom has asked for the wisdom of the Perl Monks concerning the following question:
Monks,
In considering how to automate testing on our (increasingly huge) application here at $work, I've been toying with creating a "script runner" in Perl, which would allow me (or my staff) to write scripts using Perl and some functions specific to driving our application, but without using (as much) object notation to manipulate the application. I haven't gotten any farther than a proof-of-concept, but I hope to pick your collective brains before I fall too far down the rabbit hole.
Here's a module which I'm using to test my "script runner" idea. If I were to move ahead, this would be significantly more involved, but I think it will help you get the idea what I'm talking about:
#! /usr/local/bin/perl package Product::Driver; use strict; use warnings; use Moose; has counter => ( isa => 'Int', default => 0, reader => 'value' ); sub add_one { my ( $self ) = @_; return ++$self->{'counter'}; } sub subtract_one { my ( $self ) = @_; return --$self->{'counter'}; } sub add_number { my ( $self, $amount ) = @_; return $self->{'counter'} += $amount; } sub subtract_number { my ( $self, $amount ) = @_; return $self->{'counter'} -= $amount; } sub reset_counter { my ( $self ) = @_; return $self->{'counter'} = 0; } 1;
Here's the "script runner" module that would read scripts and execute them
#! /usr/local/bin/perl package ScriptRunner; use strict; use warnings; use Product::Driver; use MooseX::Singleton; has driver => ( isa => 'Product::Driver' ); #Not yet implemented #has recorder => ( does => 'Tester::Recorder', required => 1 ); sub BUILD { my ( $self ) = @_; $self->_init_driver(); } sub _init_driver { my $self = ref($_[0]) ? shift : ScriptRunner->new(); $self->{'driver'} = Product::Driver->new(); } sub close_driver { my $self = ref($_[0]) ? shift : ScriptRunner->new(); $self->{'driver'} = undef; } sub start_driver { my $self = ref($_[0]) ? shift : ScriptRunner->new(); if ( $self->{'driver'} == undef ) { $self->_init_driver(); } } sub restart_driver { my $self = ref($_[0]) ? shift : ScriptRunner->new(); $self->close_driver(); $self->start_driver(); } sub check_success { my ( $self ) = ScriptRunner->new(); my $val = ( $_[0] ne '' ) ? $_[0] : 0; my $msg = $_[1] ? $_[1] : "Unknown Event"; # Eventually, instead of printing the result, there would be a # recorder module that would be available to record the test # results in some convenient, meaningful way. But for now... printf "%-20s%s\n", $val ? "PASSED TEST" : "FAILED TEST", $msg; } sub add_one { my ( $self ) = ScriptRunner->new(); return $self->{'driver'}->add_one; } sub add_number { my ( $self ) = ScriptRunner->new(); my ( $amount ) = @_; return $self->{'driver'}->add_number($amount); } sub subtract_one { my ( $self ) = ScriptRunner->new(); return $self->{'driver'}->subtract_one; } sub subtract_number { my ( $self ) = ScriptRunner->new(); my ( $amount ) = @_; return $self->{'driver'}->subtract_number($amount); } sub value { my ( $self ) = ScriptRunner->new(); return $self->{'driver'}->value; } sub reset_counter { my ( $self ) = ScriptRunner->new(); return $self->{'driver'}->reset_counter; } sub do_script { my $self = ref($_[0]) ? shift : ScriptRunner->new(); my ( $file ) = @_; if ( ! -f $file ) { die "$file not found!" } my $res = do $file; if ( $@ ) { die "Can't do $file\n", $@ } return $res; } 1;
A simple app that creates a ScriptRunner object and then feeds the filenames of scripts to be executed (specified on the command line).
#! /usr/local/bin/perl use strict; use warnings; use lib "./lib"; use ScriptRunner; my $runner = ScriptRunner->new(); # Do any necessary environmental setup here. Since this is # just a demo, there's nothing really to do. foreach my $f ( @ARGV ) { print "Running script file: $f\n"; $runner->do_script($f) }
And a sample script:
my $string = ''; $string .= add_one(); $string .= " "; $string .= add_number(50); $string .= " "; $string .= subtract_one(); $string .= " "; $string .= subtract_number(45); $string .= " "; $string .= value(); check_success ( $string eq "1 51 50 5 5", "This is a comment" ); return ( $string );
The code probably isn't the best at this point, I've only been trying to decide if I'm moving forward with this.
So, with all that in mind, here are my questions:
Thanks for your time and feedback.
Regards,
Bald Man Tom
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: On the merits of implementing a script running application
by moritz (Cardinal) on Apr 03, 2008 at 16:45 UTC | |
by BaldManTom (Friar) on Apr 03, 2008 at 17:36 UTC | |
|
Re: On the merits of implementing a script running application
by samtregar (Abbot) on Apr 03, 2008 at 17:23 UTC | |
by BaldManTom (Friar) on Apr 03, 2008 at 17:44 UTC | |
|
Re: On the merits of implementing a script running application
by stvn (Monsignor) on Apr 05, 2008 at 02:55 UTC | |
by BaldManTom (Friar) on Apr 08, 2008 at 16:01 UTC |