23
votes

I wanted to run parameterized test functions in parallel. This is for a concurrency testing scenario. Same testcase runs in parallel with different parameters in a device. After completing all the parameterized variant of one test function, I want to proceed with the next one.

If we take this simple example, I want to run all 4 instances of test_even parallely and then move to test_odd.

@pytest.mark.parametrize("x", range(4))
def test_even(x):
    assert x % 2 == 0        
@pytest.mark.parametrize("x", range(4))
def test_odd(x):
    assert x % 2 != 0

Is it possible to do in pytest? I checked xdist, but could not find this type of support. Could anybody please give some pointers on how to implement this in pytest?

1
I don't think xdist will allow you to synchronize between tests (it will run all your tests on all available processes -- which is generally what you'd want)thebjorn
The directly relevant GitHub issue: Parallelization of the parameters #58, seems to be an open question as of Feb 18, 2019jxramos
It is possible to create your own xdist scheduler (you can fork an existing one and modify it). Then, you can schedule the tests per worker according to your logic. If you elaborate a little on what exactly you are trying to accomplish, it may be possible to think of an alternative way to accomplish it without modifying the scheduler.Meir

1 Answers

0
votes

Have a look at pytest-xdist it does a bunch of cool things, including allowing you to run your tests in parallel.

$ pip install pytest-xdist
$ pytest -n <num cpus>