I came across this question when writing a test script for a function that returns an (at the time of the test) unknown (possibly random) number of values (e.g. a list where the list length is random, like in the sample below). I can currently see no way to test this function with Test::More except using the no_plan pragma. The documentation says that using no_plan is generally A Bad Idea(tm).
In this case, you can declare that you have no plan. (Try to avoid using this as it weakens your test.)A small sample:
# Dyn.pm package Dyn; use strict; use warnings; # Return a list with a random number of elements sub get_list { my @arr = (); push @arr, $_ for (0 .. int(rand(10))); return @arr; } 1; __END__
Now running the test illustrates the problem of having a random number of tests:# Dyn.t use Test::More qw ( no_plan ); use Dyn; my @arr = Dyn::get_list(); if (@arr) { foreach my $elem (@arr) { like($elem, qr/^\d+$/, q{Expect an integer}); } } __END__
How would the enlightened Monks approach this problem? Should I accept having no_plan?Dyn....ok All tests successful. Files=1, Tests=5, 0 wallclock secs ( .. ) $ prove Dyn.t Dyn....ok All tests successful. Files=1, Tests=10, 0 wallclock secs ( .. ) $ prove Dyn.t Dyn....ok All tests successful. Files=1, Tests=7, 0 wallclock secs ( .. )
In reply to Replace 'no_plan' with fixed number using Test::More when output is random by andreas1234567
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |