Consider this pytest test function that uses a mock API client to test a GET request response status code.
import pytest from unittest.mock import Mock class APIClient: def get(self, url): # Simulate a real HTTP GET request return Mock(status_code=200) def test_get_status_code(): client = APIClient() response = client.get('/resource') assert response.status_code == 200 print('Test passed')
Look at the mocked response status_code and the assertion.
The mock response has status_code 200, which matches the assertion, so the test passes and prints 'Test passed'.
You receive this JSON response from an API client: {"user": "alice", "active": true}. Which pytest assertion correctly checks that the user is 'alice' and active is True?
Remember how to access dictionary keys and compare values in Python.
Option A uses correct dictionary key access and comparison operators. Option A uses assignment (=) instead of comparison (==). Option A tries attribute access on a dict, which raises an error. Option A uses unquoted strings and lowercase true, which are invalid in Python.
You want to mock the post method of an API client instance client to return a response with status code 201. Which option correctly sets this up using unittest.mock?
Think about how to assign a mock method that returns a mock response.
Option A correctly assigns a Mock to client.post that returns another Mock with status_code 201. Option A tries to set return_value on a method that may not be mocked yet. Option A uses Mock incorrectly as a function. Option A assigns a Mock with status_code attribute directly, but not as a callable method.
Examine this test code snippet:
def test_api_call(client):
response = client.get('/data')
assert response.status == 200The test fails with AttributeError: 'Mock' object has no attribute 'status'. Why?
Check the attribute names commonly used in HTTP response objects.
HTTP response objects typically have a 'status_code' attribute, not 'status'. Accessing 'status' causes AttributeError.
You want to write pytest tests for an API client where each test gets a fresh client instance with a mocked get method returning status 200. Which fixture setup is best?
Consider fixture scope and test isolation.
Option D creates a new client instance per test (default function scope), ensuring isolation. Options B, C, and D use broader scopes or autouse, which may share the client across tests, risking state leakage.