-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_users.py
More file actions
103 lines (82 loc) · 3.61 KB
/
test_users.py
File metadata and controls
103 lines (82 loc) · 3.61 KB
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
import unittest
from users import get_users, get_user
from unittest.mock import patch, Mock
class BasicTests(unittest.TestCase):
@patch('users.requests.get') # Mock 'requests' module 'get' method.
def test_request_response_with_decorator(self, mock_get):
"""Mocking using a decorator"""
mock_get.return_value.status_code = 200 # Mock status code of response.
response = get_users()
# Assert that the request-response cycle completed successfully.
self.assertEqual(response.status_code, 200)
def test_request_response_with_context_manager(self):
"""Mocking using a context manager"""
with patch('users.requests.get') as mock_get:
# Configure the mock to return a response with status code 200.
mock_get.return_value.status_code = 200
# Call the function, which will send a request to the server.
response = get_users()
# Assert that the request-response cycle completed successfully.
self.assertEqual(response.status_code, 200)
def test_request_response_with_patcher(self):
"""Mocking using a patcher"""
mock_get_patcher = patch('users.requests.get')
users = [{
"id": 0,
"first_name": "Dell",
"last_name": "Norval",
"phone": "994-979-3976"
}]
# Start patching 'requests.get'.
mock_get = mock_get_patcher.start()
# Configure the mock to return a response with status code 200.
mock_get.return_value = Mock(status_code=200)
mock_get.return_value.json.return_value = users
# Call the service, which will send a request to the server.
response = get_users()
# Stop patching 'requests'.
mock_get_patcher.stop()
# Assert that the request-response cycle completed successfully.
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), users)
def test_mock_whole_function(self):
"""Mocking a whole function"""
mock_get_patcher = patch('users.requests.get')
users = [{
"id": 0,
"first_name": "Dell",
"last_name": "Norval",
"phone": "994-979-3976"
}]
# Start patching 'requests.get'.
mock_get = mock_get_patcher.start()
# Configure the mock to return a response with status code 200 and a list of users.
mock_get.return_value = Mock(status_code = 200)
mock_get.return_value.json.return_value = users
# Call the service, which will send a request to the server.
response = get_users()
# Stop patching 'requests'.
mock_get_patcher.stop()
# Assert that the request-response cycle completed successfully.
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), users)
@patch('users.get_users')
def test_get_one_user(self, mock_get_users):
"""
Test for getting one user using their userID
Demonstrates mocking third party functions
"""
users = [
{'phone': '514-794-6957', 'first_name': 'Brant',
'last_name': 'Mekhi', 'id': 0},
{'phone': '772-370-0117', 'first_name': 'Thalia',
'last_name': 'Kenyatta', 'id': 1},
{'phone': '176-290-7637', 'first_name': 'Destin',
'last_name': 'Soledad', 'id': 2}
]
mock_get_users.return_value = Mock()
mock_get_users.return_value.json.return_value = users
user = get_user(2)
self.assertEqual(user, users[2])
if __name__ == "__main__":
unittest.main()