Skip to content

Commit 0bf36d7

Browse files
authored
New client implementation
1 parent e9eaceb commit 0bf36d7

File tree

7 files changed

+230
-185
lines changed

7 files changed

+230
-185
lines changed

reportportal_client/client.py

Lines changed: 151 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@
1515
limitations under the License.
1616
"""
1717
import logging
18-
1918
import requests
2019
from requests.adapters import HTTPAdapter
2120

2221
from reportportal_client.core.log_manager import LogManager
23-
from reportportal_client.core.test_manager import TestManager
2422
from reportportal_client.core.rp_requests import (
2523
HttpRequest,
24+
ItemStartRequest,
25+
ItemFinishRequest,
2626
LaunchStartRequest,
2727
LaunchFinishRequest
2828
)
29-
from reportportal_client.helpers import uri_join
29+
from reportportal_client.helpers import uri_join, verify_value_length
3030

3131
logger = logging.getLogger(__name__)
3232
logger.addHandler(logging.NullHandler())
@@ -83,8 +83,6 @@ def __init__(self,
8383
self._log_manager = LogManager(
8484
self.endpoint, self.session, self.api_v2, self.launch_id,
8585
self.project, log_batch_size=log_batch_size)
86-
self._test_manager = TestManager(
87-
self.session, self.endpoint, project, self.launch_id)
8886

8987
def finish_launch(self,
9088
end_time,
@@ -101,14 +99,15 @@ def finish_launch(self,
10199
"""
102100
url = uri_join(self.base_url_v2, 'launch', self.launch_id, 'finish')
103101
request_payload = LaunchFinishRequest(
104-
end_time=end_time,
102+
end_time,
105103
status=status,
106104
attributes=attributes,
107-
**kwargs
105+
description=kwargs.get('description')
108106
).payload
109107
response = HttpRequest(self.session.put, url=url, json=request_payload,
110108
verify_ssl=self.verify_ssl).make()
111109
logger.debug('finish_launch - ID: %s', self.launch_id)
110+
logger.debug('response message: %s', response.message)
112111
return response.message
113112

