conftest.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """
  2. pytest 配置文件
  3. 定义全局 fixtures 和配置
  4. """
  5. import pytest
  6. import sys
  7. import os
  8. # 添加项目根目录到 Python 路径
  9. project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
  10. if project_root not in sys.path:
  11. sys.path.insert(0, project_root)
  12. @pytest.fixture(scope="session")
  13. def project_root_path():
  14. """返回项目根目录路径"""
  15. return project_root
  16. @pytest.fixture(scope="session")
  17. def test_data_dir():
  18. """返回测试数据目录路径"""
  19. return os.path.join(os.path.dirname(__file__), 'test_data')
  20. @pytest.fixture(autouse=True)
  21. def reset_environment():
  22. """每个测试前重置环境"""
  23. # 测试前的设置
  24. yield
  25. # 测试后的清理
  26. pass
  27. def pytest_configure(config):
  28. """pytest 配置钩子"""
  29. config.addinivalue_line(
  30. "markers", "integration: 标记集成测试(需要实际API服务)"
  31. )
  32. config.addinivalue_line(
  33. "markers", "slow: 标记慢速测试"
  34. )
  35. config.addinivalue_line(
  36. "markers", "unit: 标记单元测试"
  37. )
  38. def pytest_collection_modifyitems(config, items):
  39. """修改测试收集项"""
  40. for item in items:
  41. # 为所有异步测试添加 asyncio 标记
  42. if "asyncio" in item.keywords:
  43. item.add_marker(pytest.mark.asyncio)