-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
89 lines (72 loc) · 3.2 KB
/
conftest.py
File metadata and controls
89 lines (72 loc) · 3.2 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
79
80
81
82
83
84
85
86
87
88
89
"""Pytest configuration and fixtures for Charon tests.
This module provides common test fixtures and configuration to ensure
tests run consistently regardless of the local environment.
"""
import os
import warnings
from typing import Any
import pytest
@pytest.fixture(autouse=True)
def mock_dotenv(monkeypatch: pytest.MonkeyPatch) -> None:
"""Prevent load_dotenv() from loading real .env files during tests.
This ensures tests don't accidentally use production credentials
and don't fail when .env files are missing.
"""
# Patch dotenv.load_dotenv to be a no-op
try:
import dotenv
monkeypatch.setattr(dotenv, "load_dotenv", lambda *args, **kwargs: None)
except ImportError:
pass # dotenv not installed, nothing to patch
@pytest.fixture(autouse=True)
def clean_env(monkeypatch: pytest.MonkeyPatch) -> None:
"""Set up a clean environment with default test values.
This fixture provides sensible defaults for environment variables
that scripts may expect, preventing failures when running tests
without a configured environment.
"""
# Default test database credentials (for Redmine scripts)
test_db_defaults = {
"REDMINE_DB_HOST": "localhost",
"REDMINE_DB_PORT": "5432",
"REDMINE_DB_USER": "test_user",
"REDMINE_DB_NAME": "test_db",
"REDMINE_DB_PASSWORD": "test_password",
}
# Default test API credentials
test_api_defaults = {
"REDMINE_URL": "https://redmine.test.example.com",
"REDMINE_API_KEY": "test-api-key",
"REDMINE_PROJECT": "test-project",
"CLOUDFLARE_API_TOKEN": "test-cloudflare-token",
"DOMAIN_NAME": "test.example.com",
}
# Set defaults only if not already set by the test
for key, value in {**test_db_defaults, **test_api_defaults}.items():
if key not in os.environ:
monkeypatch.setenv(key, value)
def pytest_configure(config: pytest.Config) -> None:
"""Configure pytest with custom markers and settings."""
# Register custom markers
config.addinivalue_line("markers", "requires_env: mark test as requiring specific environment variables")
config.addinivalue_line("markers", "requires_cert: mark test as requiring certificate files")
def pytest_collection_modifyitems(config: pytest.Config, items: list[Any]) -> None:
"""Modify test collection to handle missing dependencies gracefully.
Tests requiring external resources (certs, env files) will warn
instead of failing when those resources are unavailable.
"""
for item in items:
# Check for requires_cert marker
if item.get_closest_marker("requires_cert"):
cert_path = (
item.get_closest_marker("requires_cert").args[0]
if item.get_closest_marker("requires_cert").args
else None
)
if cert_path and not os.path.exists(cert_path):
item.add_marker(pytest.mark.skip(reason=f"Certificate not found: {cert_path} (this is expected in CI)"))
warnings.warn(
f"Skipping test {item.name}: certificate {cert_path} not found",
UserWarning,
stacklevel=2,
)