Two-parter, both related to testing and module development:

First, how do others incorporate unit testing into their module work? The 'correct' way is to first write out the design of the code; then starting with the least user-code-dependant portion, write the testing code first, and THEN the module (eg, you write out it's usage, then you write the details of the function); unit test that particular piece of code, then continue to build on that until you've covered the entire code, making sure to unit test all previous code when adding new code to a project in case something has changed. Obviously this can be long and convoluted, but it should work. However, looking at much of the examples of perl modules out there, it would seem to me, at least, that testing is always a secondary feature that is completely written after the module is written for the first time. Has anyone gotten a reasonable approach for how to write test code for perl modules?

Second, when unit testing, particularly perl code, how much do you test? For example, say we have a simple function pythag($a,$b) which returns sqrt($a*$a+$b*$b). Given that a scalar can be a number of things, the code should check that $a and $b are valid numbers, and (optionally, but sake of example), $a and $b are positive numbers. Given this, there's at least three possible (4 many) numbers one can try for each: a negative number, zero, a small positive number, and a very large positive number (which when squared will overflow the maximum float value), for a total of 16 possibilities. But then you can consider the non-numericals; the empty value '', string scalars, and references. Obviously, in the code of the function, these can all be dealt with with a few if statments, but if you consider these 3 situations as 3 more cases to test, you're now at a total of 49 test situations, and you could easily extrapolate more. I would think at some point, there's a limit to what is overkill, but is there some practical point for this?

And I guess a bonus question: in testing modules that use DBI, is it first possible to figure out what DB server is installed and thus was DBI connect to provide, and is there a standard test database that one can use for DBI that can be assumed to be in place when the user ok's DBI testing, or should one just create and delete a new table when this step is done?

-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
It's not what you know, but knowing how to find it if you don't know that's important


In reply to Unit Testing and Perl Module Development by Masem

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.