METADATA 8.3 KB

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