Files
lyxy-document/tests/test_readers/conftest.py
lanyuanxiaoyao 82b09614d3 test: 修复测试静默跳过问题,统一conftest行为
- 移除 test_readers/conftest.py 中重复的 temp_docx/temp_pdf/temp_pptx/temp_xlsx fixtures
- 统一测试行为:依赖缺失时测试失败而非静默跳过
- 修复根 conftest.py 中裸露的 except 子句,改为 except Exception
- 保留 temp_html 和静态文件相关 fixtures
2026-03-12 18:57:36 +08:00

115 lines
3.1 KiB
Python

"""Reader 测试专用 fixtures。"""
import pytest
from pathlib import Path
@pytest.fixture
def temp_html(tmp_path):
"""创建临时 HTML 文件的 fixture 工厂。
Args:
content: HTML 内容字符串
encoding: 文件编码,默认 'utf-8'
Returns:
str: 临时文件路径
"""
def _create_html(content="<html><body><p>Test</p></body></html>", encoding='utf-8'):
file_path = tmp_path / "test.html"
# 如果内容不包含完整的 HTML 结构,添加基本结构
if not content.strip().startswith('<html'):
content = f"<html><head><meta charset='{encoding}'></head><body>{content}</body></html>"
with open(file_path, 'w', encoding=encoding) as f:
f.write(content)
return str(file_path)
return _create_html
# 静态测试文件目录
FIXTURES_DIR = Path(__file__).parent / "fixtures"
@pytest.fixture
def doc_fixture_path():
"""返回 DOC 静态测试文件目录"""
return FIXTURES_DIR / "doc"
@pytest.fixture
def xls_fixture_path():
"""返回 XLS 静态测试文件目录"""
return FIXTURES_DIR / "xls"
@pytest.fixture
def ppt_fixture_path():
"""返回 PPT 静态测试文件目录"""
return FIXTURES_DIR / "ppt"
def _get_static_file_path(fixture_dir, filename):
"""获取静态文件路径,不存在时跳过测试"""
file_path = fixture_dir / filename
if not file_path.exists():
pytest.skip(f"静态测试文件不存在: {file_path}")
return str(file_path)
@pytest.fixture
def simple_doc_path(doc_fixture_path):
"""返回简单 DOC 测试文件路径"""
return _get_static_file_path(doc_fixture_path, "simple.doc")
@pytest.fixture
def with_headings_doc_path(doc_fixture_path):
"""返回带标题的 DOC 测试文件路径"""
return _get_static_file_path(doc_fixture_path, "with_headings.doc")
@pytest.fixture
def with_table_doc_path(doc_fixture_path):
"""返回带表格的 DOC 测试文件路径"""
return _get_static_file_path(doc_fixture_path, "with_table.doc")
@pytest.fixture
def simple_xls_path(xls_fixture_path):
"""返回简单 XLS 测试文件路径"""
return _get_static_file_path(xls_fixture_path, "simple.xls")
@pytest.fixture
def multiple_sheets_xls_path(xls_fixture_path):
"""返回多工作表 XLS 测试文件路径"""
return _get_static_file_path(xls_fixture_path, "multiple_sheets.xls")
@pytest.fixture
def with_formulas_xls_path(xls_fixture_path):
"""返回带公式 XLS 测试文件路径"""
return _get_static_file_path(xls_fixture_path, "with_formulas.xls")
@pytest.fixture
def simple_ppt_path(ppt_fixture_path):
"""返回简单 PPT 测试文件路径"""
return _get_static_file_path(ppt_fixture_path, "simple.ppt")
@pytest.fixture
def multiple_slides_ppt_path(ppt_fixture_path):
"""返回多幻灯片 PPT 测试文件路径"""
return _get_static_file_path(ppt_fixture_path, "multiple_slides.ppt")
@pytest.fixture
def with_images_ppt_path(ppt_fixture_path):
"""返回带图片 PPT 测试文件路径"""
return _get_static_file_path(ppt_fixture_path, "with_images.ppt")