Skip to content

Commit e5da922

Browse files
committed
add unit test
1 parent 1d0ab2c commit e5da922

File tree

5 files changed

+74
-67
lines changed

5 files changed

+74
-67
lines changed

bzt/modules/_apiritif/generator.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1937,7 +1937,7 @@ def _gen_actionid_exception_wrapped_body(self, body):
19371937
prefixed_error = ast.BinOp(
19381938
left=ast.BinOp(
19391939
left=ast.BinOp(
1940-
left=ast.Constant(" actionId: ", kind=""),
1940+
left=ast.Constant("actionId: ", kind=""),
19411941
op=ast.Add(),
19421942
right=ast.Call(
19431943
func=ast.Name(id="str", ctx=ast.Load()),
@@ -1970,6 +1970,32 @@ def _gen_actionid_exception_wrapped_body(self, body):
19701970
)
19711971
)
19721972

1973+
if len(body) == 1 and isinstance(body[0], ast.With):
1974+
with_stmt = body[0]
1975+
wrapped_try = ast.Try(
1976+
body=with_stmt.body,
1977+
handlers=[ast.ExceptHandler(
1978+
type=ast.Name(id="Exception", ctx=ast.Load()),
1979+
name="exc",
1980+
body=[ast.Raise(
1981+
exc=ast.Call(
1982+
func=ast.Call(
1983+
func=ast.Name(id="type", ctx=ast.Load()),
1984+
args=[ast.Name(id="exc", ctx=ast.Load())],
1985+
keywords=[]
1986+
),
1987+
args=[final_error],
1988+
keywords=[]
1989+
),
1990+
cause=ast.Name(id="exc", ctx=ast.Load())
1991+
)]
1992+
)],
1993+
orelse=[],
1994+
finalbody=[]
1995+
)
1996+
with_stmt.body = [wrapped_try]
1997+
return [with_stmt]
1998+
19731999
wrapped_try = ast.Try(
19742000
body=body,
19752001
handlers=[ast.ExceptHandler(
@@ -1992,11 +2018,6 @@ def _gen_actionid_exception_wrapped_body(self, body):
19922018
finalbody=[]
19932019
)
19942020

1995-
if len(body) == 1 and isinstance(body[0], ast.With):
1996-
with_stmt = body[0]
1997-
with_stmt.body = [wrapped_try]
1998-
return [with_stmt]
1999-
20002021
return [wrapped_try]
20012022

20022023
def _gen_master_test_method(self, try_block, finally_block):

bzt/modules/aggregator.py

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ def __deepcopy__(self, memo):
249249
def error_item_skel(
250250
error: str, ret_c: str, cnt: int, err_type: int,
251251
urls: Counter, tag: str, err_resp_data: Optional[ErrorResponseData] = None) -> dict:
252-
# urls: Counter, tag: str, err_resp_data: Optional[ErrorResponseData] = None, action_id: str = None) -> dict:
253252
assert isinstance(urls, collections.Counter)
254253
response_bodies = KPISet._get_response_bodies(err_resp_data)
255254
return {
@@ -261,18 +260,6 @@ def error_item_skel(
261260
"urls": urls,
262261
"responseBodies": response_bodies
263262
}
264-
# item = {
265-
# "cnt": cnt,
266-
# "msg": error,
267-
# "tag": tag, # just one more string qualifier
268-
# "rc": ret_c,
269-
# "type": err_type,
270-
# "urls": urls,
271-
# "responseBodies": response_bodies
272-
# }
273-
# if action_id is not None:
274-
# item["action_id"] = action_id
275-
# return item
276263

277264
@staticmethod
278265
def _get_response_bodies(err_resp_data: Optional[ErrorResponseData]) -> list:
@@ -290,16 +277,10 @@ def _get_response_bodies(err_resp_data: Optional[ErrorResponseData]) -> list:
290277
def add_sample(self, sample):
291278
"""
292279
Add sample, consisting of: cnc, rt, cn, lt, rc, error, trname, byte_count
293-
Add sample, consisting of: cnc, rt, cn, lt, rc, error, trname, byte_count, [action_id]
294280
295281
:type sample: tuple
296282
"""
297283
cnc, r_time, con_time, latency, r_code, error, trname, byte_count = sample
298-
# if len(sample) == 9:
299-
# cnc, r_time, con_time, latency, r_code, error, trname, byte_count, action_id = sample
300-
# else:
301-
# cnc, r_time, con_time, latency, r_code, error, trname, byte_count = sample
302-
# action_id = None
303284
self[self.SAMPLE_COUNT] += 1
304285
if cnc:
305286
self.add_concurrency(cnc, trname)
@@ -316,15 +297,8 @@ def add_sample(self, sample):
316297

317298
if error is not None:
318299
self[self.FAILURES] += 1
300+
319301
item = self.error_item_skel(error, r_code, 1, KPISet.ERRTYPE_ERROR, Counter(), None)
320-
# item = self.error_item_skel(
321-
# error,
322-
# r_code,
323-
# 1,
324-
# KPISet.ERRTYPE_ERROR,
325-
# Counter(),
326-
# None,
327-
# action_id=action_id)
328302
self.inc_list(self[self.ERRORS], ("msg", error), item)
329303
else:
330304
self[self.SUCCESSES] += 1

bzt/modules/functional.py

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,6 @@ def post_process(self):
6565

6666

6767
class FunctionalSample(object):
68-
# def to_error_dict(self):
69-
# err = {
70-
# "error_msg": self.error_msg,
71-
# "error_trace": self.error_trace,
72-
# "status": self.status,
73-
# }
74-
# if getattr(self, "actionId", None):
75-
# err["actionId"] = self.actionId
76-
# return err
77-
78-
# def __init__(
79-
# self, test_case, test_suite, status,
80-
# start_time, duration, error_msg, error_trace,
81-
# extras=None, subsamples=None, path=None, actionId=None
82-
# ):
8368
def __init__(
8469
self, test_case, test_suite, status,
8570
start_time, duration, error_msg, error_trace,
@@ -105,7 +90,6 @@ def __init__(
10590
self.extras = extras or {}
10691
self.subsamples = subsamples or []
10792
self.path = path or []
108-
# self.actionId = actionId
10993

11094
def get_fqn(self):
11195
if self.path:
@@ -278,22 +262,9 @@ def _samples_from_row(self, row):
278262
result.extend([sub for sub in subsamples if sub.get_type() == 'transaction'])
279263
else:
280264
sample = FunctionalSample(test_case=row["test_case"], test_suite=row["test_suite"],
281-
status=row["status"], start_time=row["start_time"], duration=row["duration"],
282-
error_msg=row["error_msg"], error_trace=row["error_trace"],
283-
extras=row.get("extras", {}), subsamples=subsamples, path=row.get("path", []))
284-
# sample = FunctionalSample(
285-
# test_case=row["test_case"],
286-
# test_suite=row["test_suite"],
287-
# status=row["status"],
288-
# start_time=row["start_time"],
289-
# duration=row["duration"],
290-
# error_msg=row["error_msg"],
291-
# error_trace=row["error_trace"],
292-
# extras=row.get("extras", {}),
293-
# subsamples=subsamples,
294-
# path=row.get("path", []),
295-
# actionId=row.get("actionId") or row.get("action_id")
296-
# )
265+
status=row["status"], start_time=row["start_time"], duration=row["duration"],
266+
error_msg=row["error_msg"], error_trace=row["error_trace"],
267+
extras=row.get("extras", {}), subsamples=subsamples, path=row.get("path", []))
297268
result.append(sample)
298269
return result
299270

bzt/modules/reporting.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,6 @@ def __report_failed_tests(self):
141141
if case.status in ("FAILED", "BROKEN"):
142142
full_name = case.test_suite + "." + case.test_case
143143
msg = "Test {test_case} failed: {error_msg}".format(test_case=full_name, error_msg=case.error_msg)
144-
# if hasattr(case, "action_id") and case.action_id:
145-
# msg += f"\nAction ID: {case.action_id}"
146144
if case.error_trace:
147145
msg += "\n" + case.error_trace
148146
self.log.warning(msg)

tests/unit/modules/_selenium/test_selenium_builder.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,49 @@ def test_modern_actions_generator(self):
204204
TestSeleniumScriptGeneration.clear_spaces(content),
205205
msg="\n\n%s. %s" % (idx, target_lines[idx]))
206206

207+
def test_actionid_try_except_inside_step_method(self):
208+
self.configure({
209+
"execution": [{
210+
"executor": "apiritif",
211+
"scenario": "loc_sc"
212+
}],
213+
"scenarios": {
214+
"loc_sc": {
215+
"default-address": "https://example.com",
216+
"requests": [{
217+
"label": "tx",
218+
"actions": [{
219+
"type": "go",
220+
"param": "https://example.com",
221+
"value": None,
222+
"actionId": "aid-1"
223+
}]
224+
}]
225+
}
226+
}
227+
})
228+
229+
self.obj.prepare()
230+
with open(self.obj.script) as fds:
231+
content = fds.read()
232+
233+
self.assertIn(
234+
TestSeleniumScriptGeneration.clear_spaces("with apiritif.smart_transaction('tx'):\n try:"),
235+
TestSeleniumScriptGeneration.clear_spaces(content)
236+
)
237+
self.assertIn(
238+
TestSeleniumScriptGeneration.clear_spaces("raise type(exc)"),
239+
TestSeleniumScriptGeneration.clear_spaces(content)
240+
)
241+
self.assertIn(
242+
TestSeleniumScriptGeneration.clear_spaces("actionId: "),
243+
TestSeleniumScriptGeneration.clear_spaces(content)
244+
)
245+
self.assertIn(
246+
TestSeleniumScriptGeneration.clear_spaces("def test_locsc(self):\n self._1_tx()"),
247+
TestSeleniumScriptGeneration.clear_spaces(content)
248+
)
249+
207250
@staticmethod
208251
def clear_spaces(content):
209252
return content.replace(" ", "").replace("\t", "").replace("\n", "")

0 commit comments

Comments
 (0)