| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- """
- pytest 配置文件
- 定义全局 fixtures 和配置
- """
- import pytest
- import sys
- import os
- # 添加项目根目录到 Python 路径
- project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
- if project_root not in sys.path:
- sys.path.insert(0, project_root)
- @pytest.fixture(scope="session")
- def project_root_path():
- """返回项目根目录路径"""
- return project_root
- @pytest.fixture(scope="session")
- def test_data_dir():
- """返回测试数据目录路径"""
- return os.path.join(os.path.dirname(__file__), 'test_data')
- @pytest.fixture(autouse=True)
- def reset_environment():
- """每个测试前重置环境"""
- # 测试前的设置
- yield
- # 测试后的清理
- pass
- def pytest_configure(config):
- """pytest 配置钩子"""
- config.addinivalue_line(
- "markers", "integration: 标记集成测试(需要实际API服务)"
- )
- config.addinivalue_line(
- "markers", "slow: 标记慢速测试"
- )
- config.addinivalue_line(
- "markers", "unit: 标记单元测试"
- )
- def pytest_collection_modifyitems(config, items):
- """修改测试收集项"""
- for item in items:
- # 为所有异步测试添加 asyncio 标记
- if "asyncio" in item.keywords:
- item.add_marker(pytest.mark.asyncio)
|