Bird
0
0

You wrote this integration test code:

medium📝 Debug Q14 of 15
Rest API - API Testing and Monitoring
You wrote this integration test code:
response = requests.get('http://api.example.com/items')
assert response.status_code == 200
assert response.json()['items']
But the test fails with a KeyError on 'items'. What is the likely cause?
AThe URL is incorrect and returns 404
BThe requests library is not imported
CThe status code is not 200
DThe response JSON does not contain the key 'items'
Step-by-Step Solution
Solution:
  1. Step 1: Understand KeyError cause

    A KeyError means the key 'items' is missing in the JSON response.
  2. Step 2: Analyze test failure reason

    Since status code is asserted as 200, the URL is likely correct and requests imported. The error is missing 'items' key in JSON.
  3. Final Answer:

    The response JSON does not contain the key 'items' -> Option D
  4. Quick Check:

    KeyError means missing key in JSON [OK]
Quick Trick: KeyError means JSON key missing, not URL or import error [OK]
Common Mistakes:
MISTAKES
  • Assuming URL is wrong without checking status code
  • Ignoring that status code 200 means request succeeded
  • Forgetting to import requests library

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes