Test::Fork(3pm)
π Test::Fork π
okay so bestie, this is the Test::Fork page from the User Contributed Perl Documentation, and it's literally here to help you test code that forks. no cap. π¦
β¨ NAME β¨
Test::Fork β test code which forks π
πΈ SYNOPSIS πΈ
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");
π DESCRIPTION π
β οΈ THIS IS ALPHA CODE! The implementation is unreliable and the interface is subject to change. π like, for real, do not build your whole empire on this yet.
so here's the vibe: every test has a number attached to it, which makes testing code that forks deeply annoying. coordinating the test number between the parent process and the child processes is complicated, it's giving chaos honestly. Test::Fork hands you a function that smooths over all those complications. you're welcome. π
π§ Functions
each function is exported by default. no imports needed, they just show up. iconic.
fork_ok
my $child_pid = fork_ok( $num_tests, sub {
...child test code...
});
runs the child test code you gave it in a forked process. it returns the pid of the forked child process, or false if the fork fails. β¨
$num_tests is how many tests live inside your child test code. think of it as a sub-plan, like a plan but smaller and nested. π
fork_ok() is itself a test, so if the fork fails, it will fail. and heads up: the fork_ok() test does not count towards your $num_tests. it's its own thing, main character energy.
# This is three tests.
fork_ok( 2, sub {
is $foo, $bar;
ok Something->method;
});
the children are automatically reaped. π«Ά they're taken care of, don't worry about it.
π CAVEATS π
okay so this is the part where we get real. π
- the failure of tests in a child process cannot be detected by the parent. so the normal end-of-test reporting that
Test::Builderdoes will not notice failed child tests. they just... slip away. π Test::Forkturns off test numbering to avoid test counter coordination issues. it turns it back on once the children are done running. on, off, on. slay.Test::Forkwill wait for all your child processes to complete at the end of the parent process. it's not leaving until everybody's home. π
π SEE ALSO π
Test::MultiFork
π AUTHOR π
Michael G Schwern <schwern@pobox.com>
π BUGS and FEEDBACK π
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. π₯Ί (and honestly? same energy. feedback is the love language.)
π COPYRIGHT and LICENSE π
Copyright 2007-2008 by Michael G Schwern <schwern@pobox.com>.
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
and that's the whole page, bestie π this one shipped as perl v5.36.0 on 2022-10-13 β go forth, fork responsibly, and remember: the children are automatically reaped, but your test failures in them are lowkey invisible. periodt. πβ¨
π¦ where this page comes from
- ubuntu noble: libtest-fork-perl 0.02-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libtest-fork-perl 0.02-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libtest-fork-perl 0.02-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms