Bird
Raised Fist0
Rest APIprogramming~20 mins

Integration testing in Rest API - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Integration Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this integration test response check?

Consider a REST API integration test that sends a GET request to /users/123. The test expects a JSON response with the user's name and status code 200.

What will be the printed output of this test snippet?

Rest API
response = client.get('/users/123')
if response.status_code == 200 and response.json().get('name') == 'Alice':
    print('Test Passed')
else:
    print('Test Failed')
ATest Failed
BTest Passed
CKeyError
DAttributeError
Attempts:
2 left
💡 Hint

Check if the response status and JSON content match the expected values.

🧠 Conceptual
intermediate
1:30remaining
Which component is typically NOT tested in integration testing of REST APIs?

Integration testing usually checks how different parts of a system work together. Which of these is least likely to be tested during REST API integration testing?

AIndividual function logic inside a utility module
BAPI endpoint response format correctness
CDatabase interaction with API endpoints
DAuthentication flow between client and server
Attempts:
2 left
💡 Hint

Think about what integration testing focuses on versus unit testing.

🔧 Debug
advanced
2:00remaining
Why does this integration test fail with a timeout error?

Given this integration test code snippet that calls an external API endpoint, why does it fail with a timeout error?

Rest API
response = client.get('/external-data', timeout=0.001)
print(response.status_code)
AThe timeout value is too low causing the request to abort before response
BThe endpoint URL is incorrect causing no response
CThe client.get method does not support timeout parameter
DThe response object does not have a status_code attribute
Attempts:
2 left
💡 Hint

Consider what happens if the timeout is set very low.

📝 Syntax
advanced
2:30remaining
Which option correctly mocks an API response in integration testing?

You want to mock a REST API response in your integration test using Python's unittest.mock. Which code snippet correctly mocks the client.get method to return a response with status code 200 and JSON {'success': true}?

A
with patch('client.get') as mock_get:
    mock_get.return_value = {'status_code': 200, 'json': {'success': True}}
B
with patch('client.get') as mock_get:
    mock_get.status_code = 200
    mock_get.json = {'success': True}
C
with patch('client.get') as mock_get:
    mock_get.return_value.status_code = 200
    mock_get.return_value.json = lambda: {'success': True}
D
with patch('client.get') as mock_get:
    mock_get.return_value.status_code = 200
    mock_get.return_value.json = {'success': True}
Attempts:
2 left
💡 Hint

Remember that json is a method, not a property.

🚀 Application
expert
2:00remaining
What is the number of items in the combined response after integration test aggregation?

An integration test calls two API endpoints: /products returning 3 items and /categories returning 2 items. The test aggregates both JSON lists into one list.

How many items will the combined list contain?

Rest API
products = client.get('/products').json()  # returns list of 3 items
categories = client.get('/categories').json()  # returns list of 2 items
combined = products + categories
print(len(combined))
A2
B6
C3
D5
Attempts:
2 left
💡 Hint

Think about how list concatenation works in Python.

Practice

(1/5)
1. What is the main goal of integration testing in REST API development?
easy
A. To measure the speed of the server hardware
B. To test the user interface design
C. To check if different parts of the application work together correctly
D. To write documentation for the API

Solution

  1. Step 1: Understand integration testing purpose

    Integration testing focuses on verifying that multiple components or services interact correctly.
  2. Step 2: Compare options with integration testing goal

    Only To check if different parts of the application work together correctly describes checking the cooperation of different parts, which matches integration testing.
  3. Final Answer:

    To check if different parts of the application work together correctly -> Option C
  4. Quick Check:

    Integration testing = check parts work together [OK]
Hint: Integration tests check connections between parts, not UI or speed [OK]
Common Mistakes:
  • Confusing integration testing with UI testing
  • Thinking integration tests measure performance
  • Believing integration tests are for documentation
2. Which of the following is the correct way to send a GET request in a Python integration test using the requests library?
easy
A. response = requests.get('http://api.example.com/data')
B. response = requests.post('http://api.example.com/data')
C. response = requests.fetch('http://api.example.com/data')
D. response = requests.send('http://api.example.com/data')

Solution

  1. Step 1: Recall correct method for GET request in requests library

    The method to send a GET request is requests.get().
  2. Step 2: Check options for correct syntax

    Only response = requests.get('http://api.example.com/data') uses requests.get() correctly with a URL string.
  3. Final Answer:

    response = requests.get('http://api.example.com/data') -> Option A
  4. Quick Check:

    GET request method = requests.get() [OK]
Hint: Use requests.get() for GET requests, not post, fetch, or send [OK]
Common Mistakes:
  • Using requests.post() instead of get() for GET requests
  • Using non-existent methods like fetch() or send()
  • Missing quotes around the URL string
3. Consider this Python integration test snippet using requests:
response = requests.post('http://api.example.com/login', json={'user':'admin', 'pass':'1234'})
print(response.status_code)
What will be the printed output if the login is successful and the server returns HTTP 200?
medium
A. 200
B. 403
C. 500
D. 404

Solution

  1. Step 1: Understand HTTP status codes

    HTTP 200 means success, 404 means not found, 500 means server error, 403 means forbidden.
  2. Step 2: Analyze the code output

    The code prints response.status_code. If login is successful, server returns 200, so output is 200.
  3. Final Answer:

    200 -> Option A
  4. Quick Check:

    Success status code = 200 [OK]
Hint: Successful HTTP response status is 200 [OK]
Common Mistakes:
  • Confusing 404 (not found) with success
  • Assuming 500 means success
  • Mixing 403 (forbidden) with success
4. 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?
medium
A. The URL is incorrect and returns 404
B. The requests library is not imported
C. The status code is not 200
D. The response JSON does not contain the key 'items'

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]
Hint: KeyError means JSON key missing, not URL or import error [OK]
Common Mistakes:
  • Assuming URL is wrong without checking status code
  • Ignoring that status code 200 means request succeeded
  • Forgetting to import requests library
5. You want to write an integration test that verifies your REST API endpoint /users returns only active users. The API returns JSON like {"users": [{"id":1,"active":true}, {"id":2,"active":false}]}. Which test code snippet correctly checks this condition?
hard
A. response = requests.get('http://api.example.com/users') users = response.json()['users'] assert any(user['active'] for user in users)
B. response = requests.get('http://api.example.com/users') users = response.json()['users'] assert all(user['active'] for user in users)
C. response = requests.get('http://api.example.com/users') users = response.json()['users'] assert user['active'] == True
D. response = requests.get('http://api.example.com/users') assert response.status_code == 404

Solution

  1. Step 1: Understand requirement to check all users are active

    The test must confirm every user in the list has active set to true.
  2. Step 2: Analyze code snippets for correctness

    response = requests.get('http://api.example.com/users') users = response.json()['users'] assert all(user['active'] for user in users) uses all() to check all users are active, which matches requirement. response = requests.get('http://api.example.com/users') users = response.json()['users'] assert any(user['active'] for user in users) checks if any user is active (not enough). response = requests.get('http://api.example.com/users') users = response.json()['users'] assert user['active'] == True references undefined variable user outside loop. response = requests.get('http://api.example.com/users') assert response.status_code == 404 checks for 404 status, which is wrong.
  3. Final Answer:

    response = requests.get('http://api.example.com/users') users = response.json()['users'] assert all(user['active'] for user in users) -> Option B
  4. Quick Check:

    Use all() to verify all users active [OK]
Hint: Use all() to confirm every user is active [OK]
Common Mistakes:
  • Using any() instead of all() to check all users
  • Referencing undefined variables in assertions
  • Checking wrong HTTP status code
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard