Test Overview
This test sends a POST request with a JSON body to an API endpoint and verifies the response status and content.
This test sends a POST request with a JSON body to an API endpoint and verifies the response status and content.
import requests import json def test_post_json_body(): url = "https://api.example.com/users" headers = {"Content-Type": "application/json"} payload = { "name": "Alice", "email": "alice@example.com", "age": 30 } response = requests.post(url, headers=headers, json=payload) assert response.status_code == 201 response_json = response.json() assert response_json.get("id") is not None assert response_json.get("name") == "Alice"
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Test environment ready, no requests sent yet | — | PASS |
| 2 | Sends POST request to https://api.example.com/users with JSON body {"name": "Alice", "email": "alice@example.com", "age": 30} | Request sent, waiting for response | — | PASS |
| 3 | Receives response with status code 201 and JSON body containing user data | Response received: {"id": "12345", "name": "Alice", "email": "alice@example.com", "age": 30} | Check response status code is 201 | PASS |
| 4 | Parses response JSON and verifies 'id' field is present | Response JSON parsed | Assert 'id' field is not None | PASS |
| 5 | Verifies 'name' field in response JSON equals 'Alice' | Response JSON parsed | Assert 'name' == 'Alice' | PASS |
| 6 | Test ends successfully | All assertions passed | — | PASS |
{"name": "Alice", "age": 30} uses double quotes correctly around keys and string values. The other options use incorrect quotes or syntax.{"colors": ["red", "green", "blue"]} correctly uses square brackets and double quotes. The other options are invalid: one uses parentheses, another is missing array brackets, and one lacks quotes around strings and keys.{
"user": {
"id": 101,
"active": true
},
"roles": ["admin", "editor"]
}user.active?{
"product": "Book",
"price": 12.99,
"inStock": yes
}{
"user": {
"name": "Bob",
"email": "bob@example.com",
"preferences": {
"notifications": false,
"themes": ["dark", "blue"]
}
}
}{ "user": { "name": "Bob", "email": "bob@example.com", "preferences": { "notifications": false, "themes": ["dark", "blue"], "language": "en" } } } adds "language": "en" inside "preferences" correctly. { "user": { "name": "Bob", "email": "bob@example.com", "preferences": { "notifications": false, "themes": ["dark", "blue"] }, "language": "en" } } adds "language" outside "preferences". { "user": { "name": "Bob", "email": "bob@example.com", "preferences": { "notifications": false, "themes": ["dark", "blue"] }, "preferences.language": "en" } } uses invalid key "preferences.language". { "user": { "name": "Bob", "email": "bob@example.com", "preferences": { "notifications": false, "themes": ["dark", "blue"] }, "preferences": { "language": "en" } } } overwrites "preferences" object, losing existing keys.Pass the test and this pattern moves unknown