Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock time to make date-based tests reliable #394

Merged
merged 2 commits into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions development.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
coverage
cryptography
flake8
freezegun
httplib2
ipdb
mccabe
Expand Down
13 changes: 0 additions & 13 deletions setup.cfg

This file was deleted.

19 changes: 10 additions & 9 deletions tests/functional/test_httplib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import re
import httplib2
from freezegun import freeze_time
from sure import expect, within, microseconds
from httpretty import HTTPretty, httprettified
from httpretty.core import decode_utf8
Expand Down Expand Up @@ -64,8 +65,8 @@ def test_httpretty_provides_easy_access_to_querystrings(now):


@httprettified
@within(two=microseconds)
def test_httpretty_should_mock_headers_httplib2(now):
@freeze_time("2013-10-04 04:20:00")
def test_httpretty_should_mock_headers_httplib2():
"HTTPretty should mock basic headers with httplib2"

HTTPretty.register_uri(HTTPretty.GET, "http://github.com/",
Expand All @@ -80,13 +81,13 @@ def test_httpretty_should_mock_headers_httplib2(now):
'content-length': '35',
'status': '201',
'server': 'Python/HTTPretty',
'date': now.strftime('%a, %d %b %Y %H:%M:%S GMT'),
'date': 'Fri, 04 Oct 2013 04:20:00 GMT',
})


@httprettified
@within(two=microseconds)
def test_httpretty_should_allow_adding_and_overwritting_httplib2(now):
@freeze_time("2013-10-04 04:20:00")
def test_httpretty_should_allow_adding_and_overwritting_httplib2():
"HTTPretty should allow adding and overwritting headers with httplib2"

HTTPretty.register_uri(HTTPretty.GET, "http://github.com/foo",
Expand All @@ -106,7 +107,7 @@ def test_httpretty_should_allow_adding_and_overwritting_httplib2(now):
'content-length': '27',
'status': '200',
'server': 'Apache',
'date': now.strftime('%a, %d %b %Y %H:%M:%S GMT'),
'date': 'Fri, 04 Oct 2013 04:20:00 GMT',
})


Expand Down Expand Up @@ -135,8 +136,8 @@ def test_httpretty_should_allow_forcing_headers_httplib2(now):


@httprettified
@within(two=microseconds)
def test_httpretty_should_allow_adding_and_overwritting_by_kwargs_u2(now):
@freeze_time("2013-10-04 04:20:00")
def test_httpretty_should_allow_adding_and_overwritting_by_kwargs_u2():
"HTTPretty should allow adding and overwritting headers by keyword args " \
"with httplib2"

Expand All @@ -159,7 +160,7 @@ def test_httpretty_should_allow_adding_and_overwritting_by_kwargs_u2(now):
'content-length': '27',
'status': '200',
'server': 'Apache',
'date': now.strftime('%a, %d %b %Y %H:%M:%S GMT'),
'date': 'Fri, 04 Oct 2013 04:20:00 GMT',
})


Expand Down
19 changes: 10 additions & 9 deletions tests/functional/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import json
import requests
import signal
from freezegun import freeze_time
from mock import Mock
from unittest import skip
from contextlib import contextmanager
Expand Down Expand Up @@ -86,8 +87,8 @@ def test_httpretty_provides_easy_access_to_querystrings(now):


@httprettified
@within(two=microseconds)
def test_httpretty_should_mock_headers_requests(now):
@freeze_time("2013-10-04 04:20:00")
def test_httpretty_should_mock_headers_requests():
"HTTPretty should mock basic headers with requests"

HTTPretty.register_uri(HTTPretty.GET, "http://github.com/",
Expand All @@ -103,13 +104,13 @@ def test_httpretty_should_mock_headers_requests(now):
'content-length': '35',
'status': '201',
'server': 'Python/HTTPretty',
'date': now.strftime('%a, %d %b %Y %H:%M:%S GMT'),
'date': 'Fri, 04 Oct 2013 04:20:00 GMT',
})


@httprettified
@within(two=microseconds)
def test_httpretty_should_allow_adding_and_overwritting_requests(now):
@freeze_time("2013-10-04 04:20:00")
def test_httpretty_should_allow_adding_and_overwritting_requests():
"HTTPretty should allow adding and overwritting headers with requests"

HTTPretty.register_uri(HTTPretty.GET, "http://github.com/foo",
Expand All @@ -128,7 +129,7 @@ def test_httpretty_should_allow_adding_and_overwritting_requests(now):
'content-length': '27',
'status': '200',
'server': 'Apache',
'date': now.strftime('%a, %d %b %Y %H:%M:%S GMT'),
'date': 'Fri, 04 Oct 2013 04:20:00 GMT',
})


