Skip to content

Commit c499ff9

Browse files
author
Sylvain MARIE
committed
Fixed issue with latest pytest+pytest-xdist versions: pytest_harvest_xdist_worker_dump hook is now called correctly. Fixes #48
1 parent d77767b commit c499ff9

3 files changed

Lines changed: 50 additions & 42 deletions

File tree

pytest_harvest/plugin.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from pytest_harvest.common import HARVEST_PREFIX
1919
from pytest_harvest.results_bags import create_results_bag_fixture
2020
from pytest_harvest.results_session import get_session_synthesis_dct, get_persistable_session_items
21-
from pytest_harvest.xdist_api import _is_xdist_master, _is_xdist_worker, get_xdist_worker_id
21+
from pytest_harvest.xdist_api import is_xdist_master, is_xdist_worker, get_xdist_worker_id
2222

2323

2424
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
@@ -437,8 +437,8 @@ def pytest_configure(config):
437437

438438
@pytest.hookimpl(tryfirst=True)
439439
def pytest_sessionstart(session):
440-
""" This should run first as it creates the temporary filder when run on the xdist master."""
441-
if _is_xdist_master(session):
440+
""" This should run first as it creates the temporary folder when run on the xdist master."""
441+
if is_xdist_master(session):
442442
# perform cleanup
443443
session.config.hook.pytest_harvest_xdist_init()
444444

@@ -449,14 +449,14 @@ def pytest_sessionstart(session):
449449
@pytest.hookimpl(trylast=True)
450450
def pytest_sessionfinish(session):
451451
""" This should run last as it deletes the persisted items when run on the xdist master."""
452-
if _is_xdist_worker(session):
452+
if is_xdist_worker(session):
453453
# persist fixture store and report items in a pickle file with this id
454454
wid = get_xdist_worker_id(session)
455455
session_items = get_persistable_session_items(session)
456456
session.config.hook.pytest_harvest_xdist_worker_dump(worker_id=wid, session_items=session_items,
457457
fixture_store=FIXTURE_STORE)
458458

459-
elif _is_xdist_master(session):
459+
elif is_xdist_master(session):
460460
# final master cleanup
461461
session.config.hook.pytest_harvest_xdist_cleanup()
462462

@@ -471,7 +471,7 @@ def possibly_restore_xdist_workers_structs(session):
471471
:param session:
472472
:return:
473473
"""
474-
if _is_xdist_master(session) and hasattr(FIXTURE_STORE, 'disabled'):
474+
if is_xdist_master(session) and hasattr(FIXTURE_STORE, 'disabled'):
475475
# load saved session items and fixtures
476476
workers_saved_material = session.config.hook.pytest_harvest_xdist_load()
477477

pytest_harvest/results_session.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,11 @@ def __init__(self, item):
582582
self.callspec = _MinimalCallSpec(params=item.callspec.params)
583583

584584
# convert these to simple tuples just in case pytest-cases is around and has messed with fixturenames.
585-
self.funcargnames = tuple(item.funcargnames) # not needed but we can keep it
585+
try:
586+
# This attribute does not seem to exist in latest pytest
587+
self.funcargnames = tuple(item.funcargnames) # not needed but we can keep it
588+
except AttributeError:
589+
pass
586590
self.fixturenames = tuple(item.fixturenames)
587591

588592
# We do not store the session object so everything that depends on it should be retrieved:

pytest_harvest/xdist_api.py

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
from xdist import __version__ as xdist_version
55
except ImportError:
66
# no xdist installed - create a substitute API
7-
def _is_xdist_worker(session):
7+
def is_xdist_worker(session):
88
return False
99

10-
def _is_xdist_master(session):
10+
def is_xdist_master(session):
1111
return False
1212

1313
def is_main_process(session):
@@ -16,45 +16,49 @@ def is_main_process(session):
1616
def get_xdist_worker_id(request_or_session):
1717
return "master" # for consistency with the original method
1818
else:
19-
# if LooseVersion(xdist_version) <= LooseVersion("1.31.0"):
20-
# old xdist with no api
21-
def _is_xdist_worker(request_or_session):
22-
"""Return `True` if this is a xdist worker, `False` otherwise
19+
try:
20+
# our PR has been accepted in pytest-xdist so we can use these in latest versions
21+
from xdist import is_xdist_worker, is_xdist_master, get_xdist_worker_id
22+
except ImportError:
23+
# if LooseVersion(xdist_version) <= LooseVersion("1.31.0"):
24+
# old xdist with no api
25+
def is_xdist_worker(request_or_session):
26+
"""Return `True` if this is a xdist worker, `False` otherwise
2327
24-
:param request_or_session: the `pytest` `request` or `session` object
25-
:return:
26-
"""
27-
return hasattr(request_or_session.config, "workerinput")
28+
:param request_or_session: the `pytest` `request` or `session` object
29+
:return:
30+
"""
31+
return hasattr(request_or_session.config, "workerinput")
2832

29-
def _is_xdist_master(request_or_session):
30-
"""Return `True` if this is the xdist master, `False` otherwise
33+
def is_xdist_master(request_or_session):
34+
"""Return `True` if this is the xdist master, `False` otherwise
3135
32-
Note: this method returns `False` when distribution has not been activated
33-
at all.
36+
Note: this method returns `False` when distribution has not been activated
37+
at all.
3438
35-
:param request_or_session: the `pytest` `request` or `session` object
36-
:return:
37-
"""
38-
return (not _is_xdist_worker(request_or_session)
39-
and getattr(request_or_session.config.option, 'dist', 'no') != "no")
39+
:param request_or_session: the `pytest` `request` or `session` object
40+
:return:
41+
"""
42+
return (not is_xdist_worker(request_or_session)
43+
and getattr(request_or_session.config.option, 'dist', 'no') != "no")
4044

41-
def get_xdist_worker_id(request_or_session):
42-
"""Return the id of the current worker ('gw0', 'gw1', etc) or None
43-
if running on the master node.
44-
45-
:param request_or_session: the `pytest` `request` or `session` object
46-
:return:
47-
"""
48-
if hasattr(request_or_session.config, "workerinput"):
49-
return request_or_session.config.workerinput["workerid"]
50-
else:
51-
# TODO shall we raise an exception if dist is not enabled ?
52-
# i.e. `not is_xdist_master(request_or_session)` ?
53-
return "master"
45+
def get_xdist_worker_id(request_or_session):
46+
"""Return the id of the current worker ('gw0', 'gw1', etc) or None
47+
if running on the master node.
48+
49+
:param request_or_session: the `pytest` `request` or `session` object
50+
:return:
51+
"""
52+
if hasattr(request_or_session.config, "workerinput"):
53+
return request_or_session.config.workerinput["workerid"]
54+
else:
55+
# TODO shall we raise an exception if dist is not enabled ?
56+
# i.e. `not is_xdist_master(request_or_session)` ?
57+
return "master"
5458
# else:
5559
# from xdist import (get_xdist_worker_id as get_xdist_worker_id,
56-
# is_xdist_master as _is_xdist_master,
57-
# is_xdist_worker as _is_xdist_worker)
60+
# is_xdist_master as is_xdist_master,
61+
# is_xdist_worker as is_xdist_worker)
5862

5963
def is_main_process(session):
60-
return not _is_xdist_worker(session)
64+
return not is_xdist_worker(session)

0 commit comments

Comments
 (0)