Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Test module to assist sytem()

by Anonymous Monk
on Nov 29, 2002 at 22:03 UTC ( [id://216605] : note . print w/replies, xml ) Need Help??


in reply to Test module to assist sytem()

I just noticed Test::Cmd on CPAN. It looks ludicrously powerful. Possibly overkill for most people, but you could probably wrap it.

There's also some work I did for the Good Folks at Mitel Network Server Solutions (if that's a mouthful, just say "E-Smith") which is fortunately GPL'd. If you can get a copy of their e-smith-test.srpm you'll find the esmith::TestUtils library with a little thing called simulate_perl_program(). It will "run" a perl script but not as a seperate process.

This is for the case where you have a perl script you wish to test but have to change something about it in order to make it testable. For example, maybe it uses a database, you want it to use a test database but there's no easy way to change where it connects to. You can wrap DBI::connect() to alter the database connection, then call simulate_perl_program(). The program's code calls DBI->connect, it hits your wrapper, swaps out the hardcoded name with the name of the test database and passes it through to the real DBI test.

Something like:

use DBI; { my $real_connect = DBI->can('connect'); no warnings 'redefine'; local *DBI::connect = sub { my($self, $ds, $user, $pass, $attr) = @_; $ds =~ s/yourdb/testdb/; $self->$real_connect($ds, $user, $pass, $attr); }; my $exit = simulate_perl_program('yourprogram.plx', @args); }

Additionally, if you're using Test::Inline, $_STDOUT_ and $_STDERR_ from simulate_perl_program() are simply trapped like always.

If someone has the tuits, it would be nice to see this taken out of e-smith-test and put on CPAN. I've temporarily mirrored ESmith::TestUtils

Replies are listed 'Best First'.
Re: Test module to assist sytem()
by schwern (Scribe) on Nov 29, 2002 at 22:05 UTC
    Damn those cookies. That's all me above.