feat: step teardown hook

This commit is contained in:
debugtalk
2020-06-07 18:20:13 +08:00
parent cffd80b57a
commit 9367cac706
3 changed files with 20 additions and 2 deletions

View File

@@ -246,6 +246,17 @@ def make_teststep_chain_style(teststep: Dict) -> Text:
call_ref_testcase = f".call({testcase})"
step_info += call_ref_testcase
if "teardown_hooks" in teststep:
teardown_hooks = teststep["teardown_hooks"]
for hook in teardown_hooks:
if isinstance(hook, Text):
step_info += f'.teardown_hook("{hook}")'
elif isinstance(hook, Dict) and len(hook) == 1:
assign_var_name, hook_content = list(hook.items())[0]
step_info += f'.teardown_hook("{hook}", "{assign_var_name}")'
else:
raise exceptions.TestCaseFormatError(f"Invalid teardown hook: {hook}")
if "extract" in teststep:
# request step
step_info += ".extract()"

View File

@@ -265,8 +265,13 @@ class RequestWithOptionalArgs(object):
self.__step_context.request.upload.update(file_info)
return self
# def hooks(self):
# pass
def teardown_hook(self, hook: Text, assign_var_name: Text = None) -> "RequestWithOptionalArgs":
if assign_var_name:
self.__step_context.teardown_hooks.append({assign_var_name: hook})
else:
self.__step_context.teardown_hooks.append(hook)
return self
def extract(self) -> StepRequestExtraction:
return StepRequestExtraction(self.__step_context)