cees has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Test::More and fork
by diotalevi (Canon) on Jun 22, 2005 at 16:34 UTC | |
by cees (Curate) on Jun 23, 2005 at 00:27 UTC | |
by diotalevi (Canon) on Jun 24, 2005 at 04:58 UTC | |
by adrianh (Chancellor) on Jun 24, 2005 at 13:21 UTC | |
|
Re: Test::More and fork
by bluto (Curate) on Jun 22, 2005 at 18:12 UTC | |
|
Re: Test::More and fork
by adrianh (Chancellor) on Jun 23, 2005 at 17:32 UTC | |
by cees (Curate) on Jun 23, 2005 at 19:05 UTC | |
by adrianh (Chancellor) on Jun 23, 2005 at 23:10 UTC |