0

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

MrBean Bremen
  • 14,916
  • 3
  • 26
  • 46
Bharath Kashyap
  • 343
  • 2
  • 5
  • 14
  • `xdist` only supports the following scopes: `loadscope` which groups tests by module/class, and `loadfile` which groups tests by test file name. If you need to group by markers, you will need to implement your own scheduler. – hoefling Jun 14 '19 at 11:03
  • @hoefling does pytest provide any hook specs which is close to my requirement ? – Bharath Kashyap Jun 14 '19 at 11:06
  • `pytest` itself doesn't implement running tests in parallel, so no, unfortunately. – hoefling Jun 14 '19 at 11:11

1 Answers1

0

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.