I humbly ask the Monks for enlightenment on the subject of testing:

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__
# 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__
Now running the test illustrates the problem of having a random number of tests:
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 ( .. )
How would the enlightened Monks approach this problem? Should I accept having no_plan?
--
Andreas

In reply to Replace 'no_plan' with fixed number using Test::More when output is random by andreas1234567

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.