Files
lyxy-document/openspec/specs/test-fixtures/spec.md
lanyuanxiaoyao 7eab1dcef1 test: 添加全面的测试套件,覆盖所有 Reader 实现
- 测试数量从 83 个增加到 193 个 (+132%)
- 代码覆盖率从 48% 提升到 69% (+44%)
- 为每种文档格式的所有 Reader 实现创建独立测试
- 添加跨 Reader 的一致性验证测试
- 新增 4 个测试规范 (cli-testing, exception-testing, reader-testing, test-fixtures)
- 更新 README 测试统计信息

测试覆盖:
- DOCX: python-docx, markitdown, docling, native-xml, pypandoc, unstructured
- PDF: pypdf, markitdown, docling, docling-ocr, unstructured, unstructured-ocr
- HTML: html2text, markitdown, trafilatura, domscribe
- PPTX: python-pptx, markitdown, docling, native-xml, unstructured
- XLSX: pandas, markitdown, docling, native-xml, unstructured
- CLI: 所有命令行选项和错误处理

所有 193 个测试通过。
2026-03-08 22:20:21 +08:00

109 lines
4.2 KiB
Markdown
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.
# Test Fixtures Specification
## Purpose
定义测试 fixtures 的规范,包括临时文件创建、自动清理、fixture 组织结构等。
## Requirements
### Requirement: 临时文件自动清理
测试使用的临时文件 MUST 在测试完成后自动清理,使用 pytest 的 tmp_path fixture。
#### Scenario: 测试完成后临时文件被删除
- **WHEN** 测试使用 tmp_path 创建临时文件
- **THEN** 测试结束后临时文件自动删除
#### Scenario: 测试失败时可保留文件
- **WHEN** 使用 `--tmp-path-retention-count` 参数运行测试
- **THEN** 失败测试的临时文件被保留用于调试
### Requirement: 临时文件独立创建
每个测试 MUST 独立创建自己的临时文件,不共享文件,保证测试隔离。
#### Scenario: 每个测试独立创建文件
- **WHEN** 多个测试使用相同 fixture
- **THEN** 每个测试获得独立的临时文件实例
#### Scenario: 测试间无文件共享
- **WHEN** 测试 A 创建并修改临时文件
- **THEN** 测试 B 的临时文件不受影响
### Requirement: 全局 conftest fixtures
tests/conftest.py MUST 提供全局可用的 fixtures。
#### Scenario: 提供 all_readers fixture
- **WHEN** 测试需要所有 Reader 实例
- **THEN** 可以使用 `all_readers` fixture 获取完整的 Reader 列表
### Requirement: Reader 专用 fixtures
tests/test_readers/conftest.py MUST 提供 Reader 测试专用的 fixtures。
#### Scenario: 提供 temp_docx fixture
- **WHEN** 测试需要临时 DOCX 文件
- **THEN** 可以使用 `temp_docx` fixture 创建临时 DOCX 文件
- **AND** fixture 接受参数(如 paragraphs、table_data自定义内容
#### Scenario: 提供 temp_pdf fixture
- **WHEN** 测试需要临时 PDF 文件
- **THEN** 可以使用 `temp_pdf` fixture 创建临时 PDF 文件
#### Scenario: 提供 temp_html fixture
- **WHEN** 测试需要临时 HTML 文件
- **THEN** 可以使用 `temp_html` fixture 创建临时 HTML 文件
#### Scenario: 提供 temp_pptx fixture
- **WHEN** 测试需要临时 PPTX 文件
- **THEN** 可以使用 `temp_pptx` fixture 创建临时 PPTX 文件
#### Scenario: 提供 temp_xlsx fixture
- **WHEN** 测试需要临时 XLSX 文件
- **THEN** 可以使用 `temp_xlsx` fixture 创建临时 XLSX 文件
### Requirement: CLI 专用 fixtures
tests/test_cli/conftest.py MUST 提供 CLI 测试专用的 fixtures。
#### Scenario: 提供 cli_runner fixture
- **WHEN** 测试需要运行 CLI
- **THEN** 可以使用 `cli_runner` fixture 调用 main() 函数并捕获输出
- **AND** 返回 (stdout, stderr) 元组
#### Scenario: 提供 temp_test_file fixture
- **WHEN** CLI 测试需要临时测试文件
- **THEN** 可以使用 `temp_test_file` fixture 根据格式类型创建对应文件
### Requirement: Fixture 返回文件路径
所有创建临时文件的 fixtures MUST 返回文件路径字符串,而非 Path 对象或文件对象。
#### Scenario: temp_docx 返回路径字符串
- **WHEN** 调用 `temp_docx(paragraphs=["test"])`
- **THEN** 返回临时文件的路径字符串(如 "/tmp/pytest-of-user/test.docx"
### Requirement: DOCX 文件创建能力
temp_docx fixture MUST 支持创建包含段落、标题、表格、列表的 DOCX 文件。
#### Scenario: 创建包含段落的 DOCX
- **WHEN** 调用 `temp_docx(paragraphs=["第一段", "第二段"])`
- **THEN** 创建包含指定段落的 DOCX 文件
#### Scenario: 创建包含表格的 DOCX
- **WHEN** 调用 `temp_docx(table_data=[["A1", "B1"], ["A2", "B2"]])`
- **THEN** 创建包含 2x2 表格的 DOCX 文件
#### Scenario: 创建包含混合内容的 DOCX
- **WHEN** 调用 `temp_docx(paragraphs=["标题"], table_data=[["A", "B"]])`
- **THEN** 创建包含段落和表格的 DOCX 文件
### Requirement: PDF 文件创建能力
temp_pdf fixture MUST 支持创建包含基本文本的 PDF 文件。
#### Scenario: 创建包含文本的 PDF
- **WHEN** 调用 `temp_pdf(text="测试内容")`
- **THEN** 创建包含指定文本的 PDF 文件
### Requirement: HTML 文件创建能力
temp_html fixture MUST 支持创建包含各种元素的 HTML 文件。
#### Scenario: 创建包含标题和段落的 HTML
- **WHEN** 调用 `temp_html(content="<h1>标题</h1><p>段落</p>")`
- **THEN** 创建包含指定内容的 HTML 文件