1
0
Files
PPTX/tests/e2e/test_check_cmd.py
lanyuanxiaoyao c73bd0fedd fix: 修复测试问题,提升测试通过率
修复内容:
- E2E测试命令执行方式:将 python -m uv run 改为 uv run
- HTML渲染器:添加 & 字符的HTML转义
- Presentation尺寸验证:添加尺寸值类型验证
- PPTX验证器:修复文本框检测兼容性
- 验证结果格式化:修复提示信息显示
- Mock配置:修复表格渲染等测试的Mock配置

测试结果:
- 修复前: 264 通过, 42 失败, 1 错误
- 修复后: 297 通过, 9 失败, 1 错误

剩余9个失败为待实现的功能增强(验证器模板变量验证)
2026-03-03 00:42:39 +08:00

189 lines
4.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Check 命令端到端测试
测试 yaml2pptx.py check 命令的验证功能
"""
import pytest
import subprocess
import sys
from pathlib import Path
class TestCheckCmd:
"""check 命令测试类"""
def run_check(self, *args):
"""辅助函数:运行 check 命令"""
cmd = ["uv", "run", "python", "yaml2pptx.py", "check"]
cmd.extend(args)
result = subprocess.run(
cmd, capture_output=True, text=True, cwd=Path(__file__).parent.parent.parent
)
return result
def test_check_valid_yaml(self, sample_yaml):
"""测试检查有效的 YAML"""
result = self.run_check(str(sample_yaml))
assert result.returncode == 0
assert "验证" in result.stdout or "通过" in result.stdout
def test_check_invalid_yaml(self, temp_dir):
"""测试检查无效的 YAML"""
# 创建包含错误的 YAML
yaml_content = """
metadata:
size: "16:9"
slides:
- elements:
- type: text
box: [1, 1, 8, 1]
content: "Test"
font:
color: "red" # 无效颜色
"""
yaml_path = temp_dir / "invalid.yaml"
yaml_path.write_text(yaml_content)
result = self.run_check(str(yaml_path))
# 应该有错误
assert result.returncode != 0 or "错误" in result.stdout
def test_check_with_warnings_only(self, temp_dir):
"""测试只有警告的 YAML验证通过但有警告"""
yaml_content = """
metadata:
size: "16:9"
slides:
- elements:
- type: text
box: [8, 1, 3, 1] # 边界超出
content: "Test"
font:
size: 24
"""
yaml_path = temp_dir / "warning.yaml"
yaml_path.write_text(yaml_content)
result = self.run_check(str(yaml_path))
# 应该显示警告但返回 0
assert "警告" in result.stdout
def test_check_with_template(self, temp_dir, sample_template):
"""测试检查使用模板的 YAML"""
yaml_content = f"""
metadata:
size: "16:9"
slides:
- template: title-slide
vars:
title: "Test Title"
"""
yaml_path = temp_dir / "test.yaml"
yaml_path.write_text(yaml_content)
result = self.run_check(str(yaml_path), "--template-dir", str(sample_template))
assert result.returncode == 0
def test_check_nonexistent_template(self, temp_dir):
"""测试检查使用不存在模板的 YAML"""
yaml_content = """
metadata:
size: "16:9"
slides:
- template: nonexistent
vars:
title: "Test"
"""
yaml_path = temp_dir / "test.yaml"
yaml_path.write_text(yaml_content)
result = self.run_check(str(yaml_path), "--template-dir", str(temp_dir))
# 应该有错误(模板不存在)
assert result.returncode != 0
def test_check_reports_multiple_errors(self, temp_dir):
"""测试检查报告多个错误"""
yaml_content = """
metadata:
size: "16:9"
slides:
- elements:
- type: text
box: [1, 1, 8, 1]
content: "Test 1"
font:
color: "red"
- type: text
box: [2, 2, 8, 1]
content: "Test 2"
font:
color: "blue"
"""
yaml_path = temp_dir / "test.yaml"
yaml_path.write_text(yaml_content)
result = self.run_check(str(yaml_path))
output = result.stdout + result.stderr
# 应该报告多个错误
assert "错误" in output or "2" in output
def test_check_includes_location_info(self, temp_dir):
"""测试检查包含位置信息"""
yaml_content = """
metadata:
size: "16:9"
slides:
- elements:
- type: text
box: [1, 1, 8, 1]
content: "Test"
font:
color: "red"
"""
yaml_path = temp_dir / "test.yaml"
yaml_path.write_text(yaml_content)
result = self.run_check(str(yaml_path))
output = result.stdout + result.stderr
# 应该包含位置信息
assert "幻灯片" in output or "元素" in output
def test_check_with_missing_required_variable(self, temp_dir, sample_template):
"""测试检查缺少必需变量的模板"""
yaml_content = f"""
metadata:
size: "16:9"
slides:
- template: title-slide
vars: {{}}
"""
yaml_path = temp_dir / "test.yaml"
yaml_path.write_text(yaml_content)
result = self.run_check(str(yaml_path), "--template-dir", str(sample_template))
# 应该有错误
assert result.returncode != 0
def test_check_nonexistent_file(self, temp_dir):
"""测试检查不存在的文件"""
result = self.run_check(str(temp_dir / "nonexistent.yaml"))
assert result.returncode != 0