-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
78 lines (66 loc) · 1.94 KB
/
conftest.py
File metadata and controls
78 lines (66 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import pytest
import os
import json
from api.clients.base_client import BaseAPIClient
# Config fixture for environment management
@pytest.fixture
def config():
"""
Load configuration based on the environment.
Default to 'dev' if not specified.
"""
env = os.environ.get("TEST_ENV", "dev")
config_dir = os.path.join(os.path.dirname(__file__), "config")
# Config file path
config_file = os.path.join(config_dir, f"{env}.json")
# Verify config file exists
if not os.path.exists(config_file):
raise FileNotFoundError(f"Configuration file not found: {config_file}")
# Load the configuration
with open(config_file, "r") as f:
return json.load(f)
# Base API client fixture using environment configuration
@pytest.fixture
def api_client(config):
"""
Create a base API client using environment-specific configuration.
"""
client = BaseAPIClient(
base_url=config["base_url"],
timeout=config["timeout"],
auth=(config["auth"]["username"], config["auth"]["password"]) if "auth" in config else None
)
# Set default headers if provided in config
if "headers" in config:
for key, value in config["headers"].items():
client.session.headers[key] = value
yield client
# Clean up
client.close()
# Test data fixture
@pytest.fixture
def test_data(config):
"""
Provide test data from configuration.
"""
if "test_data" in config:
return config["test_data"]
# Default test data if not in config
return {
"post_id": 1,
"user_id": 1
}
# Endpoints fixture
@pytest.fixture
def endpoints(config):
"""
Provide API endpoints from configuration.
"""
if "endpoints" in config:
return config["endpoints"]
# Default endpoints if not in config
return {
"posts": "/posts",
"users": "/users",
"comments": "/comments"
}