test: add comprehensive pytest test suite
Add complete test infrastructure for yaml2pptx project with 245+ tests covering unit, integration, and end-to-end scenarios. Test structure: - Unit tests: elements, template system, validators, loaders, utils - Integration tests: presentation and rendering flows - E2E tests: CLI commands (convert, check, preview) Key features: - PptxFileValidator for Level 2 PPTX validation (file structure, element count, content matching, position tolerance) - Comprehensive fixtures for test data consistency - Mock-based testing for external dependencies - Test images generated with PIL/Pillow - Boundary case coverage for edge scenarios Dependencies added: - pytest, pytest-cov, pytest-mock - pillow (for test image generation) Documentation updated: - README.md: test running instructions - README_DEV.md: test development guide Co-authored-by: OpenSpec change: add-comprehensive-tests
This commit is contained in:
3
tests/e2e/__init__.py
Normal file
3
tests/e2e/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
"""
|
||||
端到端测试包
|
||||
"""
|
||||
191
tests/e2e/test_check_cmd.py
Normal file
191
tests/e2e/test_check_cmd.py
Normal file
@@ -0,0 +1,191 @@
|
||||
"""
|
||||
Check 命令端到端测试
|
||||
|
||||
测试 yaml2pptx.py check 命令的验证功能
|
||||
"""
|
||||
|
||||
import pytest
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class TestCheckCmd:
|
||||
"""check 命令测试类"""
|
||||
|
||||
def run_check(self, *args):
|
||||
"""辅助函数:运行 check 命令"""
|
||||
cmd = [sys.executable, "-m", "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
|
||||
205
tests/e2e/test_convert_cmd.py
Normal file
205
tests/e2e/test_convert_cmd.py
Normal file
@@ -0,0 +1,205 @@
|
||||
"""
|
||||
Convert 命令端到端测试
|
||||
|
||||
测试 yaml2pptx.py convert 命令的完整功能
|
||||
"""
|
||||
|
||||
import pytest
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from pptx import Presentation
|
||||
|
||||
|
||||
class TestConvertCmd:
|
||||
"""convert 命令测试类"""
|
||||
|
||||
def run_convert(self, *args):
|
||||
"""辅助函数:运行 convert 命令"""
|
||||
cmd = [sys.executable, "-m", "uv", "run", "python", "yaml2pptx.py", "convert"]
|
||||
cmd.extend(args)
|
||||
result = subprocess.run(
|
||||
cmd,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
cwd=Path(__file__).parent.parent.parent
|
||||
)
|
||||
return result
|
||||
|
||||
def test_basic_conversion(self, sample_yaml, temp_dir):
|
||||
"""测试基本转换"""
|
||||
output = temp_dir / "output.pptx"
|
||||
result = self.run_convert(str(sample_yaml), str(output))
|
||||
|
||||
assert result.returncode == 0
|
||||
assert output.exists()
|
||||
assert output.stat().st_size > 0
|
||||
|
||||
def test_auto_output_filename(self, sample_yaml, temp_dir):
|
||||
"""测试自动生成输出文件名"""
|
||||
# 在 temp_dir 中运行
|
||||
result = subprocess.run(
|
||||
[sys.executable, "-m", "uv", "run", "python",
|
||||
"yaml2pptx.py", "convert", str(sample_yaml)],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
cwd=temp_dir
|
||||
)
|
||||
|
||||
assert result.returncode == 0
|
||||
# 应该生成与输入同名的 .pptx 文件
|
||||
expected_output = temp_dir / "test.pptx"
|
||||
assert expected_output.exists()
|
||||
|
||||
def test_conversion_with_template(self, temp_dir, sample_template):
|
||||
"""测试使用模板转换"""
|
||||
yaml_content = f"""
|
||||
metadata:
|
||||
size: 16:9
|
||||
|
||||
slides:
|
||||
- template: title-slide
|
||||
vars:
|
||||
title: "Template Test"
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
output = temp_dir / "output.pptx"
|
||||
result = self.run_convert(
|
||||
str(yaml_path),
|
||||
str(output),
|
||||
"--template-dir", str(sample_template)
|
||||
)
|
||||
|
||||
assert result.returncode == 0
|
||||
assert output.exists()
|
||||
|
||||
def test_skip_validation(self, sample_yaml, temp_dir):
|
||||
"""测试跳过验证"""
|
||||
output = temp_dir / "output.pptx"
|
||||
result = self.run_convert(
|
||||
str(sample_yaml),
|
||||
str(output),
|
||||
"--skip-validation"
|
||||
)
|
||||
|
||||
assert result.returncode == 0
|
||||
assert output.exists()
|
||||
|
||||
def test_force_overwrite(self, sample_yaml, temp_dir):
|
||||
"""测试强制覆盖"""
|
||||
output = temp_dir / "output.pptx"
|
||||
|
||||
# 先创建一个存在的文件
|
||||
output.write_text("existing")
|
||||
|
||||
# 使用 --force 应该覆盖
|
||||
result = self.run_convert(
|
||||
str(sample_yaml),
|
||||
str(output),
|
||||
"--force"
|
||||
)
|
||||
|
||||
assert result.returncode == 0
|
||||
# 文件应该是有效的 PPTX,不是原来的文本
|
||||
assert output.stat().st_size > 1000
|
||||
|
||||
def test_existing_file_without_force(self, sample_yaml, temp_dir):
|
||||
"""测试文件已存在且不使用 --force"""
|
||||
output = temp_dir / "output.pptx"
|
||||
|
||||
# 先创建一个存在的文件
|
||||
output.write_text("existing")
|
||||
|
||||
# 不使用 --force 应该失败或提示
|
||||
result = self.run_convert(str(sample_yaml), str(output))
|
||||
|
||||
# 程序应该拒绝覆盖
|
||||
assert result.returncode != 0 or "已存在" in result.stderr
|
||||
|
||||
def test_invalid_input_file(self, temp_dir):
|
||||
"""测试无效输入文件"""
|
||||
nonexistent = temp_dir / "nonexistent.yaml"
|
||||
output = temp_dir / "output.pptx"
|
||||
|
||||
result = self.run_convert(str(nonexistent), str(output))
|
||||
|
||||
assert result.returncode != 0
|
||||
|
||||
def test_conversion_with_all_element_types(self, temp_dir, sample_image):
|
||||
"""测试转换包含所有元素类型的 YAML"""
|
||||
fixtures_yaml = Path(__file__).parent.parent / "fixtures" / "yaml_samples" / "full_features.yaml"
|
||||
if not fixtures_yaml.exists():
|
||||
pytest.skip("full_features.yaml not found")
|
||||
|
||||
output = temp_dir / "output.pptx"
|
||||
result = self.run_convert(str(fixtures_yaml), str(output))
|
||||
|
||||
assert result.returncode == 0
|
||||
assert output.exists()
|
||||
|
||||
# 验证生成的 PPTX
|
||||
prs = Presentation(str(output))
|
||||
assert len(prs.slides) >= 1
|
||||
|
||||
def test_conversion_preserves_chinese_content(self, temp_dir):
|
||||
"""测试转换保留中文内容"""
|
||||
yaml_content = """
|
||||
metadata:
|
||||
size: 16:9
|
||||
|
||||
slides:
|
||||
- elements:
|
||||
- type: text
|
||||
box: [1, 1, 8, 1]
|
||||
content: "测试中文内容"
|
||||
font:
|
||||
size: 24
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content, encoding='utf-8')
|
||||
|
||||
output = temp_dir / "output.pptx"
|
||||
result = self.run_convert(str(yaml_path), str(output))
|
||||
|
||||
assert result.returncode == 0
|
||||
assert output.exists()
|
||||
|
||||
# 验证内容
|
||||
prs = Presentation(str(output))
|
||||
text_content = prs.slides[0].shapes[0].text_frame.text
|
||||
assert "测试中文内容" in text_content
|
||||
|
||||
def test_different_slide_sizes(self, temp_dir):
|
||||
"""测试不同的幻灯片尺寸"""
|
||||
for size in ["16:9", "4:3"]:
|
||||
yaml_content = f"""
|
||||
metadata:
|
||||
size: {size}
|
||||
|
||||
slides:
|
||||
- elements:
|
||||
- type: text
|
||||
box: [1, 1, 8, 1]
|
||||
content: "Size {size}"
|
||||
font:
|
||||
size: 24
|
||||
"""
|
||||
yaml_path = temp_dir / f"test_{size.replace(':', '')}.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
output = temp_dir / f"output_{size.replace(':', '')}.pptx"
|
||||
result = self.run_convert(str(yaml_path), str(output))
|
||||
|
||||
assert result.returncode == 0, f"Failed for size {size}"
|
||||
assert output.exists()
|
||||
|
||||
# 验证尺寸
|
||||
prs = Presentation(str(output))
|
||||
if size == "16:9":
|
||||
assert abs(prs.slide_width.inches - 10.0) < 0.01
|
||||
assert abs(prs.slide_height.inches - 5.625) < 0.01
|
||||
else: # 4:3
|
||||
assert abs(prs.slide_width.inches - 10.0) < 0.01
|
||||
assert abs(prs.slide_height.inches - 7.5) < 0.01
|
||||
201
tests/e2e/test_preview_cmd.py
Normal file
201
tests/e2e/test_preview_cmd.py
Normal file
@@ -0,0 +1,201 @@
|
||||
"""
|
||||
Preview 命令端到端测试
|
||||
|
||||
测试 yaml2pptx.py preview 命令的 HTML 生成功能(不启动真实服务器)
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch, MagicMock
|
||||
from preview.server import generate_preview_html, create_flask_app
|
||||
|
||||
|
||||
class TestGeneratePreviewHtml:
|
||||
"""generate_preview_html 函数测试类"""
|
||||
|
||||
def test_generate_html_from_valid_yaml(self, sample_yaml):
|
||||
"""测试从有效 YAML 生成 HTML"""
|
||||
html = generate_preview_html(str(sample_yaml), None)
|
||||
|
||||
assert isinstance(html, str)
|
||||
assert "<!DOCTYPE html>" in html
|
||||
assert "<html>" in html
|
||||
assert "</html>" in html
|
||||
|
||||
def test_html_contains_slide_content(self, sample_yaml):
|
||||
"""测试 HTML 包含幻灯片内容"""
|
||||
html = generate_preview_html(str(sample_yaml), None)
|
||||
|
||||
# 应该包含文本内容
|
||||
assert "Hello, World!" in html
|
||||
|
||||
def test_html_contains_css_styles(self, sample_yaml):
|
||||
"""测试 HTML 包含 CSS 样式"""
|
||||
html = generate_preview_html(str(sample_yaml), None)
|
||||
|
||||
assert "<style>" in html
|
||||
assert ".slide" in html
|
||||
assert "position: absolute" in html
|
||||
|
||||
def test_html_with_template(self, temp_dir, sample_template):
|
||||
"""测试使用模板生成 HTML"""
|
||||
yaml_content = f"""
|
||||
metadata:
|
||||
size: 16:9
|
||||
|
||||
slides:
|
||||
- template: title-slide
|
||||
vars:
|
||||
title: "Template Title"
|
||||
subtitle: "Template Subtitle"
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
html = generate_preview_html(str(yaml_path), str(sample_template))
|
||||
|
||||
assert "Template Title" in html
|
||||
|
||||
def test_html_with_invalid_yaml(self, temp_dir):
|
||||
"""测试无效 YAML 返回错误页面"""
|
||||
yaml_path = temp_dir / "invalid.yaml"
|
||||
yaml_path.write_text("invalid: [unclosed")
|
||||
|
||||
html = generate_preview_html(str(yaml_path), None)
|
||||
|
||||
# 应该返回错误页面
|
||||
assert "<!DOCTYPE html>" in html
|
||||
assert "错误" in html or "error" in html.lower()
|
||||
|
||||
def test_html_with_multiple_slides(self, temp_dir):
|
||||
"""测试多张幻灯片的 HTML"""
|
||||
yaml_content = """
|
||||
metadata:
|
||||
size: 16:9
|
||||
|
||||
slides:
|
||||
- elements:
|
||||
- type: text
|
||||
box: [1, 1, 8, 1]
|
||||
content: "Slide 1"
|
||||
font:
|
||||
size: 24
|
||||
|
||||
- elements:
|
||||
- type: text
|
||||
box: [1, 1, 8, 1]
|
||||
content: "Slide 2"
|
||||
font:
|
||||
size: 24
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
html = generate_preview_html(str(yaml_path), None)
|
||||
|
||||
# 应该包含两张幻灯片的内容
|
||||
assert "Slide 1" in html
|
||||
assert "Slide 2" in html
|
||||
|
||||
def test_html_contains_slide_number(self, sample_yaml):
|
||||
"""测试 HTML 包含幻灯片编号"""
|
||||
html = generate_preview_html(str(sample_yaml), None)
|
||||
|
||||
assert "slide-number" in html
|
||||
|
||||
def test_html_contains_sse_script(self, sample_yaml):
|
||||
"""测试 HTML 包含 SSE 事件脚本"""
|
||||
html = generate_preview_html(str(sample_yaml), None)
|
||||
|
||||
assert "EventSource" in html
|
||||
assert "/events" in html
|
||||
|
||||
|
||||
class TestCreateFlaskApp:
|
||||
"""create_flask_app 函数测试类"""
|
||||
|
||||
@patch('preview.server.current_yaml_file', 'test.yaml')
|
||||
@patch('preview.server.current_template_dir', None)
|
||||
@patch('preview.server.change_queue')
|
||||
def test_creates_flask_app(self, mock_queue):
|
||||
"""测试创建 Flask 应用"""
|
||||
app = create_flask_app()
|
||||
|
||||
assert app is not None
|
||||
assert hasattr(app, 'url_map')
|
||||
|
||||
@patch('preview.server.current_yaml_file', 'test.yaml')
|
||||
@patch('preview.server.current_template_dir', None)
|
||||
@patch('preview.server.change_queue')
|
||||
def test_has_index_route(self, mock_queue):
|
||||
"""测试有 / 路由"""
|
||||
app = create_flask_app()
|
||||
|
||||
# 检查路由
|
||||
rules = [rule.rule for rule in app.url_map.iter_rules()]
|
||||
assert '/' in rules
|
||||
|
||||
@patch('preview.server.current_yaml_file', 'test.yaml')
|
||||
@patch('preview.server.current_template_dir', None)
|
||||
@patch('preview.server.change_queue')
|
||||
def test_has_events_route(self, mock_queue):
|
||||
"""测试有 /events 路由"""
|
||||
app = create_flask_app()
|
||||
|
||||
rules = [rule.rule for rule in app.url_map.iter_rules()]
|
||||
assert '/events' in rules
|
||||
|
||||
|
||||
class TestYAMLChangeHandler:
|
||||
"""YAMLChangeHandler 测试类"""
|
||||
|
||||
def test_on_modified_with_yaml_file(self):
|
||||
"""测试处理 YAML 文件修改"""
|
||||
from preview.server import YAMLChangeHandler
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
handler = YAMLChangeHandler()
|
||||
mock_queue = MagicMock()
|
||||
import preview.server
|
||||
preview.server.change_queue = mock_queue
|
||||
|
||||
event = MagicMock()
|
||||
event.src_path = "test.yaml"
|
||||
|
||||
handler.on_modified(event)
|
||||
|
||||
mock_queue.put.assert_called_once_with('reload')
|
||||
|
||||
def test_on_modified_with_non_yaml_file(self):
|
||||
"""测试忽略非 YAML 文件修改"""
|
||||
from preview.server import YAMLChangeHandler
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
handler = YAMLChangeHandler()
|
||||
mock_queue = MagicMock()
|
||||
import preview.server
|
||||
preview.server.change_queue = mock_queue
|
||||
|
||||
event = MagicMock()
|
||||
event.src_path = "test.txt"
|
||||
|
||||
handler.on_modified(event)
|
||||
|
||||
mock_queue.put.assert_not_called()
|
||||
|
||||
|
||||
class TestPreviewHTMLTemplate:
|
||||
"""HTML 模板常量测试"""
|
||||
|
||||
def test_html_template_is_defined(self):
|
||||
"""测试 HTML_TEMPLATE 已定义"""
|
||||
from preview.server import HTML_TEMPLATE
|
||||
assert isinstance(HTML_TEMPLATE, str)
|
||||
assert "<!DOCTYPE html>" in HTML_TEMPLATE
|
||||
|
||||
def test_error_template_is_defined(self):
|
||||
"""测试 ERROR_TEMPLATE 已定义"""
|
||||
from preview.server import ERROR_TEMPLATE
|
||||
assert isinstance(ERROR_TEMPLATE, str)
|
||||
assert "<!DOCTYPE html>" in ERROR_TEMPLATE
|
||||
assert "错误" in ERROR_TEMPLATE or "error" in ERROR_TEMPLATE.lower()
|
||||
Reference in New Issue
Block a user