'How to execute pytest paralelly through xdist plugin?

How can I run my pytests parallely on multiple custom environments ? I have pytest-xdist as well. Not sure if this plugin helps

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))

When I run the command:

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

this is my output ...

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 ==============================================================

Similary, when I run the command:

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

this is my output ...

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 ==============================================================

I have 2 requirements here,

  1. I want to run more than one environments to be run at the same time
  2. It needs to run paralelly

So I chose xdist plugin; but I am not sure if I can run with my examples. Is it possible to run with xdist for my example ?

I need something like this ...

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

to run both env1, env2 paralelly



Solution 1:[1]

Check this:

--dist loadgroup: Tests are grouped by the xdist_group mark. Groups are distributed to available workers as whole units. This guarantees that all tests with same xdist_group name run in the same worker.

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

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

This will make sure test1 and TestA::test2 will run in the same worker. Tests without the xdist_group mark are distributed normally as in the --dist=load mode.

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bashar SHARBAK