Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
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
Replies are listed 'Best First'.
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 browsing the Monastery: (16)
As of 2015-07-30 13:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (271 votes), past polls