README.rst 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. FreezeGun: Let your Python tests travel through time
  2. ====================================================
  3. .. image:: https://img.shields.io/pypi/v/freezegun.svg
  4. :target: https://pypi.python.org/pypi/freezegun/
  5. .. image:: https://secure.travis-ci.org/spulec/freezegun.svg?branch=master
  6. :target: https://travis-ci.org/spulec/freezegun
  7. .. image:: https://coveralls.io/repos/spulec/freezegun/badge.svg?branch=master
  8. :target: https://coveralls.io/r/spulec/freezegun
  9. FreezeGun is a library that allows your Python tests to travel through time by mocking the datetime module.
  10. Usage
  11. -----
  12. 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.
  13. Decorator
  14. ~~~~~~~~~
  15. .. code-block:: python
  16. from freezegun import freeze_time
  17. import datetime
  18. import unittest
  19. @freeze_time("2012-01-14")
  20. def test():
  21. assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
  22. # Or a unittest TestCase - freezes for every test, from the start of setUpClass to the end of tearDownClass
  23. @freeze_time("1955-11-12")
  24. class MyTests(unittest.TestCase):
  25. def test_the_class(self):
  26. assert datetime.datetime.now() == datetime.datetime(1955, 11, 12)
  27. # Or any other class - freezes around each callable (may not work in every case)
  28. @freeze_time("2012-01-14")
  29. class Tester(object):
  30. def test_the_class(self):
  31. assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
  32. Context manager
  33. ~~~~~~~~~~~~~~~
  34. .. code-block:: python
  35. from freezegun import freeze_time
  36. def test():
  37. assert datetime.datetime.now() != datetime.datetime(2012, 1, 14)
  38. with freeze_time("2012-01-14"):
  39. assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
  40. assert datetime.datetime.now() != datetime.datetime(2012, 1, 14)
  41. Raw use
  42. ~~~~~~~
  43. .. code-block:: python
  44. from freezegun import freeze_time
  45. freezer = freeze_time("2012-01-14 12:00:01")
  46. freezer.start()
  47. assert datetime.datetime.now() == datetime.datetime(2012, 1, 14, 12, 0, 1)
  48. freezer.stop()
  49. Timezones
  50. ~~~~~~~~~
  51. .. code-block:: python
  52. from freezegun import freeze_time
  53. @freeze_time("2012-01-14 03:21:34", tz_offset=-4)
  54. def test():
  55. assert datetime.datetime.utcnow() == datetime.datetime(2012, 1, 14, 3, 21, 34)
  56. assert datetime.datetime.now() == datetime.datetime(2012, 1, 13, 23, 21, 34)
  57. # datetime.date.today() uses local time
  58. assert datetime.date.today() == datetime.date(2012, 1, 13)
  59. @freeze_time("2012-01-14 03:21:34", tz_offset=-datetime.timedelta(hours=3, minutes=30))
  60. def test_timedelta_offset():
  61. assert datetime.datetime.now() == datetime.datetime(2012, 1, 13, 23, 51, 34)
  62. Nice inputs
  63. ~~~~~~~~~~~
  64. FreezeGun uses dateutil behind the scenes so you can have nice-looking datetimes.
  65. .. code-block:: python
  66. @freeze_time("Jan 14th, 2012")
  67. def test_nice_datetime():
  68. assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
  69. Function and generator objects
  70. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  71. FreezeGun is able to handle function and generator objects.
  72. .. code-block:: python
  73. def test_lambda():
  74. with freeze_time(lambda: datetime.datetime(2012, 1, 14)):
  75. assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
  76. def test_generator():
  77. datetimes = (datetime.datetime(year, 1, 1) for year in range(2010, 2012))
  78. with freeze_time(datetimes):
  79. assert datetime.datetime.now() == datetime.datetime(2010, 1, 1)
  80. with freeze_time(datetimes):
  81. assert datetime.datetime.now() == datetime.datetime(2011, 1, 1)
  82. # The next call to freeze_time(datetimes) would raise a StopIteration exception.
  83. ``tick`` argument
  84. ~~~~~~~~~~~~~~~~~
  85. FreezeGun has an additional ``tick`` argument which will restart time at the given
  86. value, but then time will keep ticking. This is alternative to the default
  87. parameters which will keep time stopped.
  88. .. code-block:: python
  89. @freeze_time("Jan 14th, 2020", tick=True)
  90. def test_nice_datetime():
  91. assert datetime.datetime.now() > datetime.datetime(2020, 1, 14)
  92. ``auto_tick_seconds`` argument
  93. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  94. FreezeGun has an additional ``auto_tick_seconds`` argument which will autoincrement the
  95. value every time by the given amount from the start value. This is alternative to the default
  96. parameters which will keep time stopped. Note that given ``auto_tick_seconds`` the ``tick`` parameter will be ignored.
  97. .. code-block:: python
  98. @freeze_time("Jan 14th, 2020", auto_tick_seconds=15)
  99. def test_nice_datetime():
  100. first_time = datetime.datetime.now()
  101. auto_incremented_time = datetime.datetime.now()
  102. assert first_time + datetime.timedelta(seconds=15) == auto_incremented_time
  103. Manual ticks
  104. ~~~~~~~~~~~~
  105. FreezeGun allows for the time to be manually forwarded as well.
  106. .. code-block:: python
  107. def test_manual_increment():
  108. initial_datetime = datetime.datetime(year=1, month=7, day=12,
  109. hour=15, minute=6, second=3)
  110. with freeze_time(initial_datetime) as frozen_datetime:
  111. assert frozen_datetime() == initial_datetime
  112. frozen_datetime.tick()
  113. initial_datetime += datetime.timedelta(seconds=1)
  114. assert frozen_datetime() == initial_datetime
  115. frozen_datetime.tick(delta=datetime.timedelta(seconds=10))
  116. initial_datetime += datetime.timedelta(seconds=10)
  117. assert frozen_datetime() == initial_datetime
  118. Moving time to specify datetime
  119. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  120. FreezeGun allows moving time to specific dates.
  121. .. code-block:: python
  122. def test_move_to():
  123. initial_datetime = datetime.datetime(year=1, month=7, day=12,
  124. hour=15, minute=6, second=3)
  125. other_datetime = datetime.datetime(year=2, month=8, day=13,
  126. hour=14, minute=5, second=0)
  127. with freeze_time(initial_datetime) as frozen_datetime:
  128. assert frozen_datetime() == initial_datetime
  129. frozen_datetime.move_to(other_datetime)
  130. assert frozen_datetime() == other_datetime
  131. frozen_datetime.move_to(initial_datetime)
  132. assert frozen_datetime() == initial_datetime
  133. @freeze_time("2012-01-14", as_arg=True)
  134. def test(frozen_time):
  135. assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
  136. frozen_time.move_to("2014-02-12")
  137. assert datetime.datetime.now() == datetime.datetime(2014, 2, 12)
  138. Parameter for ``move_to`` can be any valid ``freeze_time`` date (string, date, datetime).
  139. Default arguments
  140. ~~~~~~~~~~~~~~~~~
  141. Note that FreezeGun will not modify default arguments. The following code will
  142. print the current date. See `here <http://docs.python-guide.org/en/latest/writing/gotchas/#mutable-default-arguments>`_ for why.
  143. .. code-block:: python
  144. from freezegun import freeze_time
  145. import datetime as dt
  146. def test(default=dt.date.today()):
  147. print(default)
  148. with freeze_time('2000-1-1'):
  149. test()
  150. Installation
  151. ------------
  152. To install FreezeGun, simply:
  153. .. code-block:: bash
  154. $ pip install freezegun
  155. On Debian systems:
  156. .. code-block:: bash
  157. $ sudo apt-get install python-freezegun