|
9 | 9 | import requests |
10 | 10 | import allure |
11 | 11 | import json |
| 12 | +import time |
12 | 13 | from jsonschema import validate |
13 | 14 | from utils.api_utils import load_schema, get_headers |
14 | 15 |
|
@@ -161,24 +162,48 @@ def test_single_user_schema_validation(base_url): |
161 | 162 | @allure.title("Validate Response Time Performance") |
162 | 163 | @allure.severity(allure.severity_level.NORMAL) |
163 | 164 | @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): |
165 | 166 | """ |
166 | 167 | Test that API responses are returned within acceptable time limits. |
167 | 168 | 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 |
168 | 174 | """ |
169 | 175 | 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 |
172 | 184 |
|
173 | 185 | 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 | + ) |
175 | 190 |
|
176 | 191 | with allure.step("Validate response time is acceptable"): |
177 | | - response_time = response.elapsed.total_seconds() |
178 | | - # Expecting response within 5 seconds (configurable) |
179 | 192 | 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 | + ) |
182 | 197 |
|
183 | 198 | 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