Skip to content

Commit b6544d7

Browse files
committed
docs: improve module and function documentation
- Add comprehensive module-level docstrings - Document test categories and markers - Add detailed function documentation - Include usage examples and best practices - Document fixtures and dependencies - Add error handling documentation
1 parent dd7bd40 commit b6544d7

6 files changed

Lines changed: 219 additions & 47 deletions

File tree

conftest.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,39 @@
1+
"""
2+
conftest.py - Pytest Configuration and Fixtures
3+
4+
This module provides pytest configuration and fixtures for the API testing framework.
5+
It handles test setup, environment configuration, and common test dependencies.
6+
7+
Configuration:
8+
- Defines custom pytest markers
9+
- Sets up directory structure for reports
10+
- Creates Allure environment properties
11+
- Validates configuration files
12+
13+
Fixtures:
14+
config (session): Provides access to configuration data
15+
base_url (session): Base URL for API requests
16+
request_id (function): Unique identifier for each request
17+
log_test_start (function): Logs test start with metadata
18+
19+
Usage:
20+
The fixtures are automatically available to all test files.
21+
Example:
22+
def test_get_users(base_url, request_id):
23+
response = requests.get(f"{base_url}/users")
24+
assert response.status_code == 200
25+
26+
Environment Variables:
27+
TEST_ENV: Override the environment from config.yaml (default: 'dev')
28+
DEBUG: Enable debug logging (default: False)
29+
30+
Directory Structure:
31+
/config/
32+
config.yaml: Main configuration file
33+
/reports/
34+
environment.properties: Allure environment data
35+
"""
36+
137
import pytest
238
import allure
339
import os

tests/test_api_validation.py

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
11
"""
2-
test_api_validation.py
3-
4-
Test with allure.step("Send GET request to /users?page=1 with retry resilience"):
5-
try:
6-
url = f"{base_url}/users?page=1"
7-
response = make_resilient_get_request(url, headers=get_headers(), timeout=10)
8-
log_request_response(response) # Enhanced logging
9-
except requests.exceptions.RequestException as e:
10-
allure.attach(str(e), "Request Error", allure.attachment_type.TEXT)
11-
raise
12-
13-
with allure.step("Verify response status is 200"):cused on API contract validation and da with allure.step("Send GET request to /users?page=1"):
14-
url = f"{base_url}/users?page=1"
15-
response = requests.get(url, headers=get_headers())
16-
log_request_response(response) # Enhanced logging
17-
18-
with allure.step("Verify response status is 200"):tegrity.
19-
Covers schema validation, header validation, and response format testing.
2+
test_api_validation.py - API Contract Testing
3+
4+
This module implements contract tests for the ReqRes API, focusing on:
5+
1. Schema Validation: Ensures API responses match defined JSON schemas
6+
2. Header Validation: Verifies correct content types and response headers
7+
3. Response Format: Validates data structure and field types
8+
4. Error Handling: Tests API error responses and status codes
9+
10+
Test Categories:
11+
- Contract Testing (@pytest.mark.contract)
12+
- Response Validation (@pytest.mark.response)
13+
- Schema Validation (@pytest.mark.schema)
14+
- Error Response (@pytest.mark.error)
15+
16+
Each test is documented with:
17+
- Purpose and description
18+
- Prerequisites and dependencies
19+
- Expected results
20+
- Allure annotations for reporting
21+
22+
Example Test Structure:
23+
@pytest.mark.contract
24+
def test_user_schema():
25+
1. Send request to API
26+
2. Validate response against schema
27+
3. Check specific fields
28+
4. Verify error cases
29+
30+
Required Fixtures:
31+
- base_url: API base URL from config
32+
- request_id: Unique test identifier
2033
"""
2134

2235
import pytest

tests/test_error_scenarios.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,41 @@
11
"""
2-
test_error_scenarios.p url = f"{base_url}/users/999"
3-
response = requests.get(url, headers=get_headers(), timeout=10)
4-
log_request_response(response) # Enhanced logging
5-
6-
with allure.step("Verify response status is 404"):est module focused on error scenarios and negative testing.
7-
Covers 404 errors, invalid requests, and edge cases.
2+
test_error_scenarios.py - API Error Handling Tests
3+
4+
This module implements negative testing and error scenarios for the ReqRes API.
5+
It verifies that the API handles error conditions gracefully and returns
6+
appropriate error responses.
7+
8+
Test Categories:
9+
- Non-existent Resources (@pytest.mark.not_found)
10+
- Invalid Input (@pytest.mark.invalid_input)
11+
- Authentication Errors (@pytest.mark.auth)
12+
- Rate Limiting (@pytest.mark.rate_limit)
13+
14+
Error Scenarios Tested:
15+
1. Resource Not Found (404)
16+
- Non-existent users
17+
- Deleted resources
18+
2. Invalid Requests (400)
19+
- Malformed JSON
20+
- Missing required fields
21+
- Invalid data types
22+
3. Authentication (401/403)
23+
- Invalid tokens
24+
- Missing credentials
25+
4. Rate Limiting (429)
26+
- Excessive requests
27+
- Throttling behavior
28+
29+
Best Practices Implemented:
30+
- Detailed error response validation
31+
- Error message content verification
32+
- HTTP status code checking
33+
- Response header validation
34+
- Error response schema validation
35+
36+
Required Fixtures:
37+
- base_url: API base URL from config
38+
- invalid_token: Invalid authentication token
839
"""
940

