Skip to content

Commit

Permalink
Fix when xdist not installed or active
Browse files Browse the repository at this point in the history
Fixes #246.
  • Loading branch information
adamchainz committed Apr 15, 2020
1 parent 98339d3 commit ea78e3f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
3 changes: 3 additions & 0 deletions HISTORY.rst
Expand Up @@ -2,6 +2,9 @@
History
-------

* Fix to work when pytest-xdist is not installed or active
(``PluginValidationError: unknown hook 'pytest_configure_node'``).

3.3.0 (2020-04-15)
------------------

Expand Down
18 changes: 13 additions & 5 deletions src/pytest_randomly.py
Expand Up @@ -10,6 +10,10 @@
else:
from importlib_metadata import entry_points

try:
import xdist
except ImportError:
xdist = None

# factory-boy
try:
Expand Down Expand Up @@ -88,6 +92,9 @@ def pytest_addoption(parser):


def pytest_configure(config):
if config.pluginmanager.hasplugin("xdist"):
config.pluginmanager.register(XdistHooks())

seed_value = config.getoption("randomly_seed")
if seed_value == "last":
seed = config.cache.get("randomly_seed", default_seed)
Expand All @@ -103,11 +110,12 @@ def pytest_configure(config):
config.option.randomly_seed = seed


def pytest_configure_node(node):
"""
pytest-xdist hook. Send the selected seed through to the nodes.
"""
node.workerinput["randomly_seed"] = node.config.getoption("randomly_seed")
class XdistHooks:
# Hooks for xdist only, registered when needed in pytest_configure()
# https://docs.pytest.org/en/latest/writing_plugins.html#optionally-using-hooks-from-3rd-party-plugins

def pytest_configure_node(self, node):
node.workerinput["randomly_seed"] = node.config.getoption("randomly_seed")


random_states = {}
Expand Down
7 changes: 6 additions & 1 deletion tests/test_pytest_randomly.py
Expand Up @@ -670,6 +670,11 @@ def fake_entry_points():
testdir.runpytest("--randomly-seed=1")


def test_works_without_xdist(simpletestdir):
out = simpletestdir.runpytest("-p", "no:xdist")
out.assert_outcomes(passed=1)


@pytest.mark.parametrize("n", list(range(5)))
def test_xdist(n, ourtestdir):
"""
Expand All @@ -685,7 +690,7 @@ def test_xdist(n, ourtestdir):
test_six="def test_a(): pass",
)

out = ourtestdir.runpytest("-n 6", "-v", "--dist=loadfile")
out = ourtestdir.runpytest("-n", "6", "-v", "--dist=loadfile")
out.assert_outcomes(passed=6)

# Can't make any assertion on the order, since output comes back from
Expand Down

0 comments on commit ea78e3f

Please sign in to comment.