gpt4 book ai didi

pytest-parallel 不遵守模块范围的固定装置

转载 作者:行者123 更新时间:2023-12-05 06:59:46 25 4
gpt4 key购买 nike

假设我在文件 test_something.py 中编写了以下测试用例:

@pytest.fixture(scope="module")
def get_some_binary_file():
# Some logic here that creates a path "/a/b/bin" and then downloads a binary into this path
os.mkdir("/a/b/bin") ### This line throws the error in pytest-parallel
some_binary = os.path.join("/a/b/bin", "binary_file")
download_bin("some_bin_url", some_binary)
return some_binary


test_input = [
{"some": "value"},
{"foo": "bar"}
]



@pytest.mark.parametrize("test_input", test_input, ids=["Test_1", "Test_2"])
def test_1(get_some_binary_file, test_input):
# Testing logic here


# Some other completely different tests below
def test_2():
# Some other testing logic here

当我使用下面的 pytest 命令运行上面的代码时,它们可以正常工作。

pytest -s --disable-warnings test_something.py

但是,我想以并行方式运行这些测试用例。我知道 test_1test_2 应该并行运行。所以我查看了 pytest-parallel 并执行了以下操作:

pytest --workers auto -s --disable-warnings test_something.py. 

但是如上代码所示,当它去创建/a/b/bin文件夹时,它抛出一个错误说该目录已经存在。所以这意味着模块范围在 pytest-parallel 中没有得到尊重。它正在尝试为 test_1 的每个参数化输入执行 get_some_binary_file 我有办法做到这一点吗?

我还使用 --dist loadscope 选项查看了 pytest-xdist,并为其运行了以下命令:

pytest -n auto --dist loadscope -s --disable-warnings test_something.py

但这给了我如下所示的输出,其中 test_1test_2 都在同一个 worker 上执行。

tests/test_something.py::test_1[Test_1]
[gw1] PASSED tests/test_something.py::test_1[Test_1] ## Expected
tests/test_something.py::test_1[Test_2]
[gw1] PASSED tests/test_something.py::test_1[Test_2] ## Expected
tests/test_something.py::test_2
[gw1] PASSED tests/test_something.py::test_2 ## Not expected to run in gw1

从上面的输出可以看出,test_2 正在 gw1 中运行。为什么?它不应该在不同的 worker 中运行吗?

最佳答案

Group the definitions with xdist_group to run per process. Run like this to assign it to per process,  pytest xdistloadscope.py -n 2 --dist=loadgroup

@pytest.mark.xdist_group("group1")
@pytest.fixture(scope="module")
def get_some_binary_file():
# Some logic here that creates a path "/a/b/bin" and then downloads a binary into this path
os.mkdir("/a/b/bin") ### This line throws the error in pytest-parallel
some_binary = os.path.join("/a/b/bin", "binary_file")
download_bin("some_bin_url", some_binary)
return some_binary


test_input = [
{"some": "value"},
{"foo": "bar"}
]


@pytest.mark.xdist_group("group1")
@pytest.mark.parametrize("test_input", test_input, ids=["Test_1", "Test_2"])
def test_1(get_some_binary_file, test_input):
# Testing logic here


# Some other completely different tests below
@pytest.mark.xdist_group("group2")
def test_2():
# Some other testing logic here

关于pytest-parallel 不遵守模块范围的固定装置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64319447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com