package sb::redirection_test; use strict; use warnings; use Test::Builder; use Test::More; # # Redirect and capture output # and diagnostocs from tests # 1; sub new { my $class = shift; my $self = {}; bless $self, $class; return $self; } sub test_sites { my $self = shift; my $sites = shift; my $log_file = '/tmp/test_sites_output'; my $log_file_ok = '/tmp/test_sites_output_ok'; my $log_file_diag = '/tmp/test_sites_output_diag'; my $log_file_todo = '/tmp/test_sites_output_todo'; my $Test = Test::Builder->new; my @handle_names = qw/ output failure_output todo_output /; my %old; $old{$_} = $Test->$_ for @handle_names; $Test->$_(\*STDOUT) for @handle_names; { $Test->output($log_file_ok); $Test->failure_output($log_file_diag); $Test->todo_output($log_file_todo); is(1,1,'One is one.'); is(1,2,'One is two.'); like('These tests are run with output redirected',qr/tests/,'tests is like tests w / redirected output.'); like('These tests are run with output redirected',qr/tarts/,'tests is like tarts w / redirected output.'); diag("We're redirecting test diagnostics here."); print "We're redirecting STDOUT here.\n"; print STDERR "We're redirecting STDERR here.\n"; $self->_test_test(); } $Test->todo_output(*STDOUT); $Test->failure_output(*STDERR); $Test->output(*STDOUT); return; } sub _test_test { is(3,3,'Three is three.'); is(3,5,'Three is five.'); like('These tests are run with output redirected',qr/tests/,'tests is like tests in su broutine w/ redirected output.'); like('These tests are run with output redirected',qr/tarts/,'tests is like tarts in su broutine w/ redirected output.'); diag("We're redirecting test diagnostics here while inside a subroutine call."); print "We're redirecting STDOUT here while inside a subroutine call.\n"; print STDERR "We're redirecting STDERR here while inside a subroutine call.\n"; return; }