Request methods and status codes in Testing Fundamentals - Build an Automation Script
import requests import pytest def test_get_post_and_status_code(): # Step 1: Send GET request get_response = requests.get('https://jsonplaceholder.typicode.com/posts/1') # Step 2: Assert status code 200 assert get_response.status_code == 200, f"Expected 200 but got {get_response.status_code}" # Step 3: Assert response JSON has id=1 json_data = get_response.json() assert json_data.get('id') == 1, f"Expected id=1 but got {json_data.get('id')}" # Step 4: Send POST request post_payload = {"title": "foo", "body": "bar", "userId": 1} post_response = requests.post('https://jsonplaceholder.typicode.com/posts', json=post_payload) # Step 5: Assert status code 201 assert post_response.status_code == 201, f"Expected 201 but got {post_response.status_code}" # Step 6: Assert response JSON has non-null id post_json = post_response.json() assert post_json.get('id') is not None, "Expected non-null id in POST response"
This test uses the requests library to send HTTP requests.
First, it sends a GET request to fetch post with id 1 and checks the status code is 200, meaning success.
It then verifies the JSON response contains the expected id field with value 1.
Next, it sends a POST request with a JSON payload to create a new post.
The test asserts the POST response status code is 201, which means resource created successfully.
Finally, it checks the response JSON contains a non-null id field, indicating the new post was assigned an id.
Assertions include helpful messages for easier debugging.
This clear step-by-step approach helps beginners understand request methods and status codes practically.
Now add data-driven testing with 3 different POST payloads to verify creation with varied data
Practice
Solution
Step 1: Understand the purpose of HTTP methods
GETis used to request data without modifying it, whilePOST,PUT, andDELETEchange data on the server.Step 2: Match method to action
Since the question asks for retrieving data without changes,GETis the correct method.Final Answer:
GET -> Option DQuick Check:
Retrieve data = GET [OK]
- Confusing POST with GET for data retrieval
- Thinking PUT or DELETE retrieve data
- Assuming GET modifies data
Solution
Step 1: Identify success status codes
HTTP status codes starting with 2xx indicate success.200 OKmeans the request succeeded and data is returned.Step 2: Eliminate error and redirect codes
404means not found,500means server error, and301means redirect. These do not indicate success with data returned.Final Answer:
200 OK -> Option AQuick Check:
Success code = 200 OK [OK]
- Choosing 404 thinking it means success
- Confusing 301 redirect with success
- Mixing client and server error codes
DELETE request to remove a resource successfully?Solution
Step 1: Understand DELETE success codes
A successfulDELETErequest usually returns204 No Content, meaning the resource was deleted and no content is returned.Step 2: Eliminate other codes
201 Createdis for resource creation,400 Bad Requestmeans client error, and403 Forbiddenmeans no permission.Final Answer:
204 No Content -> Option BQuick Check:
DELETE success = 204 No Content [OK]
- Choosing 201 which is for creation
- Confusing 400 with success
- Thinking 403 means success
POST request to create a new user but receives a 405 Method Not Allowed status. What is the most likely cause?Solution
Step 1: Understand 405 status meaning
405 Method Not Allowedmeans the HTTP method used is not supported by the server for the requested URL.Step 2: Eliminate other causes
Invalid data usually causes 400 errors, server down causes 5xx errors, and timeout causes no response or 408.Final Answer:
The server does not support the POST method on this URL -> Option AQuick Check:
405 = Method not allowed [OK]
- Thinking 405 means invalid data
- Confusing 405 with server errors
- Assuming timeout causes 405
PUT request to update a resource returns 409 Conflict. What should the tester check next?Solution
Step 1: Understand 409 Conflict meaning
409 Conflictmeans the request could not be completed due to a conflict with the current state of the resource, often caused by simultaneous updates.Step 2: Identify what to check
The tester should check if the resource exists and if there are conflicting changes that prevent the update.Final Answer:
If the resource already exists and has conflicting changes -> Option CQuick Check:
409 = Conflict with resource state [OK]
- Assuming 409 means wrong URL
- Thinking 409 means server overload
- Confusing 409 with method errors
Ready to prove it?
Pass the test and this pattern moves unknown
