【问题标题】:pytest configure upfront before running in parallel with xdistpytest 在与 xdist 并行运行之前预先配置
【发布时间】:2016-03-21 20:51:39
【问题描述】:

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

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

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

编辑: 我可能已经找到解决问题的方法here。不过,我仍然需要对其进行测试。

【问题讨论】:

  • 我猜你应该编写自己的锁机制。
  • 你能给我举个例子吗?

标签: pytest xdist


【解决方案1】:

是的,这解决了我的问题。我添加了来自link 我如何实现它的示例代码。它使用夹具向slaveinput dict 注入数据,该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')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 2023-02-18
    • 2018-06-15
    • 2022-08-05
    • 2022-10-13
    • 1970-01-01
    相关资源
    最近更新 更多