88import pytest
99import requests
1010import allure
11- from utils .api_utils import load_config , get_headers
12-
13- # Load base configuration from config.yaml
14- config = load_config ()
15- BASE_URL = config ["environments" ][config ["env" ]]["base_url" ]
11+ import json
12+ from utils .api_utils import get_headers
1613
1714
1815@pytest .mark .error_scenarios
2118@allure .feature ("User Retrieval Errors" )
2219@allure .title ("GET Non-Existent User" )
2320@allure .severity (allure .severity_level .MINOR )
24- def test_get_user_not_found ():
21+ def test_get_user_not_found (base_url ):
2522 """
2623 Test retrieving a non-existent user.
2724 Expects a 404 Not Found response.
2825 """
2926 with allure .step ("Send GET request for non-existent user" ):
30- url = f"{ BASE_URL } /users/9999"
31- response = requests .get (url , headers = get_headers ())
27+ url = f"{ base_url } /users/9999"
28+ response = requests .get (url , headers = get_headers (), timeout = 10 )
3229
3330 with allure .step ("Verify response status is 404" ):
34- assert response .status_code == 404
31+ assert response .status_code == 404 , (
32+ f"Expected status 404, got { response .status_code } . "
33+ f"Response: { response .text } "
34+ )
3535
3636 with allure .step ("Verify response body is empty or contains error info" ):
37- # ReqRes API returns empty object for 404
38- data = response .json ()
39- assert data == {}
37+ try :
38+ data = response .json ()
39+ assert data == {}, f"Expected empty object, got: { json .dumps (data , indent = 2 )} "
40+ except json .JSONDecodeError as e :
41+ allure .attach (response .text , "Invalid JSON Response" , allure .attachment_type .TEXT )
42+ allure .attach (str (e ), "JSON Parse Error" , allure .attachment_type .TEXT )
43+ raise
4044
4145 with allure .step ("Attach response details to report" ):
4246 allure .attach (response .text , "404 Response Body" , allure .attachment_type .JSON )
47+ allure .attach (
48+ json .dumps (get_headers (), indent = 2 ),
49+ "Request Headers" ,
50+ allure .attachment_type .JSON
51+ )
4352
4453
4554@pytest .mark .error_scenarios
@@ -49,13 +58,13 @@ def test_get_user_not_found():
4958@allure .title ("GET User with Invalid ID Format" )
5059@allure .severity (allure .severity_level .MINOR )
5160@pytest .mark .parametrize ("invalid_id" , ["abc" , "!@#" , "0" , "-1" ])
52- def test_get_user_invalid_id (invalid_id ):
61+ def test_get_user_invalid_id (invalid_id , base_url ):
5362 """
5463 Test retrieving a user with invalid ID formats.
5564 Validates error handling for malformed requests.
5665 """
5766 with allure .step (f"Send GET request with invalid ID: { invalid_id } " ):
58- url = f"{ BASE_URL } /users/{ invalid_id } "
67+ url = f"{ base_url } /users/{ invalid_id } "
5968 response = requests .get (url , headers = get_headers ())
6069
6170 with allure .step ("Verify response indicates error or not found" ):
@@ -72,13 +81,13 @@ def test_get_user_invalid_id(invalid_id):
7281@allure .feature ("User Creation Errors" )
7382@allure .title ("POST User with Empty Payload" )
7483@allure .severity (allure .severity_level .NORMAL )
75- def test_create_user_empty_payload ():
84+ def test_create_user_empty_payload (base_url ):
7685 """
7786 Test creating a user with empty payload.
7887 Validates API behavior with missing data.
7988 """
8089 with allure .step ("Send POST request with empty payload" ):
81- url = f"{ BASE_URL } /users"
90+ url = f"{ base_url } /users"
8291 response = requests .post (url , json = {}, headers = get_headers ())
8392
8493 with allure .step ("Verify response (API may accept empty payload)" ):
@@ -101,13 +110,13 @@ def test_create_user_empty_payload():
101110@allure .feature ("User Creation Errors" )
102111@allure .title ("POST User with Invalid JSON" )
103112@allure .severity (allure .severity_level .NORMAL )
104- def test_create_user_invalid_json ():
113+ def test_create_user_invalid_json (base_url ):
105114 """
106115 Test creating a user with malformed JSON.
107116 Validates API error handling for invalid request format.
108117 """
109118 with allure .step ("Send POST request with invalid JSON" ):
110- url = f"{ BASE_URL } /users"
119+ url = f"{ base_url } /users"
111120 headers = get_headers ()
112121 # Send malformed JSON as string
113122 response = requests .post (url , data = '{"name": "test", "job":}' , headers = headers )
@@ -126,13 +135,13 @@ def test_create_user_invalid_json():
126135@allure .feature ("Invalid Endpoints" )
127136@allure .title ("GET Invalid Endpoint" )
128137@allure .severity (allure .severity_level .MINOR )
129- def test_invalid_endpoint ():
138+ def test_invalid_endpoint (base_url ):
130139 """
131140 Test accessing an invalid/non-existent endpoint.
132141 ReqRes API may handle invalid endpoints differently than expected.
133142 """
134143 with allure .step ("Send GET request to invalid endpoint" ):
135- url = f"{ BASE_URL } /invalid-endpoint"
144+ url = f"{ base_url } /invalid-endpoint"
136145 response = requests .get (url , headers = get_headers ())
137146
138147 with allure .step ("Document API behavior for invalid endpoints" ):
@@ -160,14 +169,14 @@ def test_invalid_endpoint():
160169@allure .feature ("Invalid Endpoints" )
161170@allure .title ("GET Non-existent User ID" )
162171@allure .severity (allure .severity_level .MINOR )
163- def test_malformed_url ():
172+ def test_malformed_url (base_url ):
164173 """
165174 Test accessing a user with an ID that definitely doesn't exist.
166175 ReqRes API returns 404 for non-existent user IDs.
167176 """
168177 with allure .step ("Send GET request to non-existent user ID" ):
169178 # Use a very high user ID that definitely doesn't exist
170- url = f"{ BASE_URL } /users/999999"
179+ url = f"{ base_url } /users/999999"
171180 response = requests .get (url , headers = get_headers ())
172181
173182 with allure .step ("Verify response indicates not found" ):
0 commit comments