Skip to content

Commit 409822b

Browse files
authored
Handle disabled pytest-xdist gracefully (#178)
* Fix loading the pytest plugin when xdist is disabled Fix the pytest plugin to run xdist-specific logic only when the plugin is actually enabled. This avoids pytest crashing when xdist is installed but explicitly disabled, e.g. via "-p no:xdist". Fixes #177 * Explicitly pass "-p xdist" to the test requiring xdist Explicitly pass "-p xdist" to ensure that the plugin is enabled when running the test requiring it. This fixes test failure when PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 is used. This option is very helpful since other pytest plugins tend to break this one's test suite.
1 parent 3dedf1b commit 409822b

3 files changed

Lines changed: 11 additions & 7 deletions

File tree

CHANGES.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ Changelog
44
10.3 (unreleased)
55
-----------------
66

7-
- Nothing changed yet.
7+
Bug fixes
8+
+++++++++
9+
10+
- Fix crash when pytest-xdist is installed but disabled.
11+
(Thanks to `@mgorny <https://github.com/mgorny>`_ for the PR.)
812

913

1014
10.2 (2021-09-17)

pytest_rerunfailures.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,8 @@ def pytest_configure(config):
316316
"between re-runs.",
317317
)
318318

319-
if HAS_PYTEST_HANDLECRASHITEM:
319+
if config.pluginmanager.hasplugin("xdist") and HAS_PYTEST_HANDLECRASHITEM:
320+
config.pluginmanager.register(XDistHooks())
320321
if is_master(config):
321322
config.failures_db = ServerStatusDB()
322323
else:
@@ -325,13 +326,12 @@ def pytest_configure(config):
325326
config.failures_db = StatusDB() # no-op db
326327

327328

328-
if HAS_PYTEST_HANDLECRASHITEM:
329-
330-
def pytest_configure_node(node):
329+
class XDistHooks:
330+
def pytest_configure_node(self, node):
331331
"""xdist hook"""
332332
node.workerinput["sock_port"] = node.config.failures_db.sock_port
333333

334-
def pytest_handlecrashitem(crashitem, report, sched):
334+
def pytest_handlecrashitem(self, crashitem, report, sched):
335335
"""
336336
Return the crashitem from pending and collection.
337337
"""

test_pytest_rerunfailures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def test_crash():
194194
def test_pass():
195195
pass"""
196196
)
197-
result = testdir.runpytest("-n", "1", "--reruns", "1", "-r", "R")
197+
result = testdir.runpytest("-p", "xdist", "-n", "1", "--reruns", "1", "-r", "R")
198198
assert_outcomes(result, passed=2, rerun=1)
199199

200200

0 commit comments

Comments
 (0)