http://qs321.pair.com?node_id=843963


in reply to How to Test Output from the Standard Output

One way is to redirect stdout to a file. For this purpose it helps if the file is actually a Perl variable. Consider:

package Test_Package; use strict; sub testing { print "okay."; }; 1; use strict; use Test::More tests => 1; my $log; open my $logFile, '>', \$log; my $oldStdOut = select $logFile; Test_Package->testing(); select $oldStdOut; close $logFile; is($log, 'okay', 'Testing okay');

Prints:

1..1 not ok 1 - Testing okay # Failed test 'Testing okay' # at ...\noname.pl line 25. # got: 'okay.' # expected: 'okay' # Looks like you failed 1 test of 1.

The error is deliberate btw.

True laziness is hard work