-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmocking_requests.py
More file actions
35 lines (25 loc) · 852 Bytes
/
mocking_requests.py
File metadata and controls
35 lines (25 loc) · 852 Bytes
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
from unittest.mock import Mock
import requests
requests = Mock()
def get_holidays():
r = requests.get("http://localhost/api/holidays")
if r.status_code == 200:
return r.json()
return None
class TestCalendar:
def log_request(self, url):
# Log a fake request for test output purposes
print(f"Making a request to {url}.")
print("Request received!")
# Create a new Mock to imitate a Response
response_mock = Mock()
response_mock.status_code = 200
response_mock.json.return_value = {
"12/25": "Christmas",
"7/4": "Independence Day",
}
return response_mock
def test_get_holidays_logging(self):
requests.get.side_effect = self.log_request
print(requests)
assert get_holidays()["12/25"] == "Christmas"