This commit is contained in:
2025-09-24 10:50:34 +08:00
parent c055a68592
commit ec1705769c
18 changed files with 348 additions and 330 deletions

View File

@@ -1,4 +1,5 @@
"""FFmpeg执行集成测试"""
import pytest
import subprocess
import tempfile
@@ -7,7 +8,7 @@ from pathlib import Path
from entity.ffmpeg_command_builder import FFmpegCommandBuilder
from entity.render_task import RenderTask
from config.settings import Config
from config.settings import FFmpegConfig
from services.render_service import DefaultRenderService
from tests.utils.test_helpers import MockRenderTask, create_test_video_file
@@ -16,17 +17,6 @@ from tests.utils.test_helpers import MockRenderTask, create_test_video_file
class TestFFmpegExecution:
"""FFmpeg执行集成测试"""
@pytest.fixture
def test_config(self, temp_dir):
"""测试配置"""
return Config(
encoder_args="-c:v libx264",
video_args="-preset ultrafast -crf 23",
template_dir=temp_dir,
api_endpoint="http://test.local",
access_key="test_key"
)
@pytest.fixture
def sample_video(self, temp_dir):
"""创建测试视频文件"""
@@ -37,11 +27,11 @@ class TestFFmpegExecution:
return video_path
@pytest.fixture
def render_service(self, test_config):
def render_service(self):
"""渲染服务实例"""
return DefaultRenderService()
def test_simple_copy_execution(self, sample_video, temp_dir, test_config):
def test_simple_copy_execution(self, sample_video, temp_dir):
"""测试简单复制执行"""
output_path = os.path.join(temp_dir, "copy_output.mp4")
@@ -50,7 +40,7 @@ class TestFFmpegExecution:
task.output_path = output_path
task.effects = []
builder = FFmpegCommandBuilder(task, test_config)
builder = FFmpegCommandBuilder(task)
command = builder.build_command()
# 执行命令
@@ -62,7 +52,7 @@ class TestFFmpegExecution:
except subprocess.TimeoutExpired:
pytest.fail("FFmpeg execution timed out")
def test_zoom_effect_execution(self, sample_video, temp_dir, test_config):
def test_zoom_effect_execution(self, sample_video, temp_dir):
"""测试缩放特效执行"""
output_path = os.path.join(temp_dir, "zoom_output.mp4")
@@ -72,7 +62,7 @@ class TestFFmpegExecution:
task.effects = ["zoom:0,2.0,2.0"] # 2倍缩放,持续2秒
task.ext_data = {"posJson": "{}"}
builder = FFmpegCommandBuilder(task, test_config)
builder = FFmpegCommandBuilder(task)
command = builder.build_command()
try:
@@ -83,7 +73,7 @@ class TestFFmpegExecution:
except subprocess.TimeoutExpired:
pytest.fail("FFmpeg execution timed out")
def test_speed_effect_execution(self, sample_video, temp_dir, test_config):
def test_speed_effect_execution(self, sample_video, temp_dir):
"""测试变速特效执行"""
output_path = os.path.join(temp_dir, "speed_output.mp4")
@@ -93,7 +83,7 @@ class TestFFmpegExecution:
task.effects = ["ospeed:2.0"] # 2倍速
task.ext_data = {}
builder = FFmpegCommandBuilder(task, test_config)
builder = FFmpegCommandBuilder(task)
command = builder.build_command()
try:
@@ -104,7 +94,7 @@ class TestFFmpegExecution:
except subprocess.TimeoutExpired:
pytest.fail("FFmpeg execution timed out")
def test_multiple_effects_execution(self, sample_video, temp_dir, test_config):
def test_multiple_effects_execution(self, sample_video, temp_dir):
"""测试多特效组合执行"""
output_path = os.path.join(temp_dir, "multi_effects_output.mp4")
@@ -114,7 +104,7 @@ class TestFFmpegExecution:
task.effects = ["zoom:0,1.5,1.0", "ospeed:1.5"] # 缩放+变速
task.ext_data = {"posJson": "{}"}
builder = FFmpegCommandBuilder(task, test_config)
builder = FFmpegCommandBuilder(task)
command = builder.build_command()
try:
@@ -125,7 +115,7 @@ class TestFFmpegExecution:
except subprocess.TimeoutExpired:
pytest.fail("FFmpeg execution timed out")
def test_concat_execution(self, temp_dir, test_config):
def test_concat_execution(self, temp_dir):
"""测试视频拼接执行"""
# 创建两个测试视频
video1_path = os.path.join(temp_dir, "video1.mp4")
@@ -143,7 +133,7 @@ class TestFFmpegExecution:
task.output_path = output_path
task.effects = []
builder = FFmpegCommandBuilder(task, test_config)
builder = FFmpegCommandBuilder(task)
command = builder.build_command()
try:
@@ -154,7 +144,7 @@ class TestFFmpegExecution:
except subprocess.TimeoutExpired:
pytest.fail("FFmpeg execution timed out")
def test_invalid_effect_execution(self, sample_video, temp_dir, test_config):
def test_invalid_effect_execution(self, sample_video, temp_dir):
"""测试无效特效的执行处理"""
output_path = os.path.join(temp_dir, "invalid_effect_output.mp4")
@@ -164,7 +154,7 @@ class TestFFmpegExecution:
task.effects = ["invalid_effect:params", "zoom:0,2.0,1.0"] # 混合有效和无效特效
task.ext_data = {"posJson": "{}"}
builder = FFmpegCommandBuilder(task, test_config)
builder = FFmpegCommandBuilder(task)
command = builder.build_command()
# 应该忽略无效特效,继续处理有效特效
@@ -175,7 +165,7 @@ class TestFFmpegExecution:
except subprocess.TimeoutExpired:
pytest.fail("FFmpeg execution timed out")
def test_render_service_integration(self, sample_video, temp_dir, test_config, render_service):
def test_render_service_integration(self, sample_video, temp_dir, render_service):
"""测试渲染服务集成"""
output_path = os.path.join(temp_dir, "service_output.mp4")
@@ -187,7 +177,7 @@ class TestFFmpegExecution:
"output_path": output_path,
"effects": ["zoom:0,1.8,2.0"],
"ext_data": {"posJson": "{}"},
"frame_rate": 25
"frame_rate": 25,
}
# 这里需要根据实际的RenderTask构造方法调整
@@ -196,11 +186,11 @@ class TestFFmpegExecution:
# 使用渲染服务执行
try:
# 这里的方法调用需要根据实际的渲染服务接口调整
# success = render_service.render(task, test_config)
# success = render_service.render(task)
# assert success, "Render service failed"
# 临时直接使用FFmpegCommandBuilder测试
builder = FFmpegCommandBuilder(task, test_config)
builder = FFmpegCommandBuilder(task)
command = builder.build_command()
result = subprocess.run(command, capture_output=True, text=True, timeout=60)
@@ -210,7 +200,7 @@ class TestFFmpegExecution:
except Exception as e:
pytest.fail(f"Render service integration failed: {e}")
def test_error_handling_missing_input(self, temp_dir, test_config):
def test_error_handling_missing_input(self, temp_dir):
"""测试缺失输入文件的错误处理"""
missing_file = os.path.join(temp_dir, "nonexistent.mp4")
output_path = os.path.join(temp_dir, "error_output.mp4")
@@ -220,14 +210,14 @@ class TestFFmpegExecution:
task.output_path = output_path
task.effects = []
builder = FFmpegCommandBuilder(task, test_config)
builder = FFmpegCommandBuilder(task)
command = builder.build_command()
# 应该失败,因为输入文件不存在
result = subprocess.run(command, capture_output=True, text=True, timeout=30)
assert result.returncode != 0, "FFmpeg should fail with missing input file"
def test_performance_multiple_effects(self, sample_video, temp_dir, test_config):
def test_performance_multiple_effects(self, sample_video, temp_dir):
"""测试多特效性能"""
output_path = os.path.join(temp_dir, "performance_output.mp4")
@@ -235,21 +225,20 @@ class TestFFmpegExecution:
task.input_files = [sample_video]
task.output_path = output_path
# 多个特效组合
task.effects = [
"zoom:0,1.5,1.0",
"ospeed:1.2",
"zoom:1.5,2.0,1.0"
]
task.effects = ["zoom:0,1.5,1.0", "ospeed:1.2", "zoom:1.5,2.0,1.0"]
task.ext_data = {"posJson": "{}"}
builder = FFmpegCommandBuilder(task, test_config)
builder = FFmpegCommandBuilder(task)
command = builder.build_command()
import time
start_time = time.time()
try:
result = subprocess.run(command, capture_output=True, text=True, timeout=120)
result = subprocess.run(
command, capture_output=True, text=True, timeout=120
)
execution_time = time.time() - start_time
assert result.returncode == 0, f"FFmpeg failed: {result.stderr}"
@@ -259,8 +248,10 @@ class TestFFmpegExecution:
except subprocess.TimeoutExpired:
pytest.fail("FFmpeg execution timed out")
@pytest.mark.skipif(not os.environ.get('RUN_STRESS_TESTS'), reason="Stress tests disabled")
def test_stress_test_large_effects_chain(self, sample_video, temp_dir, test_config):
@pytest.mark.skipif(
not os.environ.get("RUN_STRESS_TESTS"), reason="Stress tests disabled"
)
def test_stress_test_large_effects_chain(self, sample_video, temp_dir):
"""压力测试:大量特效链"""
output_path = os.path.join(temp_dir, "stress_output.mp4")
@@ -271,12 +262,14 @@ class TestFFmpegExecution:
task.effects = [f"zoom:{i*0.5},1.{i+5},{i*0.2+0.5}" for i in range(10)]
task.ext_data = {"posJson": "{}"}
builder = FFmpegCommandBuilder(task, test_config)
builder = FFmpegCommandBuilder(task)
command = builder.build_command()
try:
result = subprocess.run(command, capture_output=True, text=True, timeout=300)
result = subprocess.run(
command, capture_output=True, text=True, timeout=300
)
assert result.returncode == 0, f"Stress test failed: {result.stderr}"
assert os.path.exists(output_path), "Output file was not created"
except subprocess.TimeoutExpired:
pytest.fail("Stress test timed out")
pytest.fail("Stress test timed out")