我刚刚开始结合使用pytest和xdist来并行运行测试。在我的contest.py中,我有一个配置钩子来创建一些测试数据目录(带有时间戳)和我的测试运行所需的文件。在我使用xdist之前,一切都很正常。看起来每个进程都先执行pytest_configure,然后再执行一次,结果如下:
INTERNALERROR> OSError: [Errno 17] File exists: '/path/to/file'
我最终得到了n+1目录(几秒钟后)。有没有办法在分发之前预先配置测试运行?
编辑:我可能已经找到了我的问题here的解决方案。不过,我仍然需要测试它。
发布于 2016-03-27 21:10:15
是的,这解决了我的问题。我添加了我如何实现它的link中的示例代码。它使用一个装置将数据注入到只由pytest_configure
中的主进程写入的slaveinput
dict中。
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')
https://stackoverflow.com/questions/36141349
复制