Skip to content

Commit 2571168

Browse files
committed
Refactoring: item tracking moved to the client
1 parent 675443c commit 2571168

File tree

5 files changed

+63
-73
lines changed

5 files changed

+63
-73
lines changed

reportportal_client/client.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
"""
1717
import logging
1818

19-
2019
import requests
2120
from requests.adapters import HTTPAdapter
2221

22+
from ._local import set_current
2323
from .core.log_manager import LogManager
2424
from .core.rp_requests import (
2525
HttpRequest,
@@ -31,7 +31,6 @@
3131
from .helpers import uri_join, verify_value_length
3232
from .static.defines import NOT_FOUND
3333
from .steps import StepReporter
34-
from ._local import set_current
3534

3635
logger = logging.getLogger(__name__)
3736
logger.addHandler(logging.NullHandler())
@@ -88,6 +87,7 @@ def __init__(self,
8887
self.verify_ssl = verify_ssl
8988
self.session = requests.Session()
9089
self.step_reporter = StepReporter(self)
90+
self._item_stack = []
9191
if retries:
9292
self.session.mount('https://', HTTPAdapter(
9393
max_retries=retries, pool_maxsize=max_pool_size))
@@ -149,6 +149,9 @@ def finish_test_item(self,
149149
:param retry: Used to report retry of the test. Allowable values:
150150
"True" or "False"
151151
"""
152+
if item_id is NOT_FOUND:
153+
logger.warning("Uttempt to finish non-existent item")
154+
return None
152155
url = uri_join(self.base_url_v2, 'item', item_id)
153156
request_payload = ItemFinishRequest(
154157
end_time,
@@ -162,7 +165,7 @@ def finish_test_item(self,
162165
).payload
163166
response = HttpRequest(self.session.put, url=url, json=request_payload,
164167
verify_ssl=self.verify_ssl).make()
165-
self.step_reporter.remove_parent(item_id)
168+
self._item_stack.pop()
166169
logger.debug('finish_test_item - ID: %s', item_id)
167170
logger.debug('response message: %s', response.message)
168171
return response.message
@@ -337,9 +340,9 @@ def start_test_item(self,
337340
json=request_payload,
338341
verify_ssl=self.verify_ssl).make()
339342
item_id = response.id
340-
self.step_reporter.set_parent(item_type, item_id)
341343
if item_id is not NOT_FOUND:
342344
logger.debug('start_test_item - ID: %s', item_id)
345+
self._item_stack.append(item_id)
343346
else:
344347
logger.warning('start_test_item - invalid response: %s',
345348
str(response.json))
@@ -367,3 +370,7 @@ def update_test_item(self, item_uuid, attributes=None, description=None):
367370
verify_ssl=self.verify_ssl).make()
368371
logger.debug('update_test_item - Item: %s', item_id)
369372
return response.message
373+
374+
def current_item(self):
375+
"""Retrieve the last item reported by the client."""
376+
return self._item_stack[-1] if len(self._item_stack) > 0 else None

reportportal_client/client.pyi

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
from requests import Session
21
from typing import Any, Dict, List, Optional, Text, Tuple, Union
2+
3+
from requests import Session
4+
35
from reportportal_client.core.log_manager import LogManager as LogManager
46
from reportportal_client.core.rp_issues import Issue as Issue
57
from reportportal_client.steps import StepReporter
@@ -23,6 +25,7 @@ class RPClient:
2325
verify_ssl: bool = ...
2426
session: Session = ...
2527
step_reporter: StepReporter = ...
28+
2629
def __init__(self,
2730
endpoint: Text,
2831
project: Text, token: Text,
@@ -32,29 +35,38 @@ class RPClient:
3235
retries: int = ...,
3336
max_pool_size: int = ...,
3437
launch_id: Text = ...) -> None: ...
38+
3539
def finish_launch(self,
3640
end_time: Text,
3741
status: Text = ...,
3842
attributes: Optional[Union[List, Dict]] = ...,
3943
**kwargs: Any) -> Dict: ...
44+
4045
def finish_test_item(self,
41-
item_id: Text,
42-
end_time: Text,
43-
status: Text,
44-
issue: Optional[Issue] = ...,
45-
attributes: List = ...,
46-
**kwargs: Any) -> Text: ...
46+
item_id: Text,
47+
end_time: Text,
48+
status: Text,
49+
issue: Optional[Issue] = ...,
50+
attributes: List = ...,
51+
**kwargs: Any) -> Optional[Text]: ...
52+
4753
def get_item_id_by_uuid(self, uuid: Text) -> Text: ...
54+
4855
def get_launch_info(self) -> Dict: ...
56+
4957
def get_launch_ui_id(self) -> Optional[Dict]: ...
58+
5059
def get_launch_ui_url(self) -> Text: ...
60+
5161
def get_project_settings(self) -> Dict: ...
62+
5263
def log(self,
5364
time: Text,
5465
message: Text,
5566
level: Optional[Union[int, Text]] = ...,
5667
attachment: Optional[Dict] = ...,
5768
item_id: Optional[Text] = ...) -> None: ...
69+
5870
def start_launch(self,
5971
name: Text,
6072
start_time: Text,
@@ -64,16 +76,23 @@ class RPClient:
6476
rerun: bool = ...,
6577
rerun_of: Text = ...,
6678
**kwargs: Any) -> Text: ...
79+
6780
def start_test_item(self,
68-
name: Text,
69-
start_time: Text,
70-
item_type: Text,
71-
description: Text = ...,
72-
attributes: Optional[Union[List, Dict]] = ...,
73-
parameters: Dict = ...,
74-
parent_item_id: Text = ...,
75-
has_stats: bool = ...,
76-
code_ref: Text = ...,
77-
**kwargs: Any) -> Text: ...
81+
name: Text,
82+
start_time: Text,
83+
item_type: Text,
84+
description: Text = ...,
85+
attributes: Optional[Union[List, Dict]] = ...,
86+
parameters: Dict = ...,
87+
parent_item_id: Text = ...,
88+
has_stats: bool = ...,
89+
code_ref: Text = ...,
90+
**kwargs: Any) -> Text: ...
91+
7892
def terminate(self, *args: Tuple, **kwargs: Any) -> None: ...
79-
def update_test_item(self, item_uuid: Text, attributes: Optional[Union[List, Dict]], description: Optional[Text]):
93+
94+
def update_test_item(self, item_uuid: Text,
95+
attributes: Optional[Union[List, Dict]],
96+
description: Optional[Text]) -> Text: ...
97+
98+
def current_item(self) -> Text: ...

