Sometime in June 2011, I wrote BroadcastWeek.pm, a module to get the broadcast week for a given date.
The input is a datetime object, and if it is not provided, it works with DateTime->now().

In August, I realized that this was a very important module for my projet, and so I wrote tests for it, after asking here - How to test a Moose Role?.

I wrote many tests for all the dates in one week in the current year, and many other dates in the beginning and the end of the the last year, this year and the next three years. All tests passed. I didn't expect any trouble from this module at least for the next three years.

And yet, on December 25th 2011, the last day of the last week of the broadcast year 2011, the module returned the week 12-01 instead of 11-52.

I ran the tests again, and again the tests passed. I checked and saw that one of the tests actually tested for 25th December 2011.

I printed the date in my module where it was failing and it said 2011-12-25. I printed the dates on the test file and it too said 2011-12-25.

So, where was the problem?

Since the value of now() changes all the time, I could only test for hard coded dates. And all my tests checked for a given date. Never for a date and a time. But now() gives me the current date with the time.

I added a test for December 25th, 2011 3:40 PM and ran it, and this time, it failed.

So, I changed the module to convert the date it receives into just the date without the time. And to use today() instead of now().

And so, all tests pass again.

I'm hoping that this post helps someone who is starting with automated testing answer these questions:
1. When should I write tests?
2. What should I test?

Merry Christmas :-)


In reply to Testing in real life by nbezzala

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.