-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathdemo2.py
More file actions
29 lines (22 loc) · 671 Bytes
/
demo2.py
File metadata and controls
29 lines (22 loc) · 671 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
from requests.exceptions import HTTPError
from unittest.mock import Mock
requests = Mock()
def get_users():
r = requests.get('http://demo/api/users')
if r.status_code == 200:
return r.json()
return None
if __name__ == '__main__':
requests.get.side_effect = HTTPError
try:
get_users()
except HTTPError:
print('HTTPError')
# Assert that the mock was called exactly once.
requests.get.assert_called_once()
# try:
# get_users()
# except HTTPError:
# print('HTTPError')
# # AssertionError: Expected 'get' to have been called once. Called 2 times
# requests.get.assert_called_once()