conftest.py 1.2 KB

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