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 |