Integration testing checks if different parts of a program work well together. It helps find problems when pieces connect.
Integration testing in Rest API
def test_api_integration(): response = client.get('/api/resource') assert response.status_code == 200 assert 'expected_key' in response.json()
This example shows a simple integration test for a REST API endpoint.
Use a test client to send requests and check responses.
def test_get_user(): response = client.get('/users/1') assert response.status_code == 200 assert response.json()['id'] == 1
def test_create_post(): data = {'title': 'Hello', 'content': 'World'} response = client.post('/posts', json=data) assert response.status_code == 201 assert response.json()['title'] == 'Hello'
This program creates a simple API with two endpoints: one to add posts and one to get all posts. The test sends a post, then checks if it was saved and can be retrieved.
from fastapi import FastAPI from fastapi.testclient import TestClient app = FastAPI() posts = [] @app.post('/posts') def create_post(post: dict): posts.append(post) return post @app.get('/posts') def get_posts(): return posts client = TestClient(app) def test_create_and_get_posts(): new_post = {'title': 'Test', 'content': 'Integration'} response = client.post('/posts', json=new_post) assert response.status_code == 201 assert response.json() == new_post response = client.get('/posts') assert response.status_code == 200 assert new_post in response.json() if __name__ == '__main__': test_create_and_get_posts() print('Integration test passed')
Integration tests are slower than unit tests because they test multiple parts together.
Keep integration tests focused on how parts work together, not on internal details.
Use test databases or mock services to avoid changing real data.
Integration testing checks if different parts of your app work together correctly.
It is useful for testing API endpoints, databases, and service communication.
Write tests that send requests and check responses to catch connection problems early.
Practice
integration testing in REST API development?Solution
Step 1: Understand integration testing purpose
Integration testing focuses on verifying that multiple components or services interact correctly.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.Final Answer:
To check if different parts of the application work together correctly -> Option CQuick Check:
Integration testing = check parts work together [OK]
- Confusing integration testing with UI testing
- Thinking integration tests measure performance
- Believing integration tests are for documentation
requests library?Solution
Step 1: Recall correct method for GET request in requests library
The method to send a GET request isrequests.get().Step 2: Check options for correct syntax
Only response = requests.get('http://api.example.com/data') usesrequests.get()correctly with a URL string.Final Answer:
response = requests.get('http://api.example.com/data') -> Option AQuick Check:
GET request method = requests.get() [OK]
- Using requests.post() instead of get() for GET requests
- Using non-existent methods like fetch() or send()
- Missing quotes around the URL string
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?Solution
Step 1: Understand HTTP status codes
HTTP 200 means success, 404 means not found, 500 means server error, 403 means forbidden.Step 2: Analyze the code output
The code printsresponse.status_code. If login is successful, server returns 200, so output is 200.Final Answer:
200 -> Option AQuick Check:
Success status code = 200 [OK]
- Confusing 404 (not found) with success
- Assuming 500 means success
- Mixing 403 (forbidden) with success
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?Solution
Step 1: Understand KeyError cause
A KeyError means the key 'items' is missing in the JSON response.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.Final Answer:
The response JSON does not contain the key 'items' -> Option DQuick Check:
KeyError means missing key in JSON [OK]
- Assuming URL is wrong without checking status code
- Ignoring that status code 200 means request succeeded
- Forgetting to import requests library
/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?Solution
Step 1: Understand requirement to check all users are active
The test must confirm every user in the list hasactiveset to true.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) usesall()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 variableuseroutside loop. response = requests.get('http://api.example.com/users') assert response.status_code == 404 checks for 404 status, which is wrong.Final Answer:
response = requests.get('http://api.example.com/users') users = response.json()['users'] assert all(user['active'] for user in users) -> Option BQuick Check:
Use all() to verify all users active [OK]
- Using any() instead of all() to check all users
- Referencing undefined variables in assertions
- Checking wrong HTTP status code
Ready to prove it?
Pass the test and this pattern moves unknown
