如何通过 xdist 插件执行 pytest paralelly



如何在多个自定义环境中并行运行pytests?我也有pytest-xdist。不确定此插件是否有帮助

test_test.py

@pytest.mark.env("env1", "env2", "env3")
def test_check(env):
    print("Chosen {} for test_check function".format(env))
@pytest.mark.env("env1", "env2")
def test_check2(env):
    print("Chosen {} for test_check2 function".format(env))
@pytest.mark.env("env1")
def test_check3(env):
    print("Chosen {} for test_check3 function".format(env))

当我运行命令时:

pytest -v -s test_test.py --env env1

这是我的输出...

collected 3 items                                                                                                                                              
conftest.py::test_check Chosen env1 for test_check function
PASSED
conftest.py::test_check2 Chosen env1 for test_check function
PASSED
conftest.py::test_check3 Chosen env1 for test_check function 
PASSED
============================================================= 3 passed in 0.02 seconds ==============================================================

同样,当我运行命令时:

pytest -v -s test_test.py --env env2

这是我的输出...

collected 2 items                                                                                                                                              
conftest.py::test_check Chosen env2 for test_check function
PASSED
conftest.py::test_check2 Chosen env2 for test_check function
PASSED
============================================================= 2 passed in 0.02 seconds ==============================================================

我这里有 2 个要求,

  1. 我想同时运行多个环境
  2. 它需要平行运行

所以我选择了xdist插件;但我不确定我是否可以运行我的例子。是否可以为我的示例使用 xdist 运行?

我需要这样的东西...

pytest -v -s -d --tx test_test.py --env env1 --env env2

要同时运行 env1,env2 paralelly

检查这个:

-

-dist 载荷组:测试按xdist_group标记分组。组作为整个单元分发给可用工作人员。这保证了具有相同xdist_group名称的所有测试都在同一辅助角色中运行。

@pytest.mark.xdist_group(name="group1")
def test1():
    pass
class TestA:
    @pytest.mark.xdist_group("group1")
    def test2():
        pass

这将确保 test1 和 TestA::test2 将在同一辅助角色中运行。没有xdist_group标记的测试在 --dist=load 模式下呈正态分布。

https://pypi.org/project/pytest-xdist/#:~:text=--dist%20loadgroup%3A%20Tests,dist%3Dload%20mode。

最新更新