Test::Fork(3) test code which forks


use Test::More tests => 4;
use Test::Fork;

fork_ok(2, sub{
pass("Test in the child process");
pass("Another test in the child process");

pass("Test in the parent");


THIS IS ALPHA CODE! The implementation is unreliable and the interface is subject to change.

Because each test has a number associated with it, testing code which forks is problematic. Coordinating the test number amongst the parent and child processes is complicated. Test::Fork provides a function to smooth over the complications.


Each function is exported by default.


    my $child_pid = fork_ok( $num_tests, sub {
        ...child test code...

Runs the given child test code in a forked process. Returns the pid of the forked child process, or false if the fork fails.

$num_tests is the number of tests in your child test code. Consider it to be a sub-plan.

fork_ok() itself is a test, if the fork fails it will fail. fork_ok() test does not count towards your $num_tests.

    # This is three tests.
    fork_ok( 2, sub {
        is $foo, $bar;
        ok Something->method;

The children are automatically reaped.


The failure of tests in a child process cannot be detected by the parent. Therefore, the normal end-of-test reporting done by Test::Builder will not notice failed child tests.

Test::Fork turns off test numbering in order to avoid test counter coordination issues. It turns it back on once the children are done running.

Test::Fork will wait for all your child processes to complete at the end of the parent process.


Michael G Schwern <[email protected]>


Please send all bugs and feature requests to bug-Test-Fork at rt.cpan.org or use the web interface via <http://rt.cpan.org>.

If you use it, please send feedback. I like getting feedback.


Copyright 2007-2008 by Michael G Schwern <[email protected]>.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See http://www.perl.com/perl/misc/Artistic.html