44 from xdist import __version__ as xdist_version
55except 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
1818else :
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