Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Results printing multiple times when testcase with fork using Test::more module

by thiagu_mvt (Sexton)
on May 20, 2009 at 13:52 UTC ( #765225=perlquestion: print w/replies, xml ) Need Help??

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

When we wrote testcase that has forking inside results are printed twice. Can we correct this ?
[host1]$ cat test.pl use Test::More tests => 2; sub for{ my $pid = fork; if($pid == 0 ){ }else{} waitpid($pid,0); } $a = "hi"; ok ("hi" eq $a); ok("1" eq &for());
[host1]$ perl test.pl 1..2 ok 1 not ok 2 # Failed test (test.pl at line 14) not ok 2 # Failed test (test.pl at line 14) # Looks like you failed 1 tests of 2.
Thanks,
Thiagu

Replies are listed 'Best First'.
Re: Results printing multiple times when testcase with fork using Test::more module
by shmem (Chancellor) on May 20, 2009 at 14:12 UTC

    Since you fork, you spawn another process from the current process and thus have two results. Why do you fork if you want just one run of the test?

    If you want the child to terminate before the evaluation of the last ok() test, your sub should look like:

    sub for{ my $pid = fork; if($pid == 0 ){ diag "$$: 0\n"; exit; }else{ diag "$$: 1\n"; waitpid($pid,0); } 1; }
      This is just an example. My actual program calls an API function which is doing the forking through 'open3'. So not my intention or in control.
Re: Results printing multiple times when testcase with fork using Test::more module
by ELISHEVA (Prior) on May 20, 2009 at 18:57 UTC
    Have you considered using Test::Fork? It is supposed to coordinate test counts across multiple threads, but I don't know how well it works.

    Best, beth

      I have not heard of it before. Will surely check it and see whether it can help my need.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://765225]
Approved by AnomalousMonk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2022-06-30 18:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (98 votes). Check out past polls.

    Notices?