def test_request_response_1(): # Call the service, which will send a request to the server. response = get_todos() # If the request is sent successfully, then I expect a response to be returned. assert_is_not_none(response)
def test_getting_todos_decorator(mock_get): # Configure the mock to return a response with an OK status code. mock_get.return_value.ok = True # Call the service, which will send a request to the server. response = get_todos() # If the request is sent successfully, then I expect a response to be returned. assert_is_not_none(response)
def test_getting_todos_when_response_is_not_ok(self): # Configure the mock to not return a response with an OK status code. self.mock_get.return_value.ok = False # Call the service, which will send a request to the server. response = get_todos() # If the response contains an error, I should get no todos. assert_is_none(response)
def test_getting_todos(self, mock_get): # Configure the mock to return a response with an OK status code mock_get.return_value.ok = True # Call the service, which will send a request to the server response = get_todos() # If a request is sent successfully, then I expect a response to be returned self.assertIsNotNone(response)
def test_getting_todos_with_context(): with patch('services.requests.get') as mock_get: # Configure the mock to return a response with an OK status code. mock_get.return_value.ok = True # Call the service, which will send a request to the server. response = get_todos() # If the request is sent successfully, then I expect a response to be returned. assert_is_not_none(response)
def test_integration_contract(self): # Call the service to hit the actual API actual = get_todos() actual_keys = actual.json().pop().keys() # Call the service to hit the mocked API with patch('services.requests.get') as mock_get: mock_get.return_value.ok = True mock_get.return_value.json.return_value = [{ 'userId': 1, 'id': 1, 'title': 'Make the bed', 'completed': False }] mocked = get_todos() mocked_keys = mocked.json().pop().keys() # An object from the actual API and an object from the mocked API should have the same data structure self.assertListEqual(list(actual_keys), list(mocked_keys))
def test_getting_todos_2(): mock_get_patcher = patch('services.requests.get') # Start patching `requests.get`. mock_get = mock_get_patcher.start() # Configure the mock to return a response with an OK status code. mock_get.return_value.ok = True # Call the service, which will send a request to the server. response = get_todos() # Stop patching `requests.get`. mock_get_patcher.stop() # If the request is sent successfully, then I expect a response to be returned. assert_is_not_none(response)
def test_getting_todos_when_response_is_ok(mock_get): todos = [{ 'userId': 1, 'id': 1, 'title': 'Make the bed', 'completed': False }] # Configure the mock to return a response with an OK status code. Also, the mock should have # a `json()` method that returns a list of todos. mock_get.return_value = Mock(ok=True) mock_get.return_value.json.return_value = todos # Call the service, which will send a request to the server. response = get_todos() # If the request is sent successfully, then I expect a response to be returned. assert_list_equal(response.json(), todos)
def test_getting_todos_when_response_is_ok(self): # Configure the mock to return a response with an OK status code. self.mock_get.return_value.ok = True todos = [{ 'userId': 1, 'id': 1, 'title': 'Make the bed', 'completed': False }] self.mock_get.return_value = Mock() self.mock_get.return_value.json.return_value = todos # Call the service, which will send a request to the server. response = get_todos() # If the request is sent successfully, then I expect a response to be returned. assert_list_equal(response.json(), todos)
def test_request_response(mock_get): mock_get.return_value.ok = True response = get_todos() assert_is_not_none(response)
def test_request_response(self): # Send a request to the API server and store the response response = get_todos() # Confirm that the request-response cycle completed successfully self.assertIsNotNone(response)