forked from reportportal/client-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_steps.py
More file actions
210 lines (157 loc) · 6.65 KB
/
Copy pathtest_steps.py
File metadata and controls
210 lines (157 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# Copyright (c) 2022 https://reportportal.io .
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License
import random
import time
from unittest import mock
from reportportal_client import step
# noinspection PyProtectedMember
from reportportal_client._internal.local import set_current
NESTED_STEP_NAME = 'test nested step'
PARENT_STEP_ID = '123-123-1234-123'
NESTED_STEP_ID = '321-321-4321-321'
def test_nested_steps_are_skipped_without_parent(rp_client):
with step(NESTED_STEP_NAME):
pass
assert rp_client.session.post.call_count == 0
assert rp_client.session.put.call_count == 0
def test_nested_steps_reported_with_parent(rp_client):
rp_client._add_current_item(PARENT_STEP_ID)
with step(NESTED_STEP_NAME):
pass
assert rp_client.session.post.call_count == 1
assert rp_client.session.put.call_count == 1
def test_nested_steps_are_skipped_without_client(rp_client):
rp_client._add_current_item(PARENT_STEP_ID)
set_current(None)
with step(NESTED_STEP_NAME):
pass
assert rp_client.session.post.call_count == 0
assert rp_client.session.put.call_count == 0
def test_nested_step_name(rp_client):
rp_client._add_current_item(PARENT_STEP_ID)
with step(NESTED_STEP_NAME):
pass
assert rp_client.session.post.call_args[1]['json']['name'] == \
NESTED_STEP_NAME
def test_nested_step_times(rp_client):
rp_client._add_current_item(PARENT_STEP_ID)
with step(NESTED_STEP_NAME):
pass
assert rp_client.session.post.call_args[1]['json']['startTime']
assert rp_client.session.put.call_args[1]['json']['endTime']
@step
def nested_step():
pass
def test_nested_step_decorator(rp_client):
rp_client._add_current_item(PARENT_STEP_ID)
nested_step()
assert rp_client.session.post.call_count == 1
assert rp_client.session.put.call_count == 1
assert len(rp_client._log_batcher._batch) == 0
def test_nested_step_failed(rp_client):
rp_client._add_current_item(PARENT_STEP_ID)
try:
with step(NESTED_STEP_NAME):
raise AssertionError
except AssertionError:
pass
assert rp_client.session.post.call_count == 1
assert rp_client.session.put.call_count == 1
assert rp_client.session.put.call_args[1]['json']['status'] == 'FAILED'
def test_nested_step_custom_status(rp_client):
rp_client._add_current_item(PARENT_STEP_ID)
with step(NESTED_STEP_NAME, status='INFO'):
pass
assert rp_client.session.post.call_count == 1
assert rp_client.session.put.call_count == 1
assert rp_client.session.put.call_args[1]['json']['status'] == 'INFO'
def test_nested_step_custom_status_failed(rp_client):
rp_client._add_current_item(PARENT_STEP_ID)
try:
with step(NESTED_STEP_NAME, status='INFO'):
raise AssertionError
except AssertionError:
pass
assert rp_client.session.post.call_count == 1
assert rp_client.session.put.call_count == 1
assert rp_client.session.put.call_args[1]['json']['status'] == 'FAILED'
def item_id_gen(*args, **kwargs):
item_id = 'post-{}-{}'.format(
str(round(time.time() * 1000)),
random.randint(0, 9999))
result = mock.Mock()
result.text = '{{"id": "{}"}}'.format(item_id)
result.json = lambda: {'id': item_id}
return result
@step
def nested_step_params(param1, param2, param3=None):
pass
def test_verify_parameters_logging_default_value(rp_client):
rp_client.session.post.side_effect = item_id_gen
rp_client._add_current_item(PARENT_STEP_ID)
nested_step_params(1, 'two')
assert len(rp_client._log_batcher._batch) == 1
assert rp_client._log_batcher._batch[0].message \
== "Parameters: param1: 1; param2: two"
def test_verify_parameters_logging_no_default_value(rp_client):
rp_client.session.post.side_effect = item_id_gen
rp_client._add_current_item(PARENT_STEP_ID)
nested_step_params(1, 'two', 'three')
assert len(rp_client._log_batcher._batch) == 1
assert rp_client._log_batcher._batch[0].message \
== "Parameters: param1: 1; param2: two; param3: three"
def test_verify_parameters_logging_named_value(rp_client):
rp_client.session.post.side_effect = item_id_gen
rp_client._add_current_item(PARENT_STEP_ID)
nested_step_params(1, 'two', param3='three')
assert len(rp_client._log_batcher._batch) == 1
assert rp_client._log_batcher._batch[0].message \
== "Parameters: param1: 1; param2: two; param3: three"
def test_verify_parameters_inline_logging(rp_client):
rp_client.session.post.side_effect = item_id_gen
rp_client._add_current_item(PARENT_STEP_ID)
with step(NESTED_STEP_NAME, params={'param1': 1, 'param2': 'two'}):
pass
assert len(rp_client._log_batcher._batch) == 1
assert rp_client._log_batcher._batch[0].message \
== "Parameters: param1: 1; param2: two"
@step
def parent_nested_step():
nested_step()
def test_two_level_nested_step_decorator(rp_client):
rp_client._add_current_item(PARENT_STEP_ID)
rp_client.session.post.side_effect = item_id_gen
parent_nested_step()
assert rp_client.session.post.call_count == 2
assert rp_client.session.put.call_count == 2
assert len(rp_client._log_batcher._batch) == 0
request_uri = rp_client.session.post.call_args_list[0][0][0]
first_parent_id = request_uri[request_uri.rindex('/') + 1:]
request_uri = rp_client.session.post.call_args_list[1][0][0]
second_parent_id = request_uri[request_uri.rindex('/') + 1:]
request_uri = rp_client.session.put.call_args_list[0][0][0]
first_id = request_uri[request_uri.rindex('/') + 1:]
request_uri = rp_client.session.put.call_args_list[1][0][0]
second_id = request_uri[request_uri.rindex('/') + 1:]
assert first_parent_id == PARENT_STEP_ID
assert second_parent_id.startswith('post-')
assert first_id.startswith('post-')
assert second_id.startswith('post-')
assert first_id != second_id
def test_verify_manual_client_bypass_step(rp_client):
set_current(None)
rp_client._add_current_item(PARENT_STEP_ID)
with step(NESTED_STEP_NAME, rp_client=rp_client):
pass
assert rp_client.session.post.call_count == 1
assert rp_client.session.put.call_count == 1