Test Overview
This test checks if the API client correctly fetches user data from a web service and verifies the response content.
Jump into concepts and practice - no test required
This test checks if the API client correctly fetches user data from a web service and verifies the response content.
import requests import pytest def get_user(user_id): response = requests.get(f"https://jsonplaceholder.typicode.com/users/{user_id}") response.raise_for_status() return response.json() def test_get_user(): user = get_user(1) assert user["id"] == 1 assert user["username"] == "Bret" assert "email" in user
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | pytest test runner initialized | — | PASS |
| 2 | Calls get_user(1) which sends GET request to https://jsonplaceholder.typicode.com/users/1 | HTTP request sent to API server | — | PASS |
| 3 | Receives HTTP 200 OK response with JSON user data | Response JSON: {"id":1,"username":"Bret","email":"Sincere@april.biz", ...} | — | PASS |
| 4 | Asserts user["id"] == 1 | User data loaded in memory | Check if user id equals 1 | PASS |
| 5 | Asserts user["username"] == "Bret" | User data loaded in memory | Check if username is 'Bret' | PASS |
| 6 | Asserts "email" key exists in user data | User data loaded in memory | Check if 'email' field is present | PASS |
| 7 | Test ends successfully | All assertions passed | — | PASS |
response.status_code.assert response.status_code == 200 is syntactically correct and checks the status code properly.def test_api_response(client):
response = client.get('/status')
data = response.json()
assert data['success'] is True
data['success'] is True, which matches the parsed value, so it passes.def test_get_user(client):
response = client.get('/user/1')
assert response.status_code = 200
assert response.json()['id'] == 1
assert response.status_code = 200 uses single '=' which is assignment, not comparison.assert response.status_code == 200.json={'name': 'book'} sends JSON properly; data= sends form data, which is incorrect here.response.status_code == 201 to check for created resource status; response.status is invalid.