pytest-dev / pytest-random-order

pytest plugin to randomise the order of tests with some control over the randomness
MIT License
65 stars 10 forks source link

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

Closed binbinlv closed 1 year ago

binbinlv commented 2 years ago

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

Thanks.

vsisl commented 2 years ago

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
Alexander-Serov commented 2 years ago

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)

matejsp commented 1 year ago

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
jbasko commented 1 year ago

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)

binbinlv commented 1 year ago

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