We have a lot of unit test code that doesn't really make use of any nicities such as Test::Class, Test::MockObject or even Test::More (it's mostly based on Test). As a result, there's a lot of messy, verbose and duplicated code. I've decided to make it my mission to get this tidied up, which is daunting, but I think will help my sanity long term.

Adapting what we have to Test::Class should be reasonably easy, it's just a matter of doing a large scale re-factor. While I haven't nailed down the exact details (which will probably require some input from my colleagues), I'm thinking we'll organise the test classes by module, e.g. Foo::Bar will have a corresponding Foo::Bar::Test

My problem is, we have lots of test code like this:

my @Tests = ( { 'desc' => 'failure', 'params' => {}, 'result' => "some error msg", }, { 'desc' => 'success', 'params' => {bar=>1, baz=2}, 'result' => "ok", }, ); foreach my $test (@Tests) { ok( sub { Foo->new($test->{params}); 'ok'; }; 'ok', "test new() " . ( exists( $test->{'desc'} ) ? " - $test->{'desc' +}" : '' ) ); }

Obviously, this is a fairly trivial example - more things can end up in the foreach loop to set up things before the method we're testing is called. My problem with this way of doing this is it makes the tests quite hard to read. I have to look at things carefully to figure out what's going on.

However, I'm not really sure of a good alternative. The setup methods in Test::Class aren't really much use, because it's only relevant to this particular method. I could break the test classes down into one for each method in a class, but that might be a bit too much fragmentation.

So does anyone else have a good solution for organising these types of tests?


In reply to 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.