@@ -32,6 +32,12 @@ def __init__(self, py_test_service,
3232
3333 @pytest .hookimpl (hookwrapper = True )
3434 def pytest_runtest_protocol (self , item ):
35+ # Adding issues id marks to the test item
36+ # if client doesn't support item updates
37+ update_supported = self .PyTestService .is_item_update_supported ()
38+ if not update_supported :
39+ self ._add_issue_id_marks (item )
40+
3541 self .PyTestService .start_pytest_item (item )
3642 if PYTEST_HAS_LOGGING_PLUGIN :
3743 # This check can go away once we support pytest >= 3.3
@@ -41,6 +47,12 @@ def pytest_runtest_protocol(self, item):
4147 yield
4248 else :
4349 yield
50+ # Updating item in RP (tags and description)
51+ # if client supports
52+ if update_supported :
53+ self ._add_issue_id_marks (item )
54+ self .PyTestService .update_pytest_item (item )
55+ # Finishing item in RP
4456 self .PyTestService .finish_pytest_item (item , self .result or 'SKIPPED' , self .issue or None )
4557
4658 @pytest .hookimpl (hookwrapper = True )
@@ -53,67 +65,88 @@ def pytest_runtest_makereport(self, item):
5365 loglevel = 'ERROR' ,
5466 )
5567
68+ # Defining test result
5669 if report .when == 'setup' :
5770 self .result = None
5871 self .issue = {}
59- if report .failed :
60- # This happens for example when a fixture fails to run
61- # causing the test to error
62- self .result = 'FAILED'
63- self ._add_issue_info (item , report )
64- elif report .skipped :
65- # This happens when a testcase is marked "skip". It will
66- # show in reportportal as not requiring investigation.
72+
73+ if report .failed :
74+ self .result = 'FAILED'
75+ elif report .skipped :
76+ if self .result in (None , 'PASSED' ):
6777 self .result = 'SKIPPED'
68- self ._add_issue_info (item , report )
78+ else :
79+ if self .result is None :
80+ self .result = 'PASSED'
6981
70- if report .when == 'call' :
71- if report .passed :
72- item_result = 'PASSED'
73- elif report .skipped :
74- item_result = 'SKIPPED'
75- self ._add_issue_info (item , report )
76- else :
77- item_result = 'FAILED'
78- self ._add_issue_info (item , report )
79- self .result = item_result
82+ # Adding test comment and issue type
83+ self ._add_issue_info (item , report )
8084
85+ def _add_issue_id_marks (self , item ):
86+ """Add marks with issue id.
87+
88+ :param item: pytest test item
89+ """
90+ issue_marks = item .session .config .getini ('rp_issue_marks' )
91+ if item .session .config .getini ('rp_issue_id_marks' ):
92+ for mark_name in issue_marks :
93+ for mark in item .iter_markers (name = mark_name ):
94+ if mark :
95+ issue_ids = mark .kwargs .get ("issue_id" , [])
96+ if not isinstance (issue_ids , list ):
97+ issue_ids = [issue_ids ]
98+ for issue_id in issue_ids :
99+ mark_issue = "{}:{}" .format (mark .name , issue_id )
100+ try :
101+ pytest .mark ._markers .add (mark_issue ) # register mark in pytest,
102+ except AttributeError : # for pytest >= 4.5.0
103+ pass
104+ item .add_marker (mark_issue )
81105
82106 def _add_issue_info (self , item , report ):
107+ """Add issues description and issue_type to the test item.
83108
84- issue_type = None
85- comment = ""
109+ :param item: pytest test item
110+ :param report: pytest report instance
111+ """
86112 url = item .session .config .getini ('rp_issue_system_url' )
87113 issue_marks = item .session .config .getini ('rp_issue_marks' )
114+ issue_type = None
115+ comment = ""
88116
89117 for mark_name in issue_marks :
90- try :
91- mark = item .get_closest_marker (mark_name )
92- except AttributeError :
93- # pytest < 3.6
94- mark = item .get_marker (mark_name )
95-
96- if mark :
97- if "reason" in mark .kwargs :
98- comment += "\n " if comment else ""
99- comment += mark .kwargs ["reason" ]
100- if "issue_id" in mark .kwargs :
101- issue_ids = mark .kwargs ["issue_id" ]
102- if not isinstance (issue_ids , list ):
103- issue_ids = [issue_ids ]
104- comment += "\n " if comment else ""
105- comment += "Issues:"
118+ for mark in item .iter_markers (name = mark_name ):
119+ if not mark :
120+ continue
121+
122+ mark_comment = ""
123+ mark_url = mark .kwargs .get ("url" , None ) or url
124+ issue_ids = mark .kwargs .get ("issue_id" , [])
125+ if not isinstance (issue_ids , list ):
126+ issue_ids = [issue_ids ]
127+
128+ if issue_ids :
129+ mark_comment = mark .kwargs .get ("reason" , mark .name )
130+ mark_comment += ":"
106131 for issue_id in issue_ids :
107- template = (" [{issue_id}]" + "({})" .format (url )) if url else " {issue_id}"
108- comment += template .format (issue_id = issue_id )
132+ issue_url = mark_url .format (issue_id = issue_id ) if mark_url else None
133+ template = " [{issue_id}]({url})" if issue_url else " {issue_id}"
134+ mark_comment += template .format (issue_id = issue_id , url = issue_url )
135+ elif "reason" in mark .kwargs :
136+ mark_comment = mark .kwargs ["reason" ]
137+
138+ if mark_comment :
139+ comment += ("\n * " if comment else "* " ) + mark_comment
109140
110- if "issue_type" in mark .kwargs :
141+ # Set issue_type only for first issue mark
142+ if "issue_type" in mark .kwargs and issue_type is None :
111143 issue_type = mark .kwargs ["issue_type" ]
112144
145+ # default value
113146 issue_type = "TI" if issue_type is None else issue_type
114147
115- if issue_type and getattr ( self . PyTestService , 'issue_types' , False ) \
116- and (issue_type in self .PyTestService . issue_types ):
148+ if issue_type and \
149+ (issue_type in getattr ( self .PyTestService , ' issue_types' , ()) ):
117150 if comment :
118151 self .issue ['comment' ] = comment
119152 self .issue ['issue_type' ] = self .PyTestService .issue_types [issue_type ]
0 commit comments