File: test_freezegun.py

package info (click to toggle)
python-pytest-freezegun 0.4.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 128 kB
  • sloc: python: 223; makefile: 5
file content (277 lines) | stat: -rw-r--r-- 7,458 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# -*- coding: utf-8 -*-
import re

from datetime import datetime


def test_freezing_time(testdir):
    testdir.makepyfile("""
        import pytest
        from datetime import date, datetime

        @pytest.mark.freeze_time('2017-05-20 15:42')
        def test_sth():
            assert datetime.now().date() == date(2017, 5, 20)
    """)

    result = testdir.runpytest('-v', '-s')
    assert result.ret == 0


def test_freezing_time_in_fixture(testdir):
    testdir.makepyfile("""
        import pytest
        from datetime import date, datetime

        @pytest.fixture
        def today():
            return datetime.now().date()

        @pytest.mark.freeze_time('2017-05-20 15:42')
        def test_sth(today):
            assert today == date(2017, 5, 20)
    """)

    result = testdir.runpytest('-v', '-s')
    assert result.ret == 0


def test_freezing_time_with_mark_and_fixture_used_by_test(testdir):
    # https://github.com/ktosiek/pytest-freezegun/issues/24
    testdir.makepyfile("""
        import pytest
        from datetime import date, datetime

        @pytest.fixture
        def today():
            return datetime.now().date()

        @pytest.mark.freeze_time('2017-05-20 15:42')
        def test_sth(today, freezer):
            assert today == date(2017, 5, 20)
    """)

    result = testdir.runpytest('-v', '-s')
    assert result.ret == 0


def test_no_mark(testdir):
    testdir.makepyfile("""
        import datetime

        def test_sth():
            assert datetime.datetime.now() > {}
    """.format(repr(datetime.now())))

    result = testdir.runpytest('-v', '-s')
    assert result.ret == 0


def test_move_to(testdir):
    testdir.makepyfile("""
        from datetime import date
        import pytest

        @pytest.mark.freeze_time
        def test_changing_date(freezer):
            freezer.move_to('2017-05-20')
            assert date.today() == date(2017, 5, 20)
            freezer.move_to('2017-05-21')
            assert date.today() == date(2017, 5, 21)
    """)

    result = testdir.runpytest('-v', '-s')
    assert result.ret == 0


def test_fixture_durations(testdir):
    """
    In an older version, the time would be frozen for the pytest itself too.
    That caused it to report weird durations for test runs,
    namely very large numbers for setup and very large
    NEGATIVE numbers for teardown.
    """
    testdir.makepyfile("""
        import pytest
        from datetime import date, datetime

        def test_truth(freezer):
            freezer.move_to('2000-01-01')
            assert True
    """)

    result = testdir.runpytest('-vv', '-s', '--durations=3')

    # We don't have access to the actual terminalreporter,
    # so the only way to collect duration times is
    # to parse the pytest output.
    DURATION_REGEX = re.compile(r'''
        (-?\d+\.\d+)s          # Time in seconds
        \s+                    # Whitespace
        (call|setup|teardown)  # Test phase
        \s+                    # Whitespace
        test_fixture_durations.py::test_truth  # Test ID
    ''', re.X)

    durations = {}
    for line in result.outlines:
        match = DURATION_REGEX.match(line)
        if match is None:
            continue

        durations[match.group(2)] = float(match.group(1))

    # It should take a non-negative amount of time for each of the steps,
    # but it also should never take longer than a second
    assert 0 <= durations['setup'] <= 1
    assert 0 <= durations['call'] <= 1
    assert 0 <= durations['teardown'] <= 1


def test_marker_durations(testdir):
    """
    In an older version, the time would be frozen for the pytest itself too.
    That caused it to report weird durations for test runs,
    namely very large numbers for setup and very large
    NEGATIVE numbers for teardown.
    """
    testdir.makepyfile("""
        import pytest
        from datetime import date, datetime

        @pytest.mark.freeze_time('2000-01-01')
        def test_truth():
            assert True
    """)

    result = testdir.runpytest('-vv', '-s', '--durations=3')

    # We don't have access to the actual terminalreporter,
    # so the only way to collect duration times is
    # to parse the pytest output.
    DURATION_REGEX = re.compile(r'''
        (-?\d+\.\d+)s          # Time in seconds
        \s+                    # Whitespace
        (call|setup|teardown)  # Test phase
        \s+                    # Whitespace
        test_marker_durations.py::test_truth  # Test ID
    ''', re.X)

    durations = {}
    for line in result.outlines:
        match = DURATION_REGEX.match(line)
        if match is None:
            continue

        durations[match.group(2)] = float(match.group(1))

    # It should take a non-negative amount of time for each of the steps,
    # but it also should never take longer than a second
    assert 0 <= durations['setup'] <= 1
    assert 0 <= durations['call'] <= 1
    assert 0 <= durations['teardown'] <= 1


def test_fixture_no_mark(testdir):
    testdir.makepyfile("""
        from datetime import datetime
        import time

        def test_just_fixture(freezer):
            now = datetime.now()
            time.sleep(0.1)
            later = datetime.now()

            assert now == later
    """)

    result = testdir.runpytest('-v', '-s')
    assert result.ret == 0


def test_fixture_freezes_time(testdir):
    testdir.makepyfile("""
        import time

        def test_fixture_freezes_time(freezer):
            now = time.time()
            time.sleep(0.1)
            later = time.time()

            assert now == later
    """)

    result = testdir.runpytest('-v', '-s')
    assert result.ret == 0


def test_marker_freezes_time(testdir):
    testdir.makepyfile("""
        import pytest
        import time

        @pytest.mark.freeze_time('2000-01-01')
        def test_marker_freezes_time(freezer):
            now = time.time()
            time.sleep(0.1)
            later = time.time()

            assert now == later
    """)

    result = testdir.runpytest('-v', '-s')
    assert result.ret == 0


def test_class_freezing_time(testdir):
    testdir.makepyfile("""
        import pytest
        from datetime import date, datetime

        class TestAsClass(object):

            @pytest.mark.freeze_time('2017-05-20 15:42')
            def test_sth(self):
                assert datetime.now().date() == date(2017, 5, 20)
    """)

    result = testdir.runpytest('-v', '-s')
    assert result.ret == 0


def test_class_move_to(testdir):
    testdir.makepyfile("""
        from datetime import date
        import pytest

        class TestAsClass(object):

            @pytest.mark.freeze_time
            def test_changing_date(self, freezer):
                freezer.move_to('2017-05-20')
                assert date.today() == date(2017, 5, 20)
                freezer.move_to('2017-05-21')
                assert date.today() == date(2017, 5, 21)
    """)

    result = testdir.runpytest('-v', '-s')
    assert result.ret == 0


def test_class_just_fixture(testdir):
    testdir.makepyfile("""
        from datetime import datetime
        import time

        class TestAsClass(object):

            def test_just_fixture(self, freezer):
                now = datetime.now()
                time.sleep(0.1)
                later = datetime.now()

                assert now == later
    """)

    result = testdir.runpytest('-v', '-s')
    assert result.ret == 0