My suggestion is to write more tests and label them very well. Then use prove -vb to run them when you want to see all the detail. Another good approach is to use diag but only when a test fails:

is( $thing->foo(), 42, "foo is 42" ) or diag $thing->as_string();

More generally, you may want to rethink your testing structure (or even your module structure). Personally, I like to do test-driven development. I write the tests first for each function, defining the expected outputs for a range of valid inputs to each argument. That lets me play-test the API up front. If I hate the API as I write the tests, I can refine it before I've even coded anything. Then I code it and make sure it works. Then I write tests for how the function handles invalid inputs.

This may seem like a lot of work, but it doesn't have to be if you put your inputs and outputs in a data structure and let your tests run in a simple loop. I elaborate a bit on this in Re^2: Wanted, more simple tutorials on testing.

If you feel that there are too many possible outputs to test them all, you may need to break your function down into several smaller units, each of which is easier to test. If you feel you need to trace what's happening in the middle of your code, that might be a warning sign that you're testing at too high a level.

If I still can't figure out why a test isn't working from examining the output of a function that is failing a test, that's when I use the debugger to jump to that part of my test script and then step through it. Sadly, prove doesn't make it easy to run the debugger</c>. You have to set an environment variable:

HARNESS_PERL_SWITCHES="-d"

Alternatively, if you're using ExtUtils::MakeMaker or Module::Build, they will set it for you:

$ make testdb TEST_FILE=t/mytest.t # ExtUtils::MakeMaker $ Build testdb --test_files t\02_trivial.t # Module::Build

-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: Testing Question by xdg
in thread Testing Question by pileofrogs

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.