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

@@ -471,8 +471,126 @@ def main():
## 测试规范
### 测试框架
项目使用 pytest 作为测试框架,测试代码位于 `tests/` 目录。
### 测试结构
```
tests/
├── conftest.py # pytest 配置和共享 fixtures
├── conftest_pptx.py # PPTX 文件验证工具
├── unit/ # 单元测试
│ ├── test_elements.py # 元素类测试
│ ├── test_template.py # 模板系统测试
│ ├── test_utils.py # 工具函数测试
│ ├── test_validators/ # 验证器测试
│ │ ├── test_geometry.py
│ │ ├── test_resource.py
│ │ ├── test_result.py
│ │ └── test_validator.py
│ └── test_loaders/ # 加载器测试
│ └── test_yaml_loader.py
├── integration/ # 集成测试
│ ├── test_presentation.py
│ ├── test_rendering_flow.py
│ └── test_validation_flow.py
├── e2e/ # 端到端测试
│ ├── test_convert_cmd.py
│ ├── test_check_cmd.py
│ └── test_preview_cmd.py
└── fixtures/ # 测试数据
├── yaml_samples/ # YAML 样本
├── templates/ # 测试模板
└── images/ # 测试图片
```
### 运行测试
```bash
# 安装测试依赖
uv pip install -e ".[dev]"
# 运行所有测试
uv run pytest
# 运行特定类型的测试
uv run pytest tests/unit/ # 单元测试
uv run pytest tests/integration/ # 集成测试
uv run pytest tests/e2e/ # 端到端测试
# 运行特定测试文件
uv run pytest tests/unit/test_elements.py
# 显示详细输出
uv run pytest -v
# 显示测试覆盖率
uv run pytest --cov=. --cov-report=html
```
### 编写测试
**测试类命名**:使用 `Test<ClassName>` 格式
**测试方法命名**:使用 `test_<what_is_being_tested>` 格式
```python
class TestTextElement:
"""TextElement 测试类"""
def test_create_with_defaults(self):
"""测试使用默认值创建 TextElement"""
elem = TextElement()
assert elem.type == 'text'
def test_invalid_color_raises_error(self):
"""测试无效颜色会引发错误"""
with pytest.raises(ValueError, match="无效颜色"):
TextElement(font={"color": "red"})
```
### Fixtures
共享 fixtures 定义在 `tests/conftest.py` 中:
- `temp_dir`: 临时目录
- `sample_yaml`: 最小测试 YAML 文件
- `sample_image`: 测试图片
- `sample_template`: 测试模板
- `pptx_validator`: PPTX 验证器
```python
def test_with_fixture(sample_yaml):
"""使用 fixture 的测试"""
assert sample_yaml.exists()
```
### PPTX 验证
使用 `PptxFileValidator` 验证生成的 PPTX 文件:
```python
def test_pptx_generation(temp_dir, pptx_validator):
"""测试 PPTX 生成"""
# ... 生成 PPTX ...
output_path = temp_dir / "output.pptx"
# 验证文件
assert pptx_validator.validate_file(output_path) is True
# 验证内容
prs = Presentation(str(output_path))
assert pptx_validator.validate_text_element(
prs.slides[0],
index=0,
expected_content="Test"
) is True
```
### 手动测试
```bash
# 验证 YAML 文件
uv run yaml2pptx.py check temp/test.yaml
@@ -510,10 +628,11 @@ uv run yaml2pptx.py preview temp/test.yaml --no-browser
### 测试文件位置
所有测试文件必须放在 `temp/` 目录下:
- `temp/*.yaml` - 测试用的 YAML 文件
- `temp/*.pptx` - 生成的 PPTX 文件
- `temp/templates/` - 测试用的模板文件
- **自动化测试**`tests/` 目录
- **手动测试文件**`temp/` 目录
- `temp/*.yaml` - 手动测试用的 YAML 文件
- `temp/*.pptx` - 生成的 PPTX 文件
- `temp/templates/` - 手动测试用的模板文件
## 常见问题