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 个测试通过。
This commit is contained in:
2026-03-08 22:20:21 +08:00
parent c35bbc90b5
commit 7eab1dcef1
53 changed files with 3094 additions and 259 deletions

View File

@@ -0,0 +1,151 @@
"""测试 html2text Reader 的解析功能。"""
import pytest
import os
from scripts.readers.html import HtmlReader
class TestHtml2TextReaderParse:
"""测试 html2text Reader 的 parse 方法。"""
def test_normal_file(self, temp_html):
"""测试正常 HTML 文件解析。"""
html_content = """
<h1>主标题</h1>
<p>这是一段测试内容。</p>
<h2>子标题</h2>
<ul>
<li>列表项1</li>
<li>列表项2</li>
</ul>
<table>
<tr><td>单元格1</td><td>单元格2</td></tr>
</table>
"""
file_path = temp_html(content=html_content)
reader = HtmlReader()
content, failures = reader.parse(file_path)
# 验证解析成功
assert content is not None, f"解析失败: {failures}"
# 验证关键内容存在
assert "主标题" in content
assert "测试内容" in content
assert "子标题" in content
assert "列表项1" in content
def test_file_not_exists(self, tmp_path):
"""测试文件不存在的情况。"""
non_existent_file = str(tmp_path / "non_existent.html")
reader = HtmlReader()
content, failures = reader.parse(non_existent_file)
# 验证返回 None 和错误信息
assert content is None
assert len(failures) > 0
assert any("不存在" in f or "找不到" in f for f in failures)
def test_empty_file(self, temp_html):
"""测试空 HTML 文件。"""
file_path = temp_html(content="<html><body></body></html>")
reader = HtmlReader()
content, failures = reader.parse(file_path)
# 空文件应该返回 None 或空字符串
assert content is None or content.strip() == ""
def test_corrupted_file(self, temp_html):
"""测试损坏的 HTML 文件。"""
# HTML 解析器通常比较宽容,但我们可以测试完全无效的内容
file_path = temp_html(content="<<>>invalid<<html>>")
reader = HtmlReader()
content, failures = reader.parse(file_path)
# HTML 解析器可能仍然能解析,或返回错误
# 这里只验证不会崩溃
def test_special_chars(self, temp_html):
"""测试特殊字符处理。"""
html_content = """
<p>中文测试内容</p>
<p>Emoji测试: 😀🎉🚀</p>
<p>特殊符号: ©®™°±</p>
<p>混合内容: Hello你好🎉World世界</p>
"""
file_path = temp_html(content=html_content)
reader = HtmlReader()
content, failures = reader.parse(file_path)
assert content is not None, f"解析失败: {failures}"
# 验证各种特殊字符都被正确处理
assert "中文测试内容" in content
assert "Hello你好" in content or "World世界" in content
def test_encoding_gbk(self, temp_html):
"""测试 GBK 编码的 HTML 文件。"""
html_content = "<html><head><meta charset='gbk'></head><body><p>中文内容</p></body></html>"
file_path = temp_html(content=html_content, encoding='gbk')
reader = HtmlReader()
content, failures = reader.parse(file_path)
# 验证能够正确处理 GBK 编码
# 注意:某些 Reader 可能无法自动检测编码
if content:
assert len(content.strip()) > 0
def test_encoding_utf8_bom(self, temp_html, tmp_path):
"""测试 UTF-8 BOM 的 HTML 文件。"""
html_content = "<html><body><p>测试内容</p></body></html>"
file_path = tmp_path / "test_bom.html"
# 写入带 BOM 的 UTF-8 文件
with open(file_path, 'wb') as f:
f.write(b'\xef\xbb\xbf') # UTF-8 BOM
f.write(html_content.encode('utf-8'))
reader = HtmlReader()
content, failures = reader.parse(str(file_path))
# 验证能够正确处理 UTF-8 BOM
if content:
assert "测试内容" in content
class TestHtml2TextReaderSupports:
"""测试 html2text Reader 的 supports 方法。"""
def test_supports_html_extension(self):
"""测试识别 .html 扩展名。"""
reader = HtmlReader()
assert reader.supports("test.html") is True
def test_supports_htm_extension(self):
"""测试识别 .htm 扩展名。"""
reader = HtmlReader()
assert reader.supports("test.htm") is True
def test_supports_uppercase_extension(self):
"""测试识别大写扩展名。"""
reader = HtmlReader()
assert reader.supports("TEST.HTML") is True
def test_supports_url(self):
"""测试 URL。"""
reader = HtmlReader()
# HTML Reader 通常支持 URL
result = reader.supports("http://example.com/page.html")
# 根据实际实现可能返回 True
def test_rejects_unsupported_format(self):
"""测试拒绝不支持的格式。"""
reader = HtmlReader()
assert reader.supports("test.pdf") is False
assert reader.supports("test.docx") is False