freezegun
Let your Python tests travel through time
I have a set of Python 3 unittests, which, when executed with this command line:
python3 -m unittest discover -f -v
...are generating a PendingDeprecationWarning:
/usr/local/Cellar/python3/3.4.2/Frameworks/Python.framework/Versions/3.4/lib/python3.4/imp.py:32:
PendingDeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
Is there an easy way for me to trace which peice of code is ultimately using imp.py
, perhaps by turning on some form of stack tracing? I have narrowed it down somewhat; it seems to only be triggered when I use freezegun. However, freezegun itself doesn't seem to use imp.
Source: (StackOverflow)
I'm puzzled why a function that freezes time with freezegun outputs different UTC times depending on whether datetime.datetime.utcnow()
is called, or datetime.datetime.now(pytz.utc)
. I'm not saying it's broken, just that I don't understand why, and would like to know!
eg, using this function:
@freeze_time("2012-01-14 03:21:34", tz_offset=-4)
def test():
print "utcnow(): %s" % datetime.datetime.utcnow()
print "pytz.utc: %s" % datetime.datetime.now(pytz.utc)
the output is:
utcnow(): 2012-01-14 03:21:34
pytz.utc: 2012-01-13 23:21:34+00:00
I guess the first is a naive datetime, but why are they different times?
(Ultimately why I want to know: if I'm using freezegun in my tests, and I use pytz to generate times in my code being tested, I want to know what its 'correct' behaviour should be.)
Source: (StackOverflow)
Im trying to use freezegun to set the clock back 10 seconds for a unit test.
I've found that setting the time to now with freezegun results in the expected behavior for datetime.datetime.now() but somewhat different behavior for time.time() in that the "frozen" time is ~30,000 seconds behind (8 hours).
Using datetime.datetime.utcnow(), its off by 3600 seconds (1 hour).
What do I need to do to appropriately mock the time to be used with time.time()?
Using now(): 8 hours off
def test_freezegun(self):
""" Test that freezegun can set time.time() back 10 seconds. """
with freezegun.freeze_time(datetime.datetime.now()):
print time.time()
print datetime.datetime.now()
print time.time()
print datetime.datetime.now()
# Output
[08:09:32] 1436339372
[08:09:32] 2015-07-08 08:09:32.119516
[08:09:32] 1436368172
[08:09:32] 2015-07-08 08:09:32.175031
Using utcnow(): 1 hour off
def test_freezegun(self):
""" Test that freezegun can set time.time() back 10 seconds. """
with freezegun.freeze_time(datetime.datetime.utcnow()):
print time.time()
print datetime.datetime.now()
print time.time()
print datetime.datetime.now()
# Output
[08:08:56] 1436364536
[08:08:56] 2015-07-08 15:08:56.589202
[08:08:56] 1436368136
[08:08:56] 2015-07-08 08:08:56.655346
UPDATE:
This works, but I'd like to understand why:
Using utcnow() + 1 hour: identical time.time() output, as expected
def test_freezegun(self):
""" Test that freezegun can set time.time() back 10 seconds. """
with freezegun.freeze_time(datetime.datetime.utcnow() + datetime.timedelta(hours=1)):
print time.time()
print datetime.datetime.now()
print time.time()
print datetime.datetime.now()
#Output
[08:22:27] 1436368947
[08:22:27] 2015-07-08 16:22:27.268315
[08:22:27] 1436368947
[08:22:27] 2015-07-08 08:22:27.339116
Source: (StackOverflow)