114113
def finish_test_item(self,
@@ -117,33 +116,100 @@ def finish_test_item(self,
117116
status,
118117
issue=None,
119118
attributes=None,
119+
description=None,
120+
retry=False,
120121
**kwargs):
121122
"""Finish suite/case/step/nested step item.
122123
123-
:param item_id: id of the test item
124-
:param end_time: time in UTC format
125-
:param status: status of the test
126-
:param issue: description of an issue
127-
:param attributes: list of attributes
128-
:param kwargs: other parameters
129-
:return: json message
124+
:param item_id: ID of the test item
125+
:param end_time: Test item end time
126+
:param status: Test status. Allowable values: "passed",
127+
"failed", "stopped", "skipped", "interrupted",
128+
"cancelled"
129+
:param attributes: Test item attributes(tags). Pairs of key and value.
130+
Overrides attributes on start
131+
:param description: Test item description. Overrides description
132+
from start request.
133+
:param issue: Issue of the current test item
134+
:param retry: Used to report retry of the test. Allowable values:
135+
"True" or "False"
136+
"""
137+
url = uri_join(self.base_url_v2, 'item', item_id)
138+
request_payload = ItemFinishRequest(
139+
end_time,
140+
self.launch_id,
141+
status,
142+
attributes=attributes,
143+
description=description,
144+
is_skipped_an_issue=self.is_skipped_an_issue,
145+
issue=issue,
146+
retry=retry
147+
).payload
148+
response = HttpRequest(self.session.put, url=url, json=request_payload,
149+
verify_ssl=self.verify_ssl).make()
150+
logger.debug('finish_test_item - ID: %s', item_id)
151+
logger.debug('response message: %s', response.message)
152+
return response.message
153+
154+
def get_item_id_by_uuid(self, uuid):
155+
"""Get test item ID by the given UUID.
156+
157+
:param uuid: UUID returned on the item start
158+
:return: Test item ID
159+
"""
160+
url = uri_join(self.base_url_v1, 'item', 'uuid', uuid)
161+
response = HttpRequest(self.session.get, url=url,
162+
verify_ssl=self.verify_ssl).make()
163+
return response.id
164+
165+
def get_launch_info(self):
166+
"""Get the current launch information.
167+
168+
:return dict: Launch information in dictionary
169+
"""
170+
if self.launch_id is None:
171+
return {}
172+
url = uri_join(self.base_url_v1, 'launch', 'uuid', self.launch_id)
173+
logger.debug('get_launch_info - ID: %s', self.launch_id)
174+
response = HttpRequest(self.session.get, url=url,
175+
verify_ssl=self.verify_ssl).make()
176+
if response.is_success:
177+
launch_info = response.json
178+
logger.debug(
179+
'get_launch_info - Launch info: %s', response.json)
180+
else:
181+
logger.warning('get_launch_info - Launch info: '
182+
'Failed to fetch launch ID from the API.')
183+
launch_info = {}
184+
return launch_info
185+
186+
def get_launch_ui_id(self):
187+
"""Get UI ID of the current launch.
188+
189+
:return: UI ID of the given launch. None if UI ID has not been found.
190+
"""
191+
return self.get_launch_info().get('id')
192+
193+
def get_launch_ui_url(self):
194+
"""Get UI URL of the current launch.
195+
196+
:return: launch URL or all launches URL.
130197
"""
131-
self._test_manager.finish_test_item(self.api_v2,
132-
item_id,
133-
end_time,
134-
status,
135-
issue=issue,
136-
attributes=attributes,
137-
**kwargs)
198+
ui_id = self.get_launch_ui_id() or ''
199+
path = 'ui/#{0}/launches/all/{1}'.format(self.project, ui_id)
200+
url = uri_join(self.endpoint, path)
201+
logger.debug('get_launch_ui_url - ID: %s', self.launch_id)
202+
return url
138203

139204
def get_project_settings(self):
140-
"""Get settings from project.
205+
"""Get project settings.
141206
142-
:return: json body
207+
:return: HTTP response in dictionary
143208
"""
144209
url = uri_join(self.base_url_v1, 'settings')
145-
r = self.session.get(url=url, json={}, verify=self.verify_ssl)
146-
return r.json()
210+
response = HttpRequest(self.session.get, url=url, json={},
211+
verify_ssl=self.verify_ssl).make()
212+
return response.json
147213

148214
def log(self, time, message, level=None, attachment=None, item_id=None):
149215
"""Send log message to the Report Portal.
@@ -168,8 +234,7 @@ def start_launch(self,
168234
mode=None,
169235
rerun=False,
170236
rerun_of=None,
171-
**kwargs
172-
):
237+
**kwargs):
173238
"""Start a new launch with the given parameters.
174239
175240
:param name: Launch name
@@ -189,14 +254,14 @@ def start_launch(self,
189254
description=description,
190255
mode=mode,
191256
rerun=rerun,
192-
rerun_of=rerun_of,
257+
rerun_of=rerun_of or kwargs.get('rerunOf'),
193258
**kwargs
194259
).payload
195260
response = HttpRequest(self.session.post,
196261
url=url,
197262
json=request_payload,
198263
verify_ssl=self.verify_ssl).make()
199-
self._test_manager.launch_id = self.launch_id = response.id
264+
self._log_manager.launch_id = self.launch_id = response.id
200265
logger.debug('start_launch - ID: %s', self.launch_id)
201266
return self.launch_id
202267

@@ -210,31 +275,68 @@ def start_test_item(self,
210275
parent_item_id=None,
211276
has_stats=True,
212277
code_ref=None,
278+
retry=False,
213279
**kwargs):
214280
"""Start case/step/nested step item.
215281
216-
:param name: Name of test item
217-
:param start_time: Test item start time
218-
:param item_type: Type of test item
219-
:param description: Test item description
220-
:param attributes: Test item attributes
221-
:param parameters: Test item parameters
222-
:param parent_item_id: Parent test item UUID
223-
:param has_stats: Does test item has stats or not
224-
:param code_ref: Test item code reference
282+
:param name: Name of the test item
283+
:param start_time: Test item start time
284+
:param item_type: Type of the test item. Allowable values: "suite",
285+
"story", "test", "scenario", "step",
286+
"before_class", "before_groups", "before_method",
287+
"before_suite", "before_test", "after_class",
288+
"after_groups", "after_method", "after_suite",
289+
"after_test"
290+
:param attributes: Test item attributes
291+
:param code_ref: Physical location of the test item
292+
:param description: Test item description
293+
:param has_stats: Set to False if test item is nested step
294+
:param parameters: Set of parameters (for parametrized test items)
295+
:param retry: Used to report retry of the test. Allowable values:
296+
"True" or "False"
225297
"""
226-
return self._test_manager.start_test_item(self.api_v2,
227-
name,
228-
start_time,
229-
item_type,
230-
description=description,
231-
attributes=attributes,
232-
parameters=parameters,
233-
parent_uuid=parent_item_id,
234-
has_stats=has_stats,
235-
code_ref=code_ref,
236-
**kwargs)
298+
if parent_item_id:
299+
url = uri_join(self.base_url_v2, 'item', parent_item_id)
300+
else:
301+
url = uri_join(self.base_url_v2, 'item')
302+
request_payload = ItemStartRequest(
303+
name,
304+
start_time,
305+
item_type,
306+
self.launch_id,
307+
attributes=verify_value_length(attributes),
308+
code_ref=code_ref,
309+
description=description,
310+
has_stats=has_stats,
311+
parameters=parameters,
312+
retry=retry
313+
).payload
314+
response = HttpRequest(self.session.post,
315+
url=url,
316+
json=request_payload,
317+
verify_ssl=self.verify_ssl).make()
318+
logger.debug('start_test_item - ID: %s', response.id)
319+
return response.id
237320

238321
def terminate(self, *args, **kwargs):
239322
"""Call this to terminate the client."""
240323
self._log_manager.stop()
324+
325+
def update_test_item(self, item_uuid, attributes=None, description=None):
326+
"""Update existing test item at the Report Portal.
327+
328+
:param str item_uuid: Test item UUID returned on the item start
329+
:param str description: Test item description
330+
:param list attributes: Test item attributes
331+
[{'key': 'k_name', 'value': 'k_value'}, ...]
332+
"""
333+
data = {
334+
'description': description,
335+
'attributes': verify_value_length(attributes),
336+
}
337+
item_id = self.get_item_id_by_uuid(item_uuid)
338+
url = uri_join(self.base_url_v1, 'item', item_id, 'update')
339+
response = HttpRequest(self.session.put, url=url, json=data,
340+
verify_ssl=self.verify_ssl).make()
341+
logger.debug('update_test_item - Item: %s', item_id)
342+
return response.message

reportportal_client/client.pyi

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
from requests import Session
22
from typing import Any, Dict, List, Optional, Text, Tuple, Union
33
from reportportal_client.core.log_manager import LogManager as LogManager
4-
from reportportal_client.core.test_manager import TestManager as TestManager
4+
from reportportal_client.core.rp_issues import Issue as Issue
55

66
class RPClient:
77
_log_manager: LogManager = ...
8-
_test_manager: TestManager = ...
98
api_v1: Text = ...
109
api_v2: Text = ...
1110
base_url_v1: Text = ...
@@ -30,15 +29,19 @@ class RPClient:
3029
def finish_launch(self,
3130
end_time: Text,
3231
status: Text = ...,
33-
attributes: List = ...,
32+
attributes: Optional[Union[List, Dict]] = ...,
3433
**kwargs: Any) -> Dict: ...
3534
def finish_test_item(self,
3635
item_id: Text,
3736
end_time: Text,
3837
status: Text,
39-
issue: Text = ...,
38+
issue: Optional[Issue] = ...,
4039
attributes: List = ...,
4140
**kwargs: Any) -> None: ...
41+
def get_item_id_by_uuid(self, uuid: Text) -> Text: ...
42+
def get_launch_info(self) -> Dict: ...
43+
def get_launch_ui_id(self) -> Optional[Dict]: ...
44+
def get_launch_ui_url(self) -> Text: ...
4245
def get_project_settings(self) -> Dict: ...
4346
def log(self,
4447
time: Text,
@@ -50,7 +53,7 @@ class RPClient:
5053
name: Text,
5154
start_time: Text,
5255
description: Text = ...,
53-
attributes: List = ...,
56+
attributes: Optional[Union[List, Dict]] = ...,
5457
mode: Text = ...,
5558
rerun: bool = ...,
5659
rerun_of: Text = ...,
@@ -60,10 +63,11 @@ class RPClient:
6063
start_time: Text,
6164
item_type: Text,
6265
description: Text = ...,
63-
attributes: List = ...,
64-
parameters: dict = ...,
66+
attributes: Optional[Union[List, Dict]] = ...,
67+
parameters: Dict = ...,
6568
parent_item_id: Text = ...,
6669
has_stats: bool = ...,
6770
code_ref: Text = ...,
6871
**kwargs: Any) -> Text: ...
6972
def terminate(self, *args: Tuple, **kwargs: Any) -> None: ...
73+
def update_test_item(self, item_uuid: Text, attributes: Optional[Union[List, Dict]], description: Optional[Text]):

0 commit comments

Comments
 (0)