I use this style of testing quite often. (C.f. Re^2: Wanted, more simple tutorials on testing) While it's not the most elegant, I find it quick to get started this way before I've really decided how the tests will evolve and what the common setup/teardown will be and then it's relatively easy to extend and refactor as needed.

I often put one or more *.pm files in the "t" directory with either helper functions or to to encapsulate repetitive tests. This is where Test::More with Test::Builder and $Test::Builder::Level are quite helpful. Essentially, you can create custom test modules on the fly as needed to refactor cut-and-paste tests from multiple parts of your *.t files.

# foo.t use strict; use warnings; use Test::More; # don't put a plan here use t::Helper qw/multi_test/; # i.e. t/Helper.pm my @cases = ( { label => "describe this case", input => "data to operate on", output => "expected result", }, # ... repeat as needed ... ); # set plan after populating @cases my $tests_per_case = 2; plan tests => 1 + $tests_per_case * @cases; # start testing here require_ok( "Some::Object" ); for my $case ( @cases ) { my $obj = Some::Object->new; multi_test( $obj, $case ); }
# t/Helper.pm use strict; use warnings; use base 'Exporter'; our @EXPORT = qw/multi_test/; sub multi_test { my ($obj, $case) = @_; local $Test::Builder::Level = $Test::Builder::Level + 1; is( $obj->wibble( $case->{input} ), $case->{output}, "wibble: $case->{label}" ); ok( $obj->wobble, "wobble: $case->{label} ); } 1;

You can see a range of examples in the *.t files in some of my CPAN modules:

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.


In reply to Re: Do you use loops in your unit tests? by xdg
in thread Do you use loops in your unit tests? by Mutant

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.