pileofrogs has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing a convenience module that makes it easy to add debugging output to quick scripts. I'd like to make some tests ala Test::More and I'm wondering what's the best way to test based on the printed or warned output from a module?

Can I do something like E.G.:

grab_stdout($foo); print 'gabba gabba hey'; ungrab_stdout(); ok($foo eq 'gabba gabba hey`) || diag "didn't print 'gabba gabba hey' to stdout.";
... or something similer... possibly better....?

Basically, I want to test if the stuff I wanted to send to stdout/stderr did in fact go to stout/stderr.

Thanks!
-Pileofrogs

Replies are listed 'Best First'.
Re: Testing a module's stdout/stderr
by Old_Gray_Bear (Bishop) on Mar 24, 2007 at 00:57 UTC
    Take a look at Perl Testing - A Developer's Notebook; Langworth and chromatic. Page 105-106 has a discussion of how to build tests for your STDERR output.

    ----
    I Go Back to Sleep, Now.

    OGB

Re: Testing a module's stdout/stderr
by kyle (Abbot) on Mar 24, 2007 at 00:56 UTC

    It doesn't follow the syntax example you have, but maybe Test::Output could do what you want.

Re: Testing a module's stdout/stderr
by grinder (Bishop) on Mar 24, 2007 at 09:59 UTC

    To test STDERR (that is, stuff that warn emits, as well as direct prints to STDERR), you can use Test::Warning. I use this for a couple of distributions, and it's great.

    Hmm... I didn't know about cpan:://Test::Output, which should solve both problems. Just goes to show, you can test just about anything these days.

    • another intruder with the mooring in the heart of the Perl

Re: Testing a module's stdout/stderr
by Sidhekin (Priest) on Mar 24, 2007 at 12:01 UTC

    ... or you could use my Test::Trap. :)

    use Test::More tests => 1; use Test::Trap; trap { print 'gabba gabba hey'; }; ok($trap->stdout eq 'gabba gabba hey') || diag "didn't print 'gabba gabba hey' to stdout.";

    Edit: On second thought, $trap->stdout_is('gabba gabba hey') may be more than you want. The good news is that ->stdout, as above, gives you all you were asking for.

    print "Just another Perl ${\(trickster and hacker)},"
    The Sidhekin proves Sidhe did it!