"""
pytest配置文件
定义测试夹具和配置。
"""
import sys
import os
from pathlib import Path
# 添加src目录到Python路径
project_root = Path(__file__).parent.parent
src_path = project_root / "src"
sys.path.insert(0, str(src_path))
# 确保环境变量加载
from dotenv import load_dotenv
env_path = project_root / ".env"
if env_path.exists():
load_dotenv(env_path)
import pytest
from fs_mcp.config import config
from fs_mcp.rustfs_client import RustFSClient
from fs_mcp.upload_tool import create_upload_tool
from fs_mcp.download_tool import create_download_tool
import tempfile
@pytest.fixture
def fs_client():
"""创建RustFS客户端夹具"""
return RustFSClient()
@pytest.fixture
def upload_tool():
"""创建上传工具夹具"""
return create_upload_tool()
@pytest.fixture
def download_tool():
"""创建下载工具夹具"""
return create_download_tool()
@pytest.fixture
def sample_file():
"""创建测试文件夹具"""
with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False) as f:
f.write("这是一个测试文件\n用于测试上传和下载功能。")
temp_path = f.name
yield temp_path
# 清理
if os.path.exists(temp_path):
os.unlink(temp_path)
@pytest.fixture
def temp_dir():
"""创建临时目录夹具"""
with tempfile.TemporaryDirectory() as temp_dir:
yield temp_dir
@pytest.fixture(scope="session")
def test_config():
"""测试配置夹具"""
return {
"fs_url": config.fs_url,
"default_bucket": config.default_bucket,
"timeout": config.timeout
}
# 测试标记
def pytest_configure(config):
"""配置pytest标记"""
config.addinivalue_line(
"markers", "unit: 单元测试"
)
config.addinivalue_line(
"markers", "integration: 集成测试"
)
config.addinivalue_line(
"markers", "slow: 慢速测试(需要网络访问)"
)