1
0

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:
2026-03-02 23:11:34 +08:00
parent 027a832c9a
commit ab2510a400
56 changed files with 7035 additions and 6 deletions

View File

@@ -0,0 +1,56 @@
## Why
yaml2pptx 项目目前没有任何测试代码,存在以下风险:
1. 代码重构和功能迭代时缺乏安全网,容易引入回归问题
2. 验证器、渲染器等核心模块的逻辑复杂,缺少自动化验证
3. 新功能开发时无法快速验证正确性
4. 无法保证 PPTX 生成质量的一致性
现在项目架构已稳定,核心功能基本完成,是建立测试体系的最佳时机。
## What Changes
- 新增完整的测试目录结构 `tests/`,包含单元测试、集成测试、端到端测试
- 新增 `pyproject.toml` 测试依赖配置pytest、pytest-cov 等)
- 新增 `tests/conftest.py` 配置共享 fixtures
- 新增 `tests/conftest_pptx.py` PPTX 文件验证辅助工具
- 新增 `tests/fixtures/` 测试数据目录YAML 样本、模板、图片)
- 实现约 150+ 测试用例覆盖所有核心模块
- 测试将实际生成 PPTX 文件并进行 Level 2 验证(文件结构、元素数量、内容匹配、位置范围)
- Preview 测试采用方案 A不启动真实服务器测试 HTML 生成函数和路由配置)
## Capabilities
### New Capabilities
- `test-framework`: pytest 测试框架基础设施包含配置、fixtures、临时文件管理
- `unit-tests`: 单元测试覆盖各模块独立功能elements、template、validators、loaders、utils
- `integration-tests`: 集成测试覆盖模块间协作presentation、渲染流程、验证流程
- `e2e-tests`: 端到端测试,覆盖 CLI 命令convert、check、preview
- `pptx-validation`: PPTX 文件验证工具,支持文件级别、幻灯片级别、元素级别的验证
### Modified Capabilities
无。测试是新增能力,不修改现有功能的行为要求。
## Impact
### 代码影响
- 新增 `tests/` 目录及所有测试文件
- 修改 `pyproject.toml` 添加测试依赖
- 可能需要调整部分代码以提升可测试性(如全局变量处理)
### 依赖增加
- `pytest`: 测试框架
- `pytest-cov`: 覆盖率报告(可选)
- `pytest-mock`: Mock 工具(可选)
- `pillow`: 用于创建测试图片
### 开发流程
- 开发者可通过 `uv run pytest` 运行测试
- 提交代码前应确保测试通过
- 新功能开发应同步编写测试
### 文档更新
- `README.md` 添加测试运行说明
- `README_DEV.md` 添加测试开发指南