微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

pytest-parallel不遵守模块范围夹具

如何解决pytest-parallel不遵守模块范围夹具

假设我将以下测试用例写在文件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中不遵循module-scope。它正在尝试为get_some_binary_file的每个参数化输入执行test_1我有办法吗?

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

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

但这给了我类似下面的输出,其中test_1test_2都在同一工作线程上执行。

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中运行。为什么?它不应该在其他工作程序中运行吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。