pytest-xdist icon indicating copy to clipboard operation
pytest-xdist copied to clipboard

Select which test should be distributed and which test should be run without concurrence

Open mscheltienne opened this issue 11 months ago • 8 comments

I would like to use pytest-xdist in mne-lsl: https://github.com/mne-tools/mne-lsl/pull/229 However, in the test suite, some test need to be run while no other test are running because they spawn network resources which might be pick-up by other tests. Example:

def test1(...):
    spawn_network_resources()
    ...

def test2(...):
    resources = resolve_all_network_resources()
    assert len(resources) == 0
    spawn_network_resources()
    resources = resolve_all_network_resources()
    assert len(resources) == 1

In this example, test2 would be affected by test1 run in a different worker. To avoid this problem, I would like new markers from pytest-xdist to mark which test should not be distributed and should be run through --dist no, either before or after all the other tests distributed to workers.

e.g.

@pytest.mark.no_xdist
def test2(...):

My current alternative idea is to create this marker and to run 2 separate pytest command.

mscheltienne avatar Mar 14 '24 16:03 mscheltienne

The difference between this request and my alternative lies that developers won't accidently run the test suit with xdist and without the -m "not no_xdist". The -m flag is not needed anymore as xdist will handle dispatching correctly.

mscheltienne avatar Mar 15 '24 09:03 mscheltienne

When xdist is active, no tests run in the controling process

There's no plan for a meta scheduler that ensures noncoop tests are executed on a single node in isolation

RonnyPfannschmidt avatar Mar 15 '24 12:03 RonnyPfannschmidt

OK thank you! Could that be a feature request then? Some test need to be run in isolation; and preventing an entire test suite to run on xdist because of a minority is disappointing.

If there is no plan/desire to add this feature, feel free to close this issue!

mscheltienne avatar Mar 22 '24 14:03 mscheltienne

there is a desire to add it, but lack of engineering time to build it

RonnyPfannschmidt avatar Mar 22 '24 14:03 RonnyPfannschmidt

@mscheltienne for me it works adding sequencial tests in a specific group (by mark) and then execute pytest with --dist loadgroup as described here https://pytest-xdist.readthedocs.io/en/latest/distribution.html#running-tests-across-multiple-cpus

andreabisello avatar Sep 22 '24 21:09 andreabisello

If I read this correctly:

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

It just means that the tests in loadgroup will be executed by the same worker. However, another worker might be executing other tests at the same time, and this is what I would want to avoid. I wanted to have a group of tests that should run in xdist, and a group of tests that should run sequentially on a single worker.

mscheltienne avatar Sep 23 '24 09:09 mscheltienne

@mscheltienne yeah pack all the test that you want to execute sequentially in the same worker using the same group, all the rest will be distributed to all the other workers

andreabisello avatar Sep 23 '24 10:09 andreabisello

Yes, but that's my issue, I while the tests run sequentially are running, the other tests are also running simultaneously. I want to split my workflow in 2 steps, one where the distributed tests run, and then one where the sequential tests run. Anyway, for now I'm not going to use xdist.

mscheltienne avatar Sep 24 '24 08:09 mscheltienne

This would need a new scheduler, currently I also use distinct calls to pytest to run sequential and parallel tests

RonnyPfannschmidt avatar Sep 24 '24 16:09 RonnyPfannschmidt