Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Perl Test - Capture print statements

by iamdaboss (Novice)
on Jul 10, 2007 at 21:10 UTC ( [id://625928]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I wanted to know how to capture print statements using Perl test scripts. Example : There is a PM xyz.pm whose subroutines print statements. I need to write xyz.t test script which captures those print statements and checks them if they are right. Note: - I am not supposed to use Test::Expect. - The module and test script would be part of the test harness. Thank You.

Replies are listed 'Best First'.
Re: Perl Test - Capture print statements
by Zaxo (Archbishop) on Jul 10, 2007 at 21:34 UTC

    You could localize STDOUT, opening it to a variable.

    my $out = do { my $msg; open local( *STDOUT), '>', \$msg or die $!; mytest(); $msg };
    That takes Perl 5.8+ for opening a handle to a variable.

    ++fenLisesi caught a thinko in text, s/STDERR/STDOUT/. Repaired.

    After Compline,
    Zaxo

Re: PerlTest - Capture print statements
by Corion (Patriarch) on Jul 10, 2007 at 21:19 UTC
Re: Perl Test - Capture print statements
by trwww (Priest) on Jul 10, 2007 at 21:50 UTC

    The following has worked well for me:

    use IO::String; ... my $add_io = IO::String->new; my $old_handle = select($add_io); ok( $app->run, 'ran app' ); select($old_handle); like( ${$add_io->string_ref}, qr|Location:|, q|got application redirect| ); undef $add_io;

    This stores whatever $app->run sends to STDOUT in $add_io.
Re: Perl Test - Capture print statements
by schwern (Scribe) on Jul 12, 2007 at 08:51 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://625928]
Approved by marto
Front-paged by naikonta
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-12-08 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (50 votes). Check out past polls.