I am having some issues getting Test::More to work properly when the test script needs to fork to do some of the tests. I require this in the test scripts for my Data::FormValidator::Filters::Image module which needs to simulate a file upload. I have the test suite working by just printing "ok 1" directly (See here), but would rather use Test::More if I can.

Here is some example code that shows the problem:

use Test::More tests => 2; ok(1, "First test"); $pid = open( CHILD, "|-" ); $SIG{PIPE} = 'IGNORE'; if ($pid) { # parent print CHILD "test\n"; close CHILD; exit 0; } # child process ok(1, "Second test");

The output is as follows:

1..2 ok 1 - First test ok 2 - Second test # Looks like you planned 2 tests but only ran 1.

Notice that the test did execute, and it did print to STDOUT, but Test::More didn't see it, hence it wasn't counted.

Here is the same test without using Test::More:

print "1..2\n"; print "ok 1 - First test\n"; $pid = open( CHILD, "|-" ); $SIG{PIPE} = 'IGNORE'; if ($pid) { # parent print CHILD "test\n"; close(CHILD); exit 0; } print "ok 2 - Second test\n";

Obviously this second test will just output the simple printed data to STDOUT, but to highlight that it works properly, you can run it through Test::Harness to see that it works OK, and that the Test::More version fails:

# perl -MTest::Harness -e 'runtests("test_plain_fork.t", "test_more_fo +rk.t")' test_plain_fork....ok test_more_fork.....ok 2/2# Looks like you planned 2 tests but only ran + 1. test_more_fork.....dubious Test returned status 1 (wstat 256, 0x100) after all the subtests completed successfully Failed Test Stat Wstat Total Fail Failed List of Failed ---------------------------------------------------------------------- +--------- test_more_fork.t 1 256 2 0 0.00% ?? Failed 1/2 test scripts, 50.00% okay. 0/4 subtests failed, 100.00% oka +y.

I have a feeling this is all because of Test::Builder, which has this comment in the docs:

It's ok for your test to change where STDOUT and STDERR point to, Test::Builder's default output settings will not be affected.

If that is the problem, is there any way around it? If it isn't the problem, what am I doing wrong?


In reply to Test::More and fork by cees

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.