|Perl Monk, Perl Meditation|
Testing in real lifeby nbezzala (Scribe)
|on Dec 25, 2011 at 16:45 UTC||Need Help??|
Sometime in June 2011, I wrote BroadcastWeek.pm, a module to get the broadcast week for a given date.
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:
Merry Christmas :-)