Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix when xdist not installed or active #248

Merged
merged 1 commit into from Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 # noqa: B950

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