pytest 在与 XDIST 并行运行之前预先配置



我刚刚开始使用pytest与xdist结合使用来并行运行测试。我 contest.py 我有一个配置钩子来创建一些测试数据副本(带有时间戳)和测试运行所需的文件。在我使用 xdist 之前一切正常。看起来pytest_configure首先执行,然后再次为每个进程执行,结果为:

INTERNALERROR> OSError: [Errno 17] File exists: '/path/to/file'

我最终得到了 n+1 个目录(几秒钟后)。有没有办法在分发之前预先配置测试运行?

编辑:我可能在这里找到了解决问题的方法。不过我仍然需要测试它。

是的,这解决了我的问题。我从链接中添加了示例代码,我是如何实现它的。它使用夹具将数据注入到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')

最新更新