"""pytest配置文件""" import pytest import os import tempfile import shutil from pathlib import Path from typing import Dict, Any from entity.render_task import RenderTask from config.settings import FFmpegConfig, APIConfig, StorageConfig @pytest.fixture def temp_dir(): """创建临时目录""" temp_path = tempfile.mkdtemp() yield temp_path shutil.rmtree(temp_path, ignore_errors=True) @pytest.fixture def test_ffmpeg_config(): """测试用FFmpeg配置""" return FFmpegConfig( encoder_args="-c:v h264", video_args="", ) @pytest.fixture def test_api_config(): """测试用API配置""" return APIConfig( endpoint="http://test.local", access_key="test_key", ) @pytest.fixture def test_storage_config(): """测试用存储配置""" return StorageConfig( template_dir="tests/test_data/templates", ) @pytest.fixture def sample_render_task(): """示例渲染任务""" return RenderTask( task_id="test_task_001", template_id="test_template", output_path="test_output.mp4", frame_rate=25, effects=["zoom:0,2.0,3.0", "ospeed:1.5"], ext_data={ "posJson": '{"ltX": 100, "ltY": 100, "rbX": 200, "rbY": 200, "imgWidth": 300, "imgHeight": 300}' }, ) @pytest.fixture def sample_video_file(temp_dir): """创建测试用的样本视频文件路径""" video_path = os.path.join(temp_dir, "sample.mp4") # 这里只返回路径,实际测试中可能需要真实视频文件 return video_path @pytest.fixture(scope="session") def ffmpeg_available(): """检查FFmpeg是否可用""" import subprocess try: subprocess.run(["ffmpeg", "-version"], capture_output=True, check=True) return True except (subprocess.CalledProcessError, FileNotFoundError): return False @pytest.fixture def mock_ext_data(): """模拟扩展数据""" return { "posJson": '{"ltX": 50, "ltY": 50, "rbX": 150, "rbY": 150, "imgWidth": 200, "imgHeight": 200}', "templateData": {"width": 1920, "height": 1080}, } # 标记:只在FFmpeg可用时运行集成测试 def pytest_collection_modifyitems(config, items): """根据FFmpeg可用性修改测试收集""" try: import subprocess subprocess.run(["ffmpeg", "-version"], capture_output=True, check=True) ffmpeg_available = True except (subprocess.CalledProcessError, FileNotFoundError): ffmpeg_available = False if not ffmpeg_available: skip_ffmpeg = pytest.mark.skip(reason="FFmpeg not available") for item in items: if "integration" in item.keywords: item.add_marker(skip_ffmpeg)