假设我在文件中编写了以下测试用例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 中没有得到尊重。它正在尝试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 正在运行。为什么?它不应该在不同的工人中运行吗?