Complete the code to import the requests library for testing external services.
import [1]
The requests library is used to make HTTP calls to external services in tests.
Complete the code to mock the external HTTP GET request using pytest and requests.
def test_api_call(mocker): mock_get = mocker.patch('requests.[1]') mock_get.return_value.status_code = 200 response = requests.get('http://example.com') assert response.status_code == 200
The get method is patched to mock the HTTP GET request.
Fix the error in the test by completing the assertion to check the mocked JSON response.
def test_api_json_response(mocker): mock_get = mocker.patch('requests.get') mock_get.return_value.status_code = 200 mock_get.return_value.json = lambda: {'key': 'value'} response = requests.get('http://example.com') assert response.json() [1] {'key': 'value'}
The assertion should check equality with == to verify the JSON content.
Fill both blanks to create a pytest fixture that mocks requests.post and returns a 201 status code.
@pytest.fixture def mock_post(mocker): mock = mocker.patch('requests.[1]') mock.return_value.status_code = [2] return mock
The fixture patches requests.post and sets the status code to 201 (Created).
Fill all three blanks to write a test that uses the mock_post fixture and asserts the response status code is 201.
def test_create_resource(mock_post): response = requests.[1]('http://example.com/resource') assert response.[2] == [3]
The test calls requests.post, checks status_code, and expects 201.