22# and/or modify it under the terms of the GPL licence
33
44import logging
5-
6- from .service import PyTestService
5+ import dill as pickle
6+ import pytest
7+ from .service import PyTestServiceClass
78from .listener import RPReportListener
89
910try :
1415 PYTEST_HAS_LOGGING_PLUGIN = False
1516
1617
18+ def is_master (config ):
19+ """
20+ True if the code running the given pytest.config object is running in a xdist master
21+ node or not running xdist at all.
22+ """
23+ return not hasattr (config , 'slaveinput' )
24+
25+
26+ @pytest .mark .optionalhook
27+ def pytest_configure_node (node ):
28+ node .slaveinput ['py_test_service' ] = pickle .dumps (node .config .py_test_service )
29+
30+
1731def pytest_sessionstart (session ):
1832 if session .config .getoption ('--collect-only' , default = False ) is True :
1933 return
2034
21- PyTestService .init_service (
22- project = session .config .getini ('rp_project' ),
23- endpoint = session .config .getini ('rp_endpoint' ),
24- uuid = session .config .getini ('rp_uuid' ),
25- log_batch_size = int (session .config .getini ('rp_log_batch_size' )),
26- ignore_errors = bool (session .config .getini ('rp_ignore_errors' )),
27- ignored_tags = session .config .getini ('rp_ignore_tags' ),
28- )
35+ if is_master (session .config ):
36+ session .config .py_test_service .init_service (
37+ project = session .config .getini ('rp_project' ),
38+ endpoint = session .config .getini ('rp_endpoint' ),
39+ uuid = session .config .getini ('rp_uuid' ),
40+ log_batch_size = int (session .config .getini ('rp_log_batch_size' )),
41+ ignore_errors = bool (session .config .getini ('rp_ignore_errors' )),
42+ ignored_tags = session .config .getini ('rp_ignore_tags' ),
43+ )
2944
30- PyTestService .start_launch (
31- session .config .option .rp_launch ,
32- tags = session .config .getini ('rp_launch_tags' ),
33- description = session .config .option . rp_launch_description ,
34- )
45+ session . config . py_test_service .start_launch (
46+ session .config .option .rp_launch ,
47+ tags = session .config .getini ('rp_launch_tags' ),
48+ description = session .config .getini ( ' rp_launch_description' ) ,
49+ )
3550
3651
3752def pytest_sessionfinish (session ):
@@ -40,7 +55,8 @@ def pytest_sessionfinish(session):
4055
4156 # FixMe: currently method of RP api takes the string parameter
4257 # so it is hardcoded
43- PyTestService .finish_launch (status = 'RP_Launch' )
58+ if is_master (session .config ):
59+ session .config .py_test_service .finish_launch (status = 'RP_Launch' )
4460
4561
4662def pytest_configure (config ):
@@ -49,30 +65,33 @@ def pytest_configure(config):
4965 if not config .option .rp_launch_description :
5066 config .option .rp_launch_description = config .getini ('rp_launch_description' )
5167
52- if config .pluginmanager .hasplugin ('xdist' ):
53- raise Exception (
54- "pytest report portal is not compatible with 'xdist' plugin." )
68+ if is_master (config ):
69+ config .py_test_service = PyTestServiceClass ()
70+ else :
71+ config .py_test_service = pickle .loads (config .slaveinput ['py_test_service' ])
72+ config .py_test_service .RP .listener .start ()
5573
5674 # set Pytest_Reporter and configure it
5775
5876 if PYTEST_HAS_LOGGING_PLUGIN :
5977 # This check can go away once we support pytest >= 3.3
6078 try :
6179 config ._reporter = RPReportListener (
80+ config .py_test_service ,
6281 _pytest .logging .get_actual_log_level (config , 'rp_log_level' )
6382 )
6483 except TypeError :
6584 # No log level set either in INI or CLI
66- config ._reporter = RPReportListener ()
85+ config ._reporter = RPReportListener (config . py_test_service )
6786 else :
68- config ._reporter = RPReportListener ()
87+ config ._reporter = RPReportListener (config . py_test_service )
6988
7089 if hasattr (config , '_reporter' ):
7190 config .pluginmanager .register (config ._reporter )
7291
7392
7493def pytest_unconfigure (config ):
75- PyTestService .terminate_service ()
94+ config . py_test_service .terminate_service ()
7695
7796 if hasattr (config , '_reporter' ):
7897 reporter = config ._reporter
0 commit comments