首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >pytest在与xdist并行运行之前预先配置

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

Stack Overflow用户
提问于 2016-03-22 04:51:40
回答 1查看 2.2K关注 0票数 3

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

代码语言:javascript
复制
INTERNALERROR> OSError: [Errno 17] File exists: '/path/to/file'

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

编辑:我可能已经找到了我的问题here的解决方案。不过,我仍然需要测试它。

EN

回答 1

Stack Overflow用户

发布于 2016-03-28 05:10:15

是的,这解决了我的问题。我添加了我如何实现它的link中的示例代码。它使用一个装置将数据注入到只由pytest_configure中的主进程写入的slaveinput dict中。

代码语言:javascript
复制
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')
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36141349

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档