reportportal_client/steps/__init__.py

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ def test_my_nested_step():
4545

4646
from reportportal_client._local import current
4747
from reportportal_client.helpers import get_function_params, timestamp
48-
from reportportal_client.static.defines import NOT_FOUND
4948

5049
NESTED_STEP_ITEMS = ('step', 'scenario', 'before_class', 'before_groups',
5150
'before_method', 'before_suite', 'before_test',
@@ -63,34 +62,8 @@ def __init__(self, rp_client):
6362
:param rp_client: Report Portal client which will be used to report
6463
steps
6564
"""
66-
self.__levels = []
6765
self.client = rp_client
6866

69-
def set_parent(self, item_type, parent_id):
70-
"""Put an id into parent items queue if it has correct type.
71-
72-
:param item_type: type of the parent item
73-
:param parent_id: ID of the parent item
74-
"""
75-
if parent_id is not NOT_FOUND:
76-
if item_type.lower() in NESTED_STEP_ITEMS:
77-
self.__levels.append(parent_id)
78-
79-
def get_parent(self):
80-
"""Retrieve the last item in the parent queue."""
81-
if len(self.__levels) > 0:
82-
return self.__levels[-1]
83-
84-
def remove_parent(self, parent_id):
85-
"""Remove the last item in the parent queue.
86-
87-
Remove the last item in the parent queue if it's equal to the method's
88-
argument.
89-
:param parent_id: item ID to remove
90-
"""
91-
if len(self.__levels) > 0 and self.__levels[-1] == parent_id:
92-
return self.__levels.pop()
93-
9467
def start_nested_step(self,
9568
name,
9669
start_time,
@@ -102,8 +75,8 @@ def start_nested_step(self,
10275
:param start_time: Nested Step start time
10376
:param parameters: Nested Step parameters
10477
"""
105-
parent_id = self.get_parent()
106-
if parent_id is None:
78+
parent_id = self.client.current_item()
79+
if not parent_id:
10780
return
10881
return self.client.start_test_item(name, start_time, 'step',
10982
has_stats=False,
@@ -121,8 +94,6 @@ def finish_nested_step(self,
12194
:param end_time: Nested Step finish time
12295
:param status: Nested Step finish status
12396
"""
124-
if not self.remove_parent(item_id):
125-
return
12697
return self.client.finish_test_item(item_id, end_time, status=status)
12798

12899

reportportal_client/steps/__init__.pyi

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ class StepReporter:
2121

2222
def __init__(self, rp_client: RPClient) -> None: ...
2323

24-
def set_parent(self, item_type: Text, parent_id: Text) -> None: ...
25-
26-
def get_parent(self) -> Optional[Text]: ...
27-
28-
def remove_parent(self, parent_id: Optional[Text] = ...) \
29-
-> Optional[Text]: ...
30-
3124
def start_nested_step(self,
3225
name: Text,
3326
start_time: Text,

tests/steps/test_steps.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_nested_steps_are_skipped_without_parent(rp_client):
3131

3232

3333
def test_nested_steps_reported_with_parent(rp_client):
34-
rp_client.step_reporter.set_parent('STEP', PARENT_STEP_ID)
34+
rp_client._item_stack.append(PARENT_STEP_ID)
3535

3636
with step(NESTED_STEP_NAME):
3737
pass
@@ -41,7 +41,7 @@ def test_nested_steps_reported_with_parent(rp_client):
4141

4242

4343
def test_nested_steps_are_skipped_without_client(rp_client):
44-
rp_client.step_reporter.set_parent('STEP', PARENT_STEP_ID)
44+
rp_client._item_stack.append(PARENT_STEP_ID)
4545
set_current(None)
4646
with step(NESTED_STEP_NAME):
4747
pass
@@ -51,7 +51,7 @@ def test_nested_steps_are_skipped_without_client(rp_client):
5151

5252

5353
def test_nested_step_name(rp_client):
54-
rp_client.step_reporter.set_parent('STEP', PARENT_STEP_ID)
54+
rp_client._item_stack.append(PARENT_STEP_ID)
5555

5656
with step(NESTED_STEP_NAME):
5757
pass
@@ -61,7 +61,7 @@ def test_nested_step_name(rp_client):
6161

6262

6363
def test_nested_step_times(rp_client):
64-
rp_client.step_reporter.set_parent('STEP', PARENT_STEP_ID)
64+
rp_client._item_stack.append(PARENT_STEP_ID)
6565

6666
with step(NESTED_STEP_NAME):
6767
pass
@@ -76,7 +76,7 @@ def nested_step():
7676

7777

7878
def test_nested_step_decorator(rp_client):
79-
rp_client.step_reporter.set_parent('STEP', PARENT_STEP_ID)
79+
rp_client._item_stack.append(PARENT_STEP_ID)
8080
nested_step()
8181

8282
assert rp_client.session.post.call_count == 1
@@ -85,7 +85,7 @@ def test_nested_step_decorator(rp_client):
8585

8686

8787
def test_nested_step_failed(rp_client):
88-
rp_client.step_reporter.set_parent('STEP', PARENT_STEP_ID)
88+
rp_client._item_stack.append(PARENT_STEP_ID)
8989
try:
9090
with step(NESTED_STEP_NAME):
9191
raise AssertionError
@@ -97,7 +97,7 @@ def test_nested_step_failed(rp_client):
9797

9898

9999
def test_nested_step_custom_status(rp_client):
100-
rp_client.step_reporter.set_parent('STEP', PARENT_STEP_ID)
100+
rp_client._item_stack.append(PARENT_STEP_ID)
101101
with step(NESTED_STEP_NAME, status='INFO'):
102102
pass
103103
assert rp_client.session.post.call_count == 1
@@ -106,7 +106,7 @@ def test_nested_step_custom_status(rp_client):
106106

107107

108108
def test_nested_step_custom_status_failed(rp_client):
109-
rp_client.step_reporter.set_parent('STEP', PARENT_STEP_ID)
109+
rp_client._item_stack.append(PARENT_STEP_ID)
110110
try:
111111
with step(NESTED_STEP_NAME, status='INFO'):
112112
raise AssertionError
@@ -123,31 +123,31 @@ def nested_step_params(param1, param2, param3=None):
123123

124124

125125
def test_verify_parameters_logging_default_value(rp_client):
126-
rp_client.step_reporter.set_parent('STEP', PARENT_STEP_ID)
126+
rp_client._item_stack.append(PARENT_STEP_ID)
127127
nested_step_params(1, 'two')
128128
assert len(rp_client._log_manager._logs_batch) == 1
129129
assert rp_client._log_manager._logs_batch[0].message \
130130
== "Parameters: param1: 1; param2: two"
131131

132132

133133
def test_verify_parameters_logging_no_default_value(rp_client):
134-
rp_client.step_reporter.set_parent('STEP', PARENT_STEP_ID)
134+
rp_client._item_stack.append(PARENT_STEP_ID)
135135
nested_step_params(1, 'two', 'three')
136136
assert len(rp_client._log_manager._logs_batch) == 1
137137
assert rp_client._log_manager._logs_batch[0].message \
138138
== "Parameters: param1: 1; param2: two; param3: three"
139139

140140

141141
def test_verify_parameters_logging_named_value(rp_client):
142-
rp_client.step_reporter.set_parent('STEP', PARENT_STEP_ID)
142+
rp_client._item_stack.append(PARENT_STEP_ID)
143143
nested_step_params(1, 'two', param3='three')
144144
assert len(rp_client._log_manager._logs_batch) == 1
145145
assert rp_client._log_manager._logs_batch[0].message \
146146
== "Parameters: param1: 1; param2: two; param3: three"
147147

148148

149149
def test_verify_parameters_inline_logging(rp_client):
150-
rp_client.step_reporter.set_parent('STEP', PARENT_STEP_ID)
150+
rp_client._item_stack.append(PARENT_STEP_ID)
151151
with step(NESTED_STEP_NAME, params={'param1': 1, 'param2': 'two'}):
152152
pass
153153
assert len(rp_client._log_manager._logs_batch) == 1
@@ -171,7 +171,7 @@ def parent_nested_step():
171171

172172

173173
def test_two_level_nested_step_decorator(rp_client):
174-
rp_client.step_reporter.set_parent('STEP', PARENT_STEP_ID)
174+
rp_client._item_stack.append(PARENT_STEP_ID)
175175
rp_client.session.post.side_effect = item_id_gen
176176
parent_nested_step()
177177

0 commit comments

Comments
 (0)