METADATA 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. Metadata-Version: 2.1
  2. Name: freezegun
  3. Version: 1.2.2
  4. Summary: Let your Python tests travel through time
  5. Home-page: https://github.com/spulec/freezegun
  6. Author: Steve Pulec
  7. Author-email: spulec@gmail.com
  8. License: Apache 2.0
  9. Project-URL: Bug Tracker, https://github.com/spulec/freezegun/issues
  10. Project-URL: Changes, https://github.com/spulec/freezegun/blob/master/CHANGELOG
  11. Project-URL: Documentation, https://github.com/spulec/freezegun/blob/master/README.rst
  12. Project-URL: Source Code, https://github.com/spulec/freezegun
  13. Classifier: License :: OSI Approved :: Apache Software License
  14. Classifier: Programming Language :: Python :: 3
  15. Classifier: Programming Language :: Python :: 3.7
  16. Classifier: Programming Language :: Python :: 3.8
  17. Classifier: Programming Language :: Python :: 3.9
  18. Classifier: Programming Language :: Python :: Implementation :: CPython
  19. Classifier: Programming Language :: Python :: Implementation :: PyPy
  20. Requires-Python: >=3.6
  21. License-File: LICENSE
  22. License-File: AUTHORS.rst
  23. Requires-Dist: python-dateutil (>=2.7)
  24. FreezeGun: Let your Python tests travel through time
  25. ====================================================
  26. .. image:: https://img.shields.io/pypi/v/freezegun.svg
  27. :target: https://pypi.python.org/pypi/freezegun/
  28. .. image:: https://github.com/spulec/freezegun/workflows/CI/badge.svg
  29. :target: https://github.com/spulec/freezegun/actions
  30. .. image:: https://coveralls.io/repos/spulec/freezegun/badge.svg?branch=master
  31. :target: https://coveralls.io/r/spulec/freezegun
  32. FreezeGun is a library that allows your Python tests to travel through time by mocking the datetime module.
  33. Usage
  34. -----
  35. 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.
  36. Decorator
  37. ~~~~~~~~~
  38. .. code-block:: python
  39. from freezegun import freeze_time
  40. import datetime
  41. import unittest
  42. # Freeze time for a pytest style test:
  43. @freeze_time("2012-01-14")
  44. def test():
  45. assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
  46. # Or a unittest TestCase - freezes for every test, and set up and tear down code
  47. @freeze_time("1955-11-12")
  48. class MyTests(unittest.TestCase):
  49. def test_the_class(self):
  50. assert datetime.datetime.now() == datetime.datetime(1955, 11, 12)
  51. # Or any other class - freezes around each callable (may not work in every case)
  52. @freeze_time("2012-01-14")
  53. class Tester(object):
  54. def test_the_class(self):
  55. assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
  56. # Or method decorator, might also pass frozen time object as kwarg
  57. class TestUnitTestMethodDecorator(unittest.TestCase):
  58. @freeze_time('2013-04-09')
  59. def test_method_decorator_works_on_unittest(self):
  60. self.assertEqual(datetime.date(2013, 4, 9), datetime.date.today())
  61. @freeze_time('2013-04-09', as_kwarg='frozen_time')
  62. def test_method_decorator_works_on_unittest(self, frozen_time):
  63. self.assertEqual(datetime.date(2013, 4, 9), datetime.date.today())
  64. self.assertEqual(datetime.date(2013, 4, 9), frozen_time.time_to_freeze.today())
  65. @freeze_time('2013-04-09', as_kwarg='hello')
  66. def test_method_decorator_works_on_unittest(self, **kwargs):
  67. self.assertEqual(datetime.date(2013, 4, 9), datetime.date.today())
  68. self.assertEqual(datetime.date(2013, 4, 9), kwargs.get('hello').time_to_freeze.today())
  69. Context manager
  70. ~~~~~~~~~~~~~~~
  71. .. code-block:: python
  72. from freezegun import freeze_time
  73. def test():
  74. assert datetime.datetime.now() != datetime.datetime(2012, 1, 14)
  75. with freeze_time("2012-01-14"):
  76. assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
  77. assert datetime.datetime.now() != datetime.datetime(2012, 1, 14)
  78. Raw use
  79. ~~~~~~~
  80. .. code-block:: python
  81. from freezegun import freeze_time
  82. freezer = freeze_time("2012-01-14 12:00:01")
  83. freezer.start()
  84. assert datetime.datetime.now() == datetime.datetime(2012, 1, 14, 12, 0, 1)
  85. freezer.stop()
  86. Timezones
  87. ~~~~~~~~~
  88. .. code-block:: python
  89. from freezegun import freeze_time
  90. @freeze_time("2012-01-14 03:21:34", tz_offset=-4)
  91. def test():
  92. assert datetime.datetime.utcnow() == datetime.datetime(2012, 1, 14, 3, 21, 34)
  93. assert datetime.datetime.now() == datetime.datetime(2012, 1, 13, 23, 21, 34)
  94. # datetime.date.today() uses local time
  95. assert datetime.date.today() == datetime.date(2012, 1, 13)
  96. @freeze_time("2012-01-14 03:21:34", tz_offset=-datetime.timedelta(hours=3, minutes=30))
  97. def test_timedelta_offset():
  98. assert datetime.datetime.now() == datetime.datetime(2012, 1, 13, 23, 51, 34)
  99. Nice inputs
  100. ~~~~~~~~~~~
  101. FreezeGun uses dateutil behind the scenes so you can have nice-looking datetimes.
  102. .. code-block:: python
  103. @freeze_time("Jan 14th, 2012")
  104. def test_nice_datetime():
  105. assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
  106. Function and generator objects
  107. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  108. FreezeGun is able to handle function and generator objects.
  109. .. code-block:: python
  110. def test_lambda():
  111. with freeze_time(lambda: datetime.datetime(2012, 1, 14)):
  112. assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
  113. def test_generator():
  114. datetimes = (datetime.datetime(year, 1, 1) for year in range(2010, 2012))
  115. with freeze_time(datetimes):
  116. assert datetime.datetime.now() == datetime.datetime(2010, 1, 1)
  117. with freeze_time(datetimes):
  118. assert datetime.datetime.now() == datetime.datetime(2011, 1, 1)
  119. # The next call to freeze_time(datetimes) would raise a StopIteration exception.
  120. ``tick`` argument
  121. ~~~~~~~~~~~~~~~~~
  122. FreezeGun has an additional ``tick`` argument which will restart time at the given
  123. value, but then time will keep ticking. This is an alternative to the default
  124. parameters which will keep time stopped.
  125. .. code-block:: python
  126. @freeze_time("Jan 14th, 2020", tick=True)
  127. def test_nice_datetime():
  128. assert datetime.datetime.now() > datetime.datetime(2020, 1, 14)
  129. ``auto_tick_seconds`` argument
  130. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  131. FreezeGun has an additional ``auto_tick_seconds`` argument which will autoincrement the
  132. value every time by the given amount from the start value. This is an alternative to the default
  133. parameters which will keep time stopped. Note that given ``auto_tick_seconds`` the ``tick`` parameter will be ignored.
  134. .. code-block:: python
  135. @freeze_time("Jan 14th, 2020", auto_tick_seconds=15)
  136. def test_nice_datetime():
  137. first_time = datetime.datetime.now()
  138. auto_incremented_time = datetime.datetime.now()
  139. assert first_time + datetime.timedelta(seconds=15) == auto_incremented_time
  140. Manual ticks
  141. ~~~~~~~~~~~~
  142. FreezeGun allows for the time to be manually forwarded as well.
  143. .. code-block:: python
  144. def test_manual_tick():
  145. initial_datetime = datetime.datetime(year=1, month=7, day=12,
  146. hour=15, minute=6, second=3)
  147. with freeze_time(initial_datetime) as frozen_datetime:
  148. assert frozen_datetime() == initial_datetime
  149. frozen_datetime.tick()
  150. initial_datetime += datetime.timedelta(seconds=1)
  151. assert frozen_datetime() == initial_datetime
  152. frozen_datetime.tick(delta=datetime.timedelta(seconds=10))
  153. initial_datetime += datetime.timedelta(seconds=10)
  154. assert frozen_datetime() == initial_datetime
  155. .. code-block:: python
  156. def test_monotonic_manual_tick():
  157. initial_datetime = datetime.datetime(year=1, month=7, day=12,
  158. hour=15, minute=6, second=3)
  159. with freeze_time(initial_datetime) as frozen_datetime:
  160. monotonic_t0 = time.monotonic()
  161. frozen_datetime.tick(1.0)
  162. monotonic_t1 = time.monotonic()
  163. assert monotonic_t1 == monotonic_t0 + 1.0
  164. Moving time to specify datetime
  165. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  166. FreezeGun allows moving time to specific dates.
  167. .. code-block:: python
  168. def test_move_to():
  169. initial_datetime = datetime.datetime(year=1, month=7, day=12,
  170. hour=15, minute=6, second=3)
  171. other_datetime = datetime.datetime(year=2, month=8, day=13,
  172. hour=14, minute=5, second=0)
  173. with freeze_time(initial_datetime) as frozen_datetime:
  174. assert frozen_datetime() == initial_datetime
  175. frozen_datetime.move_to(other_datetime)
  176. assert frozen_datetime() == other_datetime
  177. frozen_datetime.move_to(initial_datetime)
  178. assert frozen_datetime() == initial_datetime
  179. @freeze_time("2012-01-14", as_arg=True)
  180. def test(frozen_time):
  181. assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
  182. frozen_time.move_to("2014-02-12")
  183. assert datetime.datetime.now() == datetime.datetime(2014, 2, 12)
  184. Parameter for ``move_to`` can be any valid ``freeze_time`` date (string, date, datetime).
  185. Default arguments
  186. ~~~~~~~~~~~~~~~~~
  187. Note that FreezeGun will not modify default arguments. The following code will
  188. print the current date. See `here <http://docs.python-guide.org/en/latest/writing/gotchas/#mutable-default-arguments>`_ for why.
  189. .. code-block:: python
  190. from freezegun import freeze_time
  191. import datetime as dt
  192. def test(default=dt.date.today()):
  193. print(default)
  194. with freeze_time('2000-1-1'):
  195. test()
  196. Installation
  197. ------------
  198. To install FreezeGun, simply:
  199. .. code-block:: bash
  200. $ pip install freezegun
  201. On Debian systems:
  202. .. code-block:: bash
  203. $ sudo apt-get install python-freezegun
  204. Ignore packages
  205. ---------------
  206. Sometimes it's desired to ignore FreezeGun behaviour for particular packages (i.e. libraries).
  207. It's possible to ignore them for a single invocation:
  208. .. code-block:: python
  209. from freezegun import freeze_time
  210. with freeze_time('2020-10-06', ignore=['threading']):
  211. # ...
  212. By default FreezeGun ignores following packages:
  213. .. code-block:: python
  214. [
  215. 'nose.plugins',
  216. 'six.moves',
  217. 'django.utils.six.moves',
  218. 'google.gax',
  219. 'threading',
  220. 'Queue',
  221. 'selenium',
  222. '_pytest.terminal.',
  223. '_pytest.runner.',
  224. 'gi',
  225. ]
  226. It's possible to set your own default ignore list:
  227. .. code-block:: python
  228. import freezegun
  229. freezegun.configure(default_ignore_list=['threading', 'tensorflow'])
  230. Please note this will override default ignore list. If you want to extend existing defaults
  231. please use:
  232. .. code-block:: python
  233. import freezegun
  234. freezegun.configure(extend_ignore_list=['tensorflow'])