Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unit test functions calling localtime() in Perl?

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!

like image 963
Joe Casadonte Avatar asked Jul 14 '10 01:07

Joe Casadonte


1 Answers

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.

like image 135
Scott Stafford Avatar answered Oct 20 '22 19:10

Scott Stafford