首页 文章

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

提问于
浏览
1

我刚开始将pytest与xdist结合使用来并行运行测试 . 我的contest.py我有一个配置挂钩来创建一些测试数据目录(带有时间戳)和我测试运行所需的文件 . 一切正常,直到我使用xdist . 看起来pytest_configure首先执行,然后再次为每个进程执行,导致:

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

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

编辑:我可能找到了解决问题的方法here . 我仍然需要测试它 .

1 回答

  • 3

    是的,这解决了我的问题 . 我添加了link中的示例代码,我是如何实现它的 . 它正在使用夹具将数据注入 slaveinput dict,该数据仅由 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')
    

相关问题