问题描述
假设我将以下测试用例写在文件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_1
和test_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_1
和test_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 (将#修改为@)