我刚开始结合使用 pytest 和 xdist 来并行运行测试。我的 contest.py 我有一个配置 Hook 来创建一些测试数据目录(带有时间戳)和测试运行所需的文件。在我使用 xdist 之前一切正常。看起来 pytest_configure 首先被执行,然后对每个进程再次执行,结果是:
INTERNALERROR> OSError: [Errno 17] File exists: '/path/to/file'
最后我得到了 n+1 个目录(几秒钟后)。 有没有办法在分发之前预先配置测试运行?
编辑: 我可能已经找到解决问题的方法 here .不过,我仍然需要对其进行测试。
最佳答案
是的,这解决了我的问题。我添加了 link 中的示例代码我是如何实现的。它使用 fixture 将数据注入(inject) slaveinput
字典,该字典仅由主进程在 pytest_configure
中写入。
def pytest_configure(config):
if is_master(config):
config.shared_directory = os.makedirs('/tests/runs/')
def pytest_configure_node(self, node):
"""xdist hook"""
node.slaveinput['shared_dir'] = node.config.shared_directory
@pytest.fixture
def shared_directory(request):
if is_master(request.config):
return request.config.shared_directory
else:
return request.config.slaveinput['shared_dir']
def is_master(config):
"""True if the code running the given pytest.config object is running in a xdist master
node or not running xdist at all.
"""
return not hasattr(config, 'slaveinput')
关于pytest 在与 xdist 并行运行之前预先配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36141349/