I have a web application with a "click here if you forgot your password" feature. When clicked, it generates a new, random password and emails it to the user. It's time to write some unit tests.

I can test that the email gets properly generated by using Test::MockObject to make a mock MIME::Lite so I don't have to really send an email. However, I can't think of a good way to test the password-generating function itself.

The random password function is quite simple. It picks a random word from a file and then sticks a random three-digit number on the end. How do I write a test that can tell the difference between a "good" random result, and something erroneous? I could regex the result and check if it looks right, but that seems like a cop-out.

Lest you think this concern is trivial, it is only a small example. I have several other non-deterministic behaviors which are more complex and do require some more rigorous testing. I wanted to start with this example so I have some ideas about how to proceed with the harder ones.


In reply to Unit Testing Non-Deterministic Behaviors by friedo

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.