single |
FreezeGun: Let your Python tests travel through time
====================================================
FreezeGun is a library that allows your Python tests to travel through time
by mocking the datetime module.
Usage
-----
Once the decorator or context manager have been invoked, all calls to
datetime.datetime.now(), datetime.datetime.utcnow(), datetime.date.today(),
time.time(), time.localtime(), time.gmtime(), and time.strftime() will
return the time that has been frozen. time.monotonic() and
time.perf_counter() will also be frozen, but as usual it makes no
guarantees about their absolute value, only their changes over time.
Decorator
~~~~~~~~~
.. code-block:: python
from freezegun import freeze_time
import datetime
import unittest
# Freeze time for a pytest style test:
@freeze_time("2012-01-14")
def test():
assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
# Or a unittest TestCase - freezes for every test, and set up and tear
down code
@freeze_time("1955-11-12")
class MyTests(unittest.TestCase):
def test_the_class(self):
assert datetime.datetime.now() == datetime.datetime(1955, 11,
12)
# Or any other class - freezes around each callable (may not work in
every case)
@freeze_time("2012-01-14")
class Tester(object):
def test_the_class(self):
assert datetime.datetime.now() == datetime.datetime(2012, 1,
14)
# Or method decorator, might also pass frozen time object as kwarg
class TestUnitTestMethodDecorator(unittest.TestCase):
@freeze_time('2013-04-09')
def test_method_decorator_works_on_unittest(self):
self.assertEqual(datetime.date(2013, 4, 9),
datetime.date.today())
@freeze_time('2013-04-09', as_kwarg='frozen_time')
def test_method_decorator_works_on_unittest(self, frozen_time):
self.assertEqual(datetime.date(2013, 4, 9),
datetime.date.today())
self.assertEqual(datetime.date(2013, 4, 9),
frozen_time.time_to_freeze.today())
@freeze_time('2013-04-09', as_kwarg='hello')
def test_method_decorator_works_on_unittest(self, **kwargs):
self.assertEqual(datetime.date(2013, 4, 9),
datetime.date.today())
self.assertEqual(datetime.date(2013, 4, 9),
kwargs.get('hello').time_to_freeze.today())
Context manager
~~~~~~~~~~~~~~~
.. code-block:: python
from freezegun import freeze_time
def test():
assert datetime.datetime.now() != datetime.datetime(2012, 1, 14)
with freeze_time("2012-01-14"):
assert datetime.datetime.now() == datetime.datetime(2012, 1,
14)
assert datetime.datetime.now() != datetime.datetime(2012, 1, 14)
Raw use
~~~~~~~
.. code-block:: python
from freezegun import freeze_time
freezer = freeze_time("2012-01-14 12:00:01")
freezer.start()
assert datetime.datetime.now() == datetime.datetime(2012, 1, 14, 12, 0,
1)
freezer.stop()
Timezones
~~~~~~~~~
|