1041
import pytest

tests/test_user_creation.py

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,45 @@
11
"""
2-
test_user_creation.py
3-
4-
Test module focused on user creation operations (POST requests).
5-
Covers data-driven testing, boundary testing, and user creation scenarios.
2+
test_user_creation.py - User Creation API Tests
3+
4+
This module implements data-driven tests for user creation endpoints in the ReqRes API.
5+
It focuses on POST operations with various payload combinations and validates the
6+
creation responses.
7+
8+
Features Tested:
9+
1. User Creation
10+
- Valid user data
11+
- Required fields only
12+
- All fields populated
13+
- Special characters in names
14+
2. Response Validation
15+
- Status code (201)
16+
- Response schema
17+
- Created user data
18+
3. Edge Cases
19+
- Minimum/maximum field lengths
20+
- Unicode characters
21+
- Whitespace handling
22+
23+
Test Data:
24+
Location: data/post_user_payloads.json
25+
Format: List of user creation payloads
26+
Example:
27+
[
28+
{
29+
"name": "John Doe",
30+
"job": "Developer"
31+
},
32+
...
33+
]
34+
35+
Test Categories:
36+
- Smoke Tests (@pytest.mark.smoke)
37+
- User Creation (@pytest.mark.user_creation)
38+
- Data Validation (@pytest.mark.validation)
39+
40+
Required Fixtures:
41+
- base_url: API base URL from config
42+
- request_id: Unique identifier for each request
643
"""
744

845
import pytest

tests/test_user_retrieval.py

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
11
"""
2-
test_user_retrieval.py
3-
try:
4-
url = f"{base_url}/users?page=1"
5-
response = requests.get(url, headers=get_headers(), timeout=10)
6-
log_request_response(response) # Enhanced logging
7-
except requests.exceptions.RequestException as e:
8-
allure.attach(str(e), "Request Error", allure.attachment_type.TEXT)
9-
raise
10-
11-
with allure.step("Verify response status is 200"):dule focused on user retrieval operations (GET requests).
12-
Covers listing users, retrieving single users, and related functionality.
2+
test_user_retrieval.py - User Retrieval API Tests
3+
4+
This module implements tests for user retrieval endpoints in the ReqRes API.
5+
It covers GET operations for both single user and user list endpoints, including
6+
pagination, filtering, and response validation.
7+
8+
Endpoints Tested:
9+
1. List Users (/users)
10+
- Pagination
11+
- Per page limits
12+
- Total count validation
13+
2. Single User (/users/{id})
14+
- Valid user IDs
15+
- Response format
16+
- User data structure
17+
3. User Filtering
18+
- Query parameters
19+
- Sort options
20+
- Field selection
21+
22+
Response Validation:
23+
- Status codes (200, 404)
24+
- JSON schema compliance
25+
- Data structure
26+
- Field types and formats
27+
- Pagination metadata
28+
29+
Test Categories:
30+
- Smoke Tests (@pytest.mark.smoke)
31+
- User Retrieval (@pytest.mark.user_retrieval)
32+
- Pagination (@pytest.mark.pagination)
33+
34+
Required Fixtures:
35+
- base_url: API base URL from config
36+
- request_id: Unique identifier for each request
37+
38+
Schema Validation:
39+
Uses schemas/user_list_schema.json and schemas/single_user_schema.json
40+
to validate response formats
1341
"""
1442

1543
import pytest

utils/api_utils.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,39 @@
11
"""
2-
api_utils.py
3-
4-
Utility functions for API test automation:
5-
- Loads configuration and schema files.
6-
- Provides default HTTP headers.
7-
- Implements request retry logic.
8-
- Attaches request/response details to Allure reports.
9-
- Validates API responses against JSON schemas.
2+
api_utils.py - API Testing Utility Functions
3+
4+
This module provides core functionality for API testing, including:
5+
- Configuration management (loading and parsing config files)
6+
- Schema validation (JSON schema loading and response validation)
7+
- HTTP request handling (headers, retry logic, response processing)
8+
- Test reporting (Allure integration for requests and responses)
9+
- Error handling (custom exceptions and retry mechanisms)
10+
11+
The module is designed to work with the ReqRes API (https://reqres.in/) but can be
12+
adapted for any RESTful API testing needs.
13+
14+
Classes:
15+
None
16+
17+
Functions:
18+
load_config() -> dict: Load and parse the config.yaml file
19+
load_schema(schema_filename: str) -> dict: Load a JSON schema file
20+
get_headers() -> dict: Get default HTTP headers from config
21+
retry_request(func) -> callable: Decorator for request retry logic
22+
validate_response(response: Response, schema: dict) -> None: Validate response against schema
23+
attach_request_details(response: Response) -> None: Add request details to Allure report
24+
25+
Dependencies:
26+
- pyyaml: For parsing config.yaml
27+
- requests: For HTTP requests
28+
- jsonschema: For JSON schema validation
29+
- allure-pytest: For test reporting
30+
31+
Example Usage:
32+
>>> config = load_config()
33+
>>> headers = get_headers()
34+
>>> response = requests.get(f"{config['base_url']}/users", headers=headers)
35+
>>> schema = load_schema("user_list_schema.json")
36+
>>> validate_response(response, schema)
1037
"""
1138

1239
import yaml

0 commit comments

Comments
 (0)