""" pytest配置文件 ============= 提供全局的fixtures和测试配置 """ import os import sys import json import tempfile import shutil from pathlib import Path from typing import Generator, Dict, Any from unittest.mock import Mock, MagicMock, patch import pytest from PIL import Image import numpy as np # 添加项目根目录到Python路径 PROJECT_ROOT = Path(__file__).parent.parent sys.path.insert(0, str(PROJECT_ROOT)) @pytest.fixture(scope="session") def project_root() -> Path: """返回项目根目录路径""" return PROJECT_ROOT @pytest.fixture(scope="session") def test_data_dir() -> Path: """返回测试数据目录""" return PROJECT_ROOT / "tests" / "fixtures" @pytest.fixture def temp_dir() -> Generator[Path, None, None]: """创建临时目录,测试后自动清理""" temp_path = Path(tempfile.mkdtemp()) yield temp_path shutil.rmtree(temp_path, ignore_errors=True) @pytest.fixture def temp_config_file(temp_dir: Path) -> Path: """创建临时配置文件""" config_path = temp_dir / "config.json" default_config = { "needswitch": False, "infinite_loop_enabled": False, "wait_minutes": 1, "min_prize_value_yuan": 0.0, "min_participants": 0, "max_participants": 0, "skip_if_countdown_gt_seconds": 600, "fudai_icon_max_wait_seconds": 5.0, "fudai_icon_retry_delay_seconds": 0.5, "no_attend_timeout_minutes": 30.0, "y_pianyi": 0, } with open(config_path, "w", encoding="utf-8") as f: json.dump(default_config, f, indent=2, ensure_ascii=False) return config_path @pytest.fixture def mock_config() -> Dict[str, Any]: """返回Mock配置字典""" return { "needswitch": False, "infinite_loop_enabled": False, "wait_minutes": 1, "min_prize_value_yuan": 1.0, "min_participants": 0, "max_participants": 0, "skip_if_countdown_gt_seconds": 600, "skip_if_countdown_lt_seconds": 5, "fudai_icon_max_wait_seconds": 5.0, "fudai_icon_retry_delay_seconds": 0.5, "no_attend_timeout_minutes": 30.0, "y_pianyi": 0, "resolution_ratio_x": 1080, "resolution_ratio_y": 2400, } @pytest.fixture def mock_analyser(mock_config: Dict[str, Any]) -> Mock: """创建Mock的analyser对象""" analyser = Mock() analyser.device_id = "test_device_123" analyser.y_pianyi = mock_config["y_pianyi"] analyser.resolution_ratio_x = mock_config["resolution_ratio_x"] analyser.resolution_ratio_y = mock_config["resolution_ratio_y"] analyser.min_prize_value_yuan = mock_config["min_prize_value_yuan"] analyser.min_participants = mock_config["min_participants"] analyser.max_participants = mock_config["max_participants"] analyser.skip_if_countdown_gt_seconds = mock_config["skip_if_countdown_gt_seconds"] analyser.skip_if_countdown_lt_seconds = mock_config["skip_if_countdown_lt_seconds"] analyser.fudai_icon_max_wait_seconds = mock_config["fudai_icon_max_wait_seconds"] analyser.fudai_icon_retry_delay_seconds = mock_config["fudai_icon_retry_delay_seconds"] analyser._no_attend_timeout_minutes = mock_config["no_attend_timeout_minutes"] analyser.last_fudai_x = 0 analyser._last_successful_attend_ts = 0.0 analyser._run_start_ts = 0.0 analyser._log_kv = Mock() analyser._log_step_time = Mock() analyser.operation = Mock() analyser.operation.get_screenshot = Mock(return_value=True) analyser.operation.click = Mock() analyser.operation.click_back = Mock() analyser.operation.delay = Mock() analyser.operation.get_device_resolution = Mock(return_value=(1080, 2400)) return analyser @pytest.fixture def mock_operation() -> Mock: """创建Mock的operation对象""" operation = Mock() operation.get_screenshot = Mock(return_value=True) operation.click = Mock() operation.click_back = Mock() operation.swipe = Mock() operation.delay = Mock() operation.get_device_resolution = Mock(return_value=(1080, 2400)) operation.analyse_pic_word = Mock(return_value="测试文本") return operation @pytest.fixture def sample_image() -> Image.Image: """创建示例图像(1080x2400 RGB)""" # 创建一个简单的测试图像 img = Image.new("RGB", (1080, 2400), color=(255, 255, 255)) return img @pytest.fixture def sample_fudai_image() -> Image.Image: """创建包含福袋图标的示例图像""" img = Image.new("RGB", (1080, 2400), color=(255, 255, 255)) # 在左上角区域添加红色区域(模拟福袋图标) pixels = img.load() for x in range(150, 200): for y in range(400, 450): pixels[x, y] = (255, 0, 0) # 红色 return img @pytest.fixture def sample_screenshot_path(temp_dir: Path, sample_image: Image.Image) -> Path: """创建示例截图文件路径""" screenshot_path = temp_dir / "screenshot.png" sample_image.save(screenshot_path) return screenshot_path @pytest.fixture(autouse=True) def mock_path_config(monkeypatch, temp_dir: Path): """自动Mock path_config模块,使用临时目录""" import path_config def mock_get_screenshot_path(): return str(temp_dir / "screenshot.png") def mock_get_pic_dir(): return str(temp_dir / "pic") monkeypatch.setattr(path_config, "get_screenshot_path", mock_get_screenshot_path) monkeypatch.setattr(path_config, "get_pic_dir", mock_get_pic_dir) @pytest.fixture def disable_logging(monkeypatch): """禁用日志输出(用于测试)""" import logging logging.disable(logging.CRITICAL) @pytest.fixture def mock_time(monkeypatch): """Mock time模块,用于时间相关的测试""" import time current_time = [1000.0] # 使用列表以便修改 def mock_time_func(): return current_time[0] def mock_sleep(seconds): current_time[0] += seconds monkeypatch.setattr(time, "time", mock_time_func) monkeypatch.setattr(time, "sleep", mock_sleep) return current_time # 标记:需要真实设备的测试 pytest_plugins = []