Skip to content

Commit 85130d8

Browse files
committed
Fix response time validation test:
- Use config fixture properly - Add proper timing calculation with time.perf_counter() - Add better error context for timeouts - Add detailed metrics in Allure report - Improve error handling and reporting - Add request timeout protection
1 parent 97bb4ab commit 85130d8

1 file changed

Lines changed: 34 additions & 9 deletions

File tree

tests/test_api_validation.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import requests
1010
import allure
1111
import json
12+
import time
1213
from jsonschema import validate
1314
from utils.api_utils import load_schema, get_headers
1415

@@ -161,24 +162,48 @@ def test_single_user_schema_validation(base_url):
161162
@allure.title("Validate Response Time Performance")
162163
@allure.severity(allure.severity_level.NORMAL)
163164
@pytest.mark.parametrize("endpoint", ["/users?page=1", "/users/1"])
164-
def test_response_time_validation(endpoint, base_url):
165+
def test_response_time_validation(endpoint, base_url, config):
165166
"""
166167
Test that API responses are returned within acceptable time limits.
167168
Validates performance requirements.
169+
170+
Args:
171+
endpoint (str): The API endpoint to test
172+
base_url (str): Base URL from fixture
173+
config (dict): Configuration fixture containing timeout settings
168174
"""
169175
with allure.step(f"Send GET request to {endpoint}"):
170-
url = f"{base_url}{endpoint}"
171-
response = requests.get(url, headers=get_headers())
176+
try:
177+
url = f"{base_url}{endpoint}"
178+
start_time = time.perf_counter()
179+
response = requests.get(url, headers=get_headers(), timeout=10)
180+
response_time = time.perf_counter() - start_time
181+
except requests.exceptions.RequestException as e:
182+
allure.attach(str(e), "Request Error", allure.attachment_type.TEXT)
183+
raise
172184

173185
with allure.step("Verify response status is 200"):
174-
assert response.status_code == 200
186+
assert response.status_code == 200, (
187+
f"Expected status 200, got {response.status_code}. "
188+
f"Response: {response.text}"
189+
)
175190

176191
with allure.step("Validate response time is acceptable"):
177-
response_time = response.elapsed.total_seconds()
178-
# Expecting response within 5 seconds (configurable)
179192
max_response_time = config.get("timeout_seconds", 5)
180-
assert response_time < max_response_time, \
181-
f"Response time {response_time}s exceeded limit of {max_response_time}s"
193+
assert response_time < max_response_time, (
194+
f"Response time {response_time:.3f}s exceeded limit of {max_response_time}s. "
195+
f"Endpoint: {endpoint}"
196+
)
182197

183198
with allure.step("Attach performance metrics"):
184-
allure.attach(f"Response Time: {response_time:.3f} seconds", "Performance Metrics", allure.attachment_type.TEXT)
199+
metrics = {
200+
"endpoint": endpoint,
201+
"response_time": f"{response_time:.3f}s",
202+
"timeout_limit": f"{max_response_time}s",
203+
"status_code": response.status_code
204+
}
205+
allure.attach(
206+
json.dumps(metrics, indent=2),
207+
"Performance Metrics",
208+
allure.attachment_type.JSON
209+
)

0 commit comments

Comments
 (0)