Spaces:
Sleeping
Sleeping
File size: 3,809 Bytes
b9c68d4 | 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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | """Global pytest configuration and fixtures."""
import os
import sys
import pytest
import tempfile
import shutil
from pathlib import Path
from unittest.mock import Mock, patch
# Add src to Python path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
@pytest.fixture(scope="session")
def project_root():
"""Return the project root directory."""
return Path(__file__).parent.parent
@pytest.fixture(scope="session")
def test_data_dir(project_root):
"""Return the test data directory."""
return project_root / "tests" / "data"
@pytest.fixture
def temp_dir():
"""Create a temporary directory for tests."""
temp_path = tempfile.mkdtemp()
yield Path(temp_path)
shutil.rmtree(temp_path, ignore_errors=True)
@pytest.fixture
def mock_database():
"""Mock database connection."""
with patch('sqlite3.connect') as mock_connect:
mock_db = Mock()
mock_cursor = Mock()
mock_db.cursor.return_value = mock_cursor
mock_connect.return_value = mock_db
yield mock_db, mock_cursor
@pytest.fixture
def sample_market_data():
"""Sample market data for testing."""
return {
'symbol': 'AAPL',
'price': 150.0,
'volume': 1000000,
'timestamp': '2024-01-01T10:00:00Z',
'high': 155.0,
'low': 145.0,
'open': 148.0,
'close': 150.0
}
@pytest.fixture
def mock_gradio_interface():
"""Mock Gradio interface for UI tests."""
with patch('gradio.Interface') as mock_interface:
mock_instance = Mock()
mock_interface.return_value = mock_instance
yield mock_instance
@pytest.fixture
def mock_transformers():
"""Mock transformers library."""
with patch('transformers.pipeline') as mock_pipeline:
mock_model = Mock()
mock_model.return_value = [{'label': 'POSITIVE', 'score': 0.9}]
mock_pipeline.return_value = mock_model
yield mock_model
@pytest.fixture(autouse=True)
def setup_test_environment(monkeypatch):
"""Setup test environment variables."""
monkeypatch.setenv('TESTING', 'true')
monkeypatch.setenv('LOG_LEVEL', 'DEBUG')
monkeypatch.setenv('DATABASE_URL', ':memory:')
@pytest.fixture
def capture_logs(caplog):
"""Capture logs during tests."""
import logging
caplog.set_level(logging.DEBUG)
return caplog
def pytest_configure(config):
"""Configure pytest."""
# Create reports directory if it doesn't exist
reports_dir = Path("reports")
reports_dir.mkdir(exist_ok=True)
# Create test data directory if it doesn't exist
test_data_dir = Path("tests/data")
test_data_dir.mkdir(parents=True, exist_ok=True)
def pytest_collection_modifyitems(config, items):
"""Modify test collection."""
# Add markers based on test file location
for item in items:
if "unit" in str(item.fspath):
item.add_marker(pytest.mark.unit)
elif "integration" in str(item.fspath):
item.add_marker(pytest.mark.integration)
# Mark slow tests
if "slow" in item.name or "performance" in item.name:
item.add_marker(pytest.mark.slow)
def pytest_runtest_setup(item):
"""Setup for each test."""
# Skip slow tests unless explicitly requested
if "slow" in item.keywords and not item.config.getoption("--runslow", default=False):
pytest.skip("need --runslow option to run")
def pytest_addoption(parser):
"""Add custom command line options."""
parser.addoption(
"--runslow", action="store_true", default=False, help="run slow tests"
)
parser.addoption(
"--runintegration", action="store_true", default=False, help="run integration tests"
) |