http://www.perlmonks.org?node_id=690242

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 ); }; }

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 (Paladin) on Jun 05, 2008 at 14:13 UTC