Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Duplicate Test:: output

by tye (Cardinal)
on Jun 04, 2008 at 20:49 UTC ( #690242=snippet: print w/ replies, xml ) Need Help??

Description:

I have some tests that include a lot of 'diagnostic' output so, by default, STDERR gets redirected to a file so I can easily go look at the details if there is a failure.

The "ok" vs "not ok" messages all need to go to STDOUT which either goes to my interactive session or to a test harness. But they would be extremely useful on STDERR to provide milestones in the detailed output. So I went looking for how to get them written to both STDOUT and STDERR.

I often find it at least mildly entertaining the rather convoluted OO-koolaidesque framework that most of the Test:: modules are now part of. I suspect that there is some way that I can define a subclass and convince Test::More to use it such that I can have "ok" message go to two different file handles. However, that wasn't obvious while the following hack was obvious to me.

Given that the functionality I wanted to replace was named "_print" (note the leading underscore), I also figured that overriding it in a subclass isn't officially sanctioned anyway. (Seems like something not unreasonable to want to override, tho.)

Replies noting the "proper" way to do something like this welcome. :)

if( (stat \*STDOUT)[1] != (stat \*STDERR)[1] ) {
    # Send "ok" messages to both STDOUT and STDERR if they are differe
+nt
    my $print= \&Test::Builder::_print;
    *Test::Builder::_print= sub {
        my( $self )= shift @_;
        $self->output( \*STDOUT );
        $self->$print( @_ );
        $self->output( \*STDERR );
        $self->$print( @_ );
        $self->output( \*STDOUT );
    };
}
Comment on Duplicate Test:: output
Download Code
Re: Duplicate Test:: output
by chromatic (Archbishop) on Jun 05, 2008 at 06:07 UTC
    Replies noting the "proper" way to do something like this welcome.

    If this were my project, I'd use TAP::Harness to collect the information and worry about filehandles... but it's not clear if you need pre- or post-processed TAP.

Re: Duplicate Test:: output
by jdporter (Canon) on Jun 05, 2008 at 14:13 UTC

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://690242]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2014-09-19 22:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (151 votes), past polls