Files
mpsp/test/conftest.py
2026-02-21 12:00:47 +08:00

47 lines
1.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Pytest 配置文件
"""
import pytest
import multiprocessing
def pytest_configure(config):
"""配置 pytest"""
# 设置多进程启动方法fork 在 Linux 上更快spawn 在 Windows/macOS 上更稳定)
try:
multiprocessing.set_start_method("fork", force=True)
except RuntimeError:
# 如果已经设置过,忽略错误
pass
@pytest.fixture(scope="function", autouse=True)
def reset_shared_pool():
"""
每个测试函数执行前清理共享池
这是一个自动使用的 fixture确保每个测试都在干净的环境中运行
"""
from mpsp.mpsp import MultiProcessingSharedPool
pool = MultiProcessingSharedPool()
pool.clear()
yield
# 测试结束后也清理
pool.clear()
@pytest.fixture(scope="session")
def shared_pool():
"""
提供共享池实例的 fixture
在整个测试会话中复用同一个实例(单例模式)
"""
from mpsp.mpsp import MultiProcessingSharedPool
return MultiProcessingSharedPool()