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

标签 pytest xdist

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

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

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

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

最佳答案

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

关于pytest 在与 xdist 并行运行之前预先配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36141349/

相关文章:

python - 使用 pytest-xdist 跨主节点和工作节点访问共享资源

python - 是否可以将 xdist 网关编号打印到 stdout 中的每一行?

pytest - 使用 xdist 执行 pytest 测试后,如何聚合测试结果以发布到 testrail?

python - 您如何正确地将用于文件解析的单元测试与 pytest 集成?

django - 使用 pytest 在 Django Admin 中测试 delete_selected 操作

python - 为什么派生类中的测试会重新运行父类测试?

python - Pytest Xdist 不同的测试被收集错误

python - 如何在运行 pytest 测试时随意执行 ipdb.set_trace()

pytest - 是否可以指向 tox 从分支中提取依赖项(又名在幕后使用 `pip -e`)?

python - pytest-xdist 对于单例线程安全吗