Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

comment on

( [id://3333] : superdoc . print w/replies, xml ) Need Help??

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

In reply to Re: Test module to assist sytem() by Anonymous Monk
in thread Test module to assist sytem() by axelrose

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.