Expand All @@ -153,8 +154,8 @@ def test_httpretty_should_allow_forcing_headers_requests(now):


@httprettified
@within(two=microseconds)
def test_httpretty_should_allow_adding_and_overwritting_by_kwargs_u2(now):
@freeze_time("2013-10-04 04:20:00")
def test_httpretty_should_allow_adding_and_overwritting_by_kwargs_u2():
"HTTPretty should allow adding and overwritting headers by keyword args " \
"with requests"

Expand All @@ -172,7 +173,7 @@ def test_httpretty_should_allow_adding_and_overwritting_by_kwargs_u2(now):
'content-length': '27',
'status': '200',
'server': 'Apache',
'date': now.strftime('%a, %d %b %Y %H:%M:%S GMT'),
'date': 'Fri, 04 Oct 2013 04:20:00 GMT',
})


Expand Down
19 changes: 10 additions & 9 deletions tests/functional/test_urllib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import urllib2
urlopen = urllib2.urlopen

from freezegun import freeze_time
from sure import within, microseconds
from httpretty import HTTPretty, httprettified
from httpretty.core import decode_utf8
Expand Down Expand Up @@ -73,8 +74,8 @@ def test_httpretty_provides_easy_access_to_querystrings(now):


@httprettified
@within(two=microseconds)
def test_httpretty_should_mock_headers_urllib2(now):
@freeze_time("2013-10-04 04:20:00")
def test_httpretty_should_mock_headers_urllib2():
"HTTPretty should mock basic headers with urllib2"

HTTPretty.register_uri(HTTPretty.GET, "http://github.com/",
Expand All @@ -93,13 +94,13 @@ def test_httpretty_should_mock_headers_urllib2(now):
'content-length': '35',
'status': '201',
'server': 'Python/HTTPretty',
'date': now.strftime('%a, %d %b %Y %H:%M:%S GMT'),
'date': 'Fri, 04 Oct 2013 04:20:00 GMT',
})


@httprettified
@within(two=microseconds)
def test_httpretty_should_allow_adding_and_overwritting_urllib2(now):
@freeze_time("2013-10-04 04:20:00")
def test_httpretty_should_allow_adding_and_overwritting_urllib2():
"HTTPretty should allow adding and overwritting headers with urllib2"

HTTPretty.register_uri(HTTPretty.GET, "http://github.com/",
Expand All @@ -121,7 +122,7 @@ def test_httpretty_should_allow_adding_and_overwritting_urllib2(now):
'content-length': '27',
'status': '200',
'server': 'Apache',
'date': now.strftime('%a, %d %b %Y %H:%M:%S GMT'),
'date': 'Fri, 04 Oct 2013 04:20:00 GMT',
})


Expand All @@ -148,8 +149,8 @@ def test_httpretty_should_allow_forcing_headers_urllib2():


@httprettified
@within(two=microseconds)
def test_httpretty_should_allow_adding_and_overwritting_by_kwargs_u2(now):
@freeze_time("2013-10-04 04:20:00")
def test_httpretty_should_allow_adding_and_overwritting_by_kwargs_u2():
("HTTPretty should allow adding and overwritting headers by "
"keyword args with urllib2")

Expand All @@ -171,7 +172,7 @@ def test_httpretty_should_allow_adding_and_overwritting_by_kwargs_u2(now):
'content-length': str(len(body)),
'status': '200',
'server': 'Apache',
'date': now.strftime('%a, %d %b %Y %H:%M:%S GMT'),
'date': 'Fri, 04 Oct 2013 04:20:00 GMT',
})


Expand Down
9 changes: 3 additions & 6 deletions tests/unit/test_core.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import io
import json
import errno
from datetime import datetime

from freezegun import freeze_time
from mock import Mock, patch, call
from sure import expect

Expand Down Expand Up @@ -186,12 +186,9 @@ def test_fake_ssl_socket_proxies_its_ow_socket():
socket.send.assert_called_once_with("FOO")


@patch('httpretty.core.datetime')
def test_fakesock_socket_getpeercert(dt):
@freeze_time("2013-10-04 04:20:00")
def test_fakesock_socket_getpeercert():
("fakesock.socket#getpeercert should return a hardcoded fake certificate")
# Background:
dt.now.return_value = datetime(2013, 10, 4, 4, 20, 0)

# Given a fake socket instance
socket = fakesock.socket()

Expand Down