Skip to content

Commit e74be4d

Browse files
committed
xstress: fix for fixtured tests with a workaround (until xdist PR#1091 is merged)
1 parent 7fd3a4a commit e74be4d

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

pytest_xstress.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import functools
34
from itertools import chain
45

56
import pytest
@@ -191,6 +192,26 @@ def pytest_xdist_make_scheduler(config: pytest.Config, log):
191192
raise ValueError(f'xstress does not support "{dist}" distmode')
192193

193194

195+
@pytest.hookimpl
196+
def pytest_plugin_registered(plugin, manager: pytest.PytestPluginManager):
197+
"""Workaround until PR#1091 in pytest-xdist is merged"""
198+
if type(plugin).__name__ == "WorkerInteractor":
199+
# NOTE: Haven't found a better way to get config at this time
200+
if not plugin.config.getvalue("xstress"):
201+
return
202+
203+
# Switch `nextitem=item` to `nextitem=None` when calling `pytest_runtest_protocol`
204+
func = plugin.config.hook.pytest_runtest_protocol
205+
206+
@functools.wraps(func)
207+
def wrapper(item, nextitem, *args, **kwargs):
208+
if item == nextitem:
209+
nextitem = None
210+
func(item=item, nextitem=nextitem, *args, **kwargs)
211+
212+
plugin.config.hook.pytest_runtest_protocol = wrapper
213+
214+
194215
@pytest.hookimpl()
195216
def pytest_addoption(parser: pytest.Parser):
196217
parser.addoption(

test.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,3 +387,39 @@ def _predicate():
387387
assert (
388388
count >= 25
389389
) # since 200 tests are run, this should always be true mathematically
390+
391+
392+
@pytest.fixture
393+
def regfix():
394+
return 5
395+
396+
397+
@pytest.mark.xdist_group(name="f")
398+
@pytest.mark.regfixture
399+
def _test_regfixture(regfix):
400+
pass
401+
402+
403+
def test_regfixture():
404+
datas = list()
405+
lock = threading.Lock()
406+
407+
def _on_update(x):
408+
with lock:
409+
datas.append(x)
410+
411+
def _predicate():
412+
with lock:
413+
return len(datas) > 10
414+
415+
with run_tests(
416+
_on_update, "regfixture", "-n2", "--dist", "loadgroup", "--xstress"
417+
) as p:
418+
assert wait_until(_predicate)
419+
p.kill()
420+
p.wait()
421+
422+
worker = datas[0]["xdist_worker"]
423+
assert worker
424+
for x in datas:
425+
assert x["result"] == "pass"

0 commit comments

Comments
 (0)