METADATA 11 KB

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