andreas1234567 has asked for the wisdom of the Perl Monks concerning the following question:
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 ( .. )
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Replace 'no_plan' with fixed number using Test::More when output is random
by Ovid (Cardinal) on Aug 17, 2007 at 08:16 UTC | |
by moritz (Cardinal) on Aug 17, 2007 at 08:49 UTC | |
|
Re: Replace 'no_plan' with fixed number using Test::More when output is random
by chakram88 (Pilgrim) on Aug 17, 2007 at 13:25 UTC | |
by pemungkah (Priest) on Aug 17, 2007 at 14:54 UTC | |
|
Re: Replace 'no_plan' with fixed number using Test::More when output is random
by belden (Friar) on Aug 17, 2007 at 16:50 UTC |