Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 100 lines (64 sloc) 2.712 kb
ba9f566 @spulec first commit
authored
1 FreezeGun: Let your Python tests travel through time
f990ef4 @EvaSDK Fix under/over underlining of titles
EvaSDK authored
2 ====================================================
ba9f566 @spulec first commit
authored
3
4
5 .. image:: https://secure.travis-ci.org/spulec/freezegun.png?branch=master
7a229c3 @spulec Fix readme links
authored
6 :target: https://travis-ci.org/spulec/freezegun
376a7d8 @spulec add coverage badge
authored
7 .. image:: https://coveralls.io/repos/spulec/freezegun/badge.png?branch=master
9930192 @spulec Fix link again
authored
8 :target: https://coveralls.io/r/spulec/freezegun
ba9f566 @spulec first commit
authored
9
497f5c9 @spulec organize readme better
authored
10 FreezeGun is a library that allows your python tests to travel through time by mocking the datetime module.
ba9f566 @spulec first commit
authored
11
12 Usage
f990ef4 @EvaSDK Fix under/over underlining of titles
EvaSDK authored
13 -----
ba9f566 @spulec first commit
authored
14
2d7ee98 @spulec Update README for additional mocks.
authored
15 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.
ba9f566 @spulec first commit
authored
16
17 Decorator
18 ~~~~~~~~~
19
20 .. code-block:: python
a5d6d70 @spulec fix readme
authored
21
ba9f566 @spulec first commit
authored
22 from freezegun import freeze_time
23
24 @freeze_time("2012-01-14")
25 def test():
26 assert datetime.datetime.now() == datetime.datetime(2012, 01, 14)
27
04152d4 @spulec document class-based decorator
authored
28 # Or class based
29
30 @freeze_time("2012-01-14")
31 class Tester(object):
32 def test_the_class(self):
33 assert datetime.datetime.now() == datetime.datetime(2012, 01, 14)
ba9f566 @spulec first commit
authored
34
35 Context Manager
36 ~~~~~~~~~~~~~~~
37
38 .. code-block:: python
a5d6d70 @spulec fix readme
authored
39
ba9f566 @spulec first commit
authored
40 from freezegun import freeze_time
41
42 def test():
43 assert datetime.datetime.now() != datetime.datetime(2012, 01, 14)
44 with freeze_time("2012-01-14"):
45 assert datetime.datetime.now() == datetime.datetime(2012, 01, 14)
46 assert datetime.datetime.now() != datetime.datetime(2012, 01, 14)
6adca04 @spulec add tz support
authored
47
c626d68 @spulec rearrange docs
authored
48 Raw use
49 ~~~~~~~
50
51 .. code-block:: python
52
53 from freezegun import freeze_time
54
55 freezer = freeze_time("2012-01-14 12:00:01")
56 freezer.start()
57 assert datetime.datetime.now() == datetime.datetime(2012, 01, 14, 12, 00, 01)
58 freezer.stop()
59
6adca04 @spulec add tz support
authored
60 Timezones
61 ~~~~~~~~~
62
63 .. code-block:: python
64
65 from freezegun import freeze_time
66
13bcba7 @spulec better timezone doc example
authored
67 @freeze_time("2012-01-14 03:21:34", tz_offset=-4)
6adca04 @spulec add tz support
authored
68 def test():
13bcba7 @spulec better timezone doc example
authored
69 assert datetime.datetime.utcnow() == datetime.datetime(2012, 01, 14, 03, 21, 34)
70 assert datetime.datetime.now() == datetime.datetime(2012, 01, 13, 23, 21, 34)
087486a @spulec better timezone docs
authored
71
6adca04 @spulec add tz support
authored
72 # datetime.date.today() uses local time
73 assert datetime.date.today() == datetime.datetime(2012, 01, 13)
74
28e7b8e @spulec add docs about nice-looking datetimes
authored
75 Nice inputs
76 ~~~~~~~~~~~
77
78 FreezeGun uses dateutil behind the scenes so you can have nice-looking datetimes
79
80 .. code-block:: python
81
82 @freeze_time("Jan 14th, 2012")
83 def test_nice_datetime():
84 assert datetime.datetime.now() == datetime.datetime(2012, 01, 14)
497f5c9 @spulec organize readme better
authored
85
86 Installation
87 ------------
88
89 To install FreezeGun, simply:
90
91 .. code-block:: bash
92
93 $ pip install freezegun
94
dd2b0cc @c4urself Update README.rst
c4urself authored
95 On Debian (Testing and Unstable) systems:
4e23195 @FedericoCeratto Add Debian install instructions
FedericoCeratto authored
96
97 .. code-block:: bash
98
99 $ sudo apt-get install python-freezegun
Something went wrong with that request. Please try again.