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

spurperl has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks,

I want to redirect the output of my Test::More printouts to a text file instead of STDOUT / STDERR. I many "is/ok/like" calls in the code running in loops and the output is often thousands of tests long.

Running "test.pl > file.txt" on Windows fails (an error from inside Test::Builder). How can this be done ?

thanks in advance

Replies are listed 'Best First'.
Re: Redirect output of Test::More/Simple
by GrandFather (Saint) on Aug 28, 2006 at 09:40 UTC

    Add:

    Test::More->builder->output ('result.txt');

    to have Test::More put it's output in a file called 'result.txt' and:

    Test::More->builder->failure_output ('errors.txt');

    to redirect error output to 'errors.txt'. See the Test::More section 'Extending and Embedding Test::More' and Test::Builder for more information.


    DWIM is Perl's answer to Gödel
      Thanks.

      It seems to me that it's a bit too hidden for such a needed feature. I'm not a Perl newbie and even have experience with Test::More and I found it difficult to locate in the docs.

      Besides the most trivial cases, lots of tests usually run autonomously, and generate copious amounts of output which is best contained in a log file.

      I guess another solution in such cases is Test::Harness, which IIRC allows redirection of output from test scripts it runs.

Re: Redirect output of Test::More/Simple
by lyklev (Pilgrim) on Aug 28, 2006 at 18:25 UTC

    Redirecting output on win32 from a script called by just its name is broken, see the release notes.

    Two fixes are possible:

    1. perl test1.pl > file.txt
    2. use pl2bat, shipped with activestate perl. This will wrap your perl-script in a .bat-file, from which redirecting will work;
      pl2bat test1.pl test1.bat > file.txt
      Redirecting output on win32 from a script called by just its name is broken, see the release notes.
      C:\temp>cat foo.pl #!/usr/bin/perl -l use strict; use warnings; print 'Foo'; __END__ C:\temp>foo Foo C:\temp>foo >foo.txt C:\temp>cat foo.txt Foo
      use pl2bat, shipped with activestate perl. This will wrap your perl-script in a .bat-file, from which redirecting will work;

      OTOH I'm new on cmd.exe, I've been on command.com (when using Windows) till just about one month ago, and there I had to use pl2bat, with which for some reason redirecting did not work. Which was a major pita and caused me to implement -i, -o switches for some of my commonly used scripts.

Re: Redirect output of Test::More/Simple
by mrpeabody (Friar) on Aug 28, 2006 at 17:29 UTC
    (an error from inside Test::Builder)

    What error, exactly?

    Redirecting output from Test::More works fine under Cygwin Perl with bash or cmd.exe. Perhaps the problem is with Activestate Perl.

Re: Redirect output of Test::More/Simple
by larsen (Parson) on Aug 28, 2006 at 16:46 UTC
Re: Redirect output of Test::More/Simple
by petdance (Parson) on Aug 28, 2006 at 22:08 UTC
    Without knowing the error you got, we can't do much to help. However, running "perl foo.t > output.txt" should always work.

    xoxo,
    Andy

      This code:

      use warnings; use strict; $|++; use Test::More qw/no_plan/; ok(1);

      When run as "z.pl > h.txt" in the command prompt (cmd), triggers the following error (tried on two different machines with different versions of Windows and ActivePerl):

      Can't dup STDOUT: Permission denied at D:/Perl/lib/Test/Builder.pm li +ne 1322. Compilation failed in require at D:/Perl/lib/Test/Builder/Module.pm li +ne 3. BEGIN failed--compilation aborted at D:/Perl/lib/Test/Builder/Module.p +m line 3. Compilation failed in require at D:/Perl/lib/Test/More.pm line 22. BEGIN failed--compilation aborted at D:/Perl/lib/Test/More.pm line 22. Compilation failed in require at C:\eli\perl_stuff\z.pl line 4. BEGIN failed--compilation aborted at C:\eli\perl_stuff\z.pl line 4.

      However, running "perl z.pl > h.txt" works fine.