Consider this Python code snippet that validates an API JSON response status and content:
response = {'status_code': 200, 'data': {'user': 'alice', 'id': 123}}
assert response['status_code'] == 200
assert 'user' in response['data']
print('Validation passed')What will be printed when this code runs?
response = {'status_code': 200, 'data': {'user': 'alice', 'id': 123}}
assert response['status_code'] == 200
assert 'user' in response['data']
print('Validation passed')Check if the status code is 200 and if the key 'user' exists in the nested data dictionary.
The response has status_code 200 and the 'user' key is present in the 'data' dictionary, so both assertions pass and 'Validation passed' is printed.
You receive this JSON response from an API:
{'status': 'success', 'items': [1, 2, 3]}Which assertion statement correctly verifies that 'items' exists and is not empty?
Check both the presence of the key and that the list has elements.
Option B explicitly checks the key exists and the list length is greater than zero, ensuring 'items' is present and non-empty.
Option B only checks not None but key might be missing.
Option B compares list identity which is unreliable.
Option B passes if list is truthy but fails if key missing.
Given this test code snippet:
request = {'method': 'POST', 'body': {'username': 'bob'}}
assert request['body']['password'] == 'secret'Why does this test raise a KeyError?
request = {'method': 'POST', 'body': {'username': 'bob'}}
assert request['body']['password'] == 'secret'Check if the key you access exists in the dictionary.
The 'body' dictionary has only 'username' key. Accessing 'password' key raises KeyError because it is missing.
Why do testers perform response schema validation when testing APIs?
Think about what schema means in data context.
Response schema validation checks that the response JSON has the correct keys, nested structure, and data types as expected.
In automated API testing, which feature of a test framework helps validate both request payloads and response data effectively?
Consider how to run the same test with many input and output variations automatically.
Data-driven testing allows running the same test logic with multiple sets of request and expected response data, automating validation efficiently.