首页 > 解决方案 > 并行运行测试时仅运行一次 pytest 夹具

问题描述

当测试通过 shell 脚本并行运行时,我在 conftest 文件中仅运行一次 pytest 夹具时遇到了一些真正的困难。shell脚本的内容如下:

#!/usr/bin/env bash
pytest -k 'mobile' --os iphone my_tests/ &
pytest -k 'mobile' --os ipad my_tests/
wait

pytest 夹具在运行测试之前为手机测试创建资源:

@pytest.fixture(scope='session', autouse=True)
def create_resources():
    // Do stuff to create the resources
    yield
    // Do stuff to remove the resources

当每个单独运行时,它都能完美运行。创建资源,运行测试并最终删除它创建的资源。当并行运行(使用 shell 脚本)时,两者都尝试同时运行 create_resources 固定装置。

有谁知道我可以只运行一次 create_resource 夹具的方法吗?如果是这样,那么第二个设备是否有可能等到所有设备运行测试之前创建资源?

标签: python-3.xpytestfixturesparallel-testingpytest-xdist

解决方案


推荐阅读