Skip to content

Commit a4debd6

Browse files
authored
Fix when xdist not installed or active (#248)
Fixes #246.
1 parent 98339d3 commit a4debd6

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

HISTORY.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
History
33
-------
44

5+
* Fix to work when pytest-xdist is not installed or active
6+
(``PluginValidationError: unknown hook 'pytest_configure_node'``).
7+
58
3.3.0 (2020-04-15)
69
------------------
710

src/pytest_randomly.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
else:
1111
from importlib_metadata import entry_points
1212

13+
try:
14+
import xdist
15+
except ImportError:
16+
xdist = None
1317

1418
# factory-boy
1519
try:
@@ -88,6 +92,9 @@ def pytest_addoption(parser):
8892

8993

9094
def pytest_configure(config):
95+
if config.pluginmanager.hasplugin("xdist"):
96+
config.pluginmanager.register(XdistHooks())
97+
9198
seed_value = config.getoption("randomly_seed")
9299
if seed_value == "last":
93100
seed = config.cache.get("randomly_seed", default_seed)
@@ -103,11 +110,12 @@ def pytest_configure(config):
103110
config.option.randomly_seed = seed
104111

105112

106-
def pytest_configure_node(node):
107-
"""
108-
pytest-xdist hook. Send the selected seed through to the nodes.
109-
"""
110-
node.workerinput["randomly_seed"] = node.config.getoption("randomly_seed")
113+
class XdistHooks:
114+
# Hooks for xdist only, registered when needed in pytest_configure()
115+
# https://docs.pytest.org/en/latest/writing_plugins.html#optionally-using-hooks-from-3rd-party-plugins # noqa: B950
116+
117+
def pytest_configure_node(self, node):
118+
node.workerinput["randomly_seed"] = node.config.getoption("randomly_seed")
111119

112120

113121
random_states = {}

tests/test_pytest_randomly.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,11 @@ def fake_entry_points():
670670
testdir.runpytest("--randomly-seed=1")
671671

672672

673+
def test_works_without_xdist(simpletestdir):
674+
out = simpletestdir.runpytest("-p", "no:xdist")
675+
out.assert_outcomes(passed=1)
676+
677+
673678
@pytest.mark.parametrize("n", list(range(5)))
674679
def test_xdist(n, ourtestdir):
675680
"""
@@ -685,7 +690,7 @@ def test_xdist(n, ourtestdir):
685690
test_six="def test_a(): pass",
686691
)
687692

688-
out = ourtestdir.runpytest("-n 6", "-v", "--dist=loadfile")
693+
out = ourtestdir.runpytest("-n", "6", "-v", "--dist=loadfile")
689694
out.assert_outcomes(passed=6)
690695

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

0 commit comments

Comments
 (0)