pytest-random-order icon indicating copy to clipboard operation
pytest-random-order copied to clipboard

Random order does not take effect with parameter "-n 4" (module pytest-xdist)

Open binbinlv opened this issue 3 years ago • 3 comments

Could you help to support the scenario of multiple process (the plugin "pytest-xdist": -n 6)?

Thanks.

binbinlv avatar Jan 13 '22 03:01 binbinlv

For me, trying to use pytest-xdist and pyters-random-order together results into an error during collection of tests. (ERROR collecting gwX). Tests are not executed at all.

Being able to run tests in randomised order in parallel would be great...

Anyways, thanks for creating this plugin, it is very helpful even without support for parallel execution!

Some info about my setup:

platform linux -- Python 3.7.7, pytest-6.2.5, py-1.9.0, pluggy-0.13.1
plugins: dash-1.16.1, timeout-2.1.0, random-order-1.0.4, forked-1.3.0, xdist-2.4.0

vsisl avatar Feb 22 '22 14:02 vsisl

For me, trying to use pytest-xdist and pyters-random-order together results into an error during collection of tests. (ERROR collecting gwX). Tests are not executed at all.

Being able to run tests in randomised order in parallel would be great...

Anyways, thanks for creating this plugin, it is very helpful even without support for parallel execution!

Some info about my setup:

platform linux -- Python 3.7.7, pytest-6.2.5, py-1.9.0, pluggy-0.13.1
plugins: dash-1.16.1, timeout-2.1.0, random-order-1.0.4, forked-1.3.0, xdist-2.4.0

I am having the same issue with pytest-xdist: https://github.com/jbasko/pytest-random-order/issues/22#issuecomment-1196393220 (pytest-random-order==1.0.4, pytest-xdist==2.5.0)

Alexander-Serov avatar Jul 27 '22 08:07 Alexander-Serov

I have a solution for this. Ideally I would prepare PR but this project seems not maintained anymore.

Put the following to conftest.py and it should work:

class XdistHooks:

    def pytest_configure_node(self, node: pytest.Item) -> None:
        seed = node.config.getoption('random_order_seed')
        node.workerinput['random_order_seed'] = seed  # type: ignore [attr-defined]


def pytest_configure(config: pytest.Config) -> None:
    if config.pluginmanager.hasplugin('xdist'):
        config.pluginmanager.register(XdistHooks())

    if hasattr(config, "workerinput"):  # pragma: no cover
        # pytest-xdist: use seed generated on main.
        seed = config.workerinput["random_order_seed"]  # type: ignore [attr-defined]
        if hasattr(config, "cache"):
            assert config.cache is not None
            config.cache.set("random_order_seed", seed)
        config.option.random_order_seed = seed

matejsp avatar Oct 12 '22 17:10 matejsp

This is fixed in v1.1.0 thanks to the code provided by @matejsp (apologies for too many mentions in the last few hours, had trouble updating to do the build and publish with github actions)

jbasko avatar Dec 03 '22 16:12 jbasko

Verified, it is fixed in pytest-random-order v1.1.0.

binbinlv avatar Dec 05 '22 09:12 binbinlv