http://www.perlmonks.org?node_id=945069

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 :-)