I suggest finding a (simple) CPAN module you like and examining the tests in its distribution.

The way to figure out what tests to write:

  1. Decide what you want a function to do.
  2. Write the documentation that says "foo() does this when given that"
  3. Write tests in a test file (I would name it descriptively like "t/functions/001-foo.t") that prove that "foo()" does what the doc says. They will fail.
  4. Write the code that makes the tests pass.

Running tests inside an editor session seems silly to me (but a lot of what TheDamian does and advocates is both way too dogmatic and way too clever for a simple monk like me). I run my tests in a separate shell session, and while they are running I am doing something else useful with the editor or in another shell.

One thing I strongly suggest is to make your test files not only thematic, but small and simple. Some test files will consist almost completely of test statements ("ok(), is_deeply()" etc) ... in which case it's fine to have lots of tests therein. You might create tests in a loop as well. But once you need even a few lines of code to provide data for the test or make the test run, I think it's much better to have one test per file, since you will probably turn on verbosity as soon as you are working with that test, and the output of all the others will be too noisy.

Hope this helps!


The way forward always starts with a minimal test.

In reply to Re: How do you structure and run module test code? by 1nickt
in thread How do you structure and run module test code? by nysus

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.