How to re-direct STDOUT and STDERR into file and on screen while running perl test case

417 views Asked by At

I have written simple Perl test case Example:

use Test::More;
is(2 + 4, 5, "Addition check"); 

output on the screen:

Failed test 'Addition check'
at t/simpel_perl_test.t line 2.
got: '6'
expected: '5'

Now I want to print a message in log file and on screen ?

I am not able use existing Perl modules IO::Tee in <test case name>.t to achieve my requirement.

Could you tell how could I write a message on screen and in a log file in Perl test case?

Command used:

prove -r --timer t/simpel_perl_test.t :: -d 
1

There are 1 answers

2
dlamblin On

The IO::Capture modules were written exactly for doing this during test execution. Start by reading IO::Capture::Overview. However you appear to want the test harnesses output to be sent to a file instead. Maybe you want to provide a formatter argument to prove that could write to a file. You could follow up the prove invocation with cating the file. Try the TAP::Formatter::File module as your argument, though I can't easily see how the file gets named.

Wouldn't it just be best to use the tee command for this?

$ prove -r --timer simple_perl_test.t :: -d |tee simple_perl_test.txt
[02:08:12] simple_perl_test.t .. ok       24 ms ( 0.00 usr  0.00 sys +  0.02 cusr  0.00 csys =  0.02 CPU)
[02:08:12]
All tests successful.
Files=1, Tests=1,  0 wallclock secs ( 0.02 usr  0.01 sys +  0.02 cusr  0.00 csys =  0.05 CPU)
Result: PASS
$ cat simple_perl_test.txt 
[02:08:12] simple_perl_test.t .. ok       24 ms ( 0.00 usr  0.00 sys +  0.02 cusr  0.00 csys =  0.02 CPU)
[02:08:12]
All tests successful.
Files=1, Tests=1,  0 wallclock secs ( 0.02 usr  0.01 sys +  0.02 cusr  0.00 csys =  0.05 CPU)
Result: PASS
$