views:

50

answers:

2

I have a method calling localtime that I wrote a unit test for. I seed the call to localtime in my test so that I know the expected answers. So far, so good. I happen to run the unit test on a machine in a different timezone and, predictably, the tests fail, because they're some # of hours off. I suppose I could dynamically determine the correct expected values but that seems to go against the idea of knowing the answer before asking the question.

Any thoughts on how to approach this? Override localtime? That seems extreme.

Thanks!

+6  A: 

I don't think there's any problem with calculating the values of answers in a unit test. Unit tests should be simple enough to avoid complexity-related bugs, but a simple time zone calculation probably doesn't cross the line.

In this particular case, though, you could set the time zone to a fixed value (or several) as part of the test. See this Stack Overflow post about setting time zone in Perl.

Scott Stafford
tzset was awesome -- thanks for the pointer!
Joe Casadonte
+3  A: 

I'm not sure exactly what your requirements are, but you might be able to get something done using Test::MockTime or Time::Mock, both of which will replace localtime and friends with mocked versions that behave however you want them to.

hobbs
If tzset wasn't so easy to use, Test::MockTime seems like it would be perfect -- thanks!
Joe Casadonte