""" Pytest 配置文件 提供全局的 fixture 和测试工具 """ import os import sys import pytest from pathlib import Path import tempfile import shutil # 添加项目根目录到 Python 路径 project_root = Path(__file__).parent.parent.absolute() sys.path.insert(0, str(project_root)) os.chdir(project_root) from main import create_app from app.settings import config @pytest.fixture(scope='session') def app(): """创建 Flask 应用实例""" app = create_app() app.config['TESTING'] = True app.config['OUTPUT_DIR'] = tempfile.mkdtemp() app.config['INPUT_DIR'] = tempfile.mkdtemp() yield app @pytest.fixture(scope='function') def client(app): """创建测试客户端""" with app.test_client() as client: yield client @pytest.fixture(scope='session') def temp_dirs(): """创建临时目录用于测试""" temp_dir = tempfile.mkdtemp() dirs = { 'root': Path(temp_dir), 'input': Path(temp_dir) / 'input', 'output': Path(temp_dir) / 'output', 'temp': Path(temp_dir) / 'temp' } for d in dirs.values(): d.mkdir(parents=True, exist_ok=True) yield dirs shutil.rmtree(temp_dir) @pytest.fixture(scope='function') def clean_temp(temp_dirs): """清理临时目录""" yield temp_dirs for d in temp_dirs.values(): if d.exists(): for f in d.glob('*'): if f.is_file(): f.unlink() @pytest.fixture def sample_audio_file(temp_dirs): """创建示例音频文件路径(不实际创建文件)""" audio_path = temp_dirs['input'] / 'test_audio.wav' yield audio_path @pytest.fixture def sample_video_file(temp_dirs): """创建示例视频文件路径(不实际创建文件)""" video_path = temp_dirs['input'] / 'test_video.mp4' yield video_path