47 lines
1.0 KiB
Python
47 lines
1.0 KiB
Python
"""
|
||
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()
|