mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-03 06:49:38 +08:00
refactor: use step export to export session variables from referenced testcase
This commit is contained in:
@@ -43,10 +43,10 @@ class HttpRunner(object):
|
||||
__teststeps: List[TStep]
|
||||
__project_meta: ProjectMeta = None
|
||||
__case_id: Text = ""
|
||||
__export: List[Text] = []
|
||||
__step_datas: List[StepData] = None
|
||||
__session: HttpSession = None
|
||||
__session_variables: VariablesMapping = {}
|
||||
__export_variables: VariablesMapping = {}
|
||||
# time
|
||||
__start_at: float = 0
|
||||
__duration: float = 0
|
||||
@@ -82,6 +82,10 @@ class HttpRunner(object):
|
||||
self.__session_variables = variables
|
||||
return self
|
||||
|
||||
def with_export(self, export: List[Text]) -> "HttpRunner":
|
||||
self.__export = export
|
||||
return self
|
||||
|
||||
def __run_step_request(self, step: TStep) -> StepData:
|
||||
"""run teststep: request"""
|
||||
step_data = StepData(name=step.name)
|
||||
@@ -166,6 +170,7 @@ class HttpRunner(object):
|
||||
"""run teststep: referenced testcase"""
|
||||
step_data = StepData(name=step.name)
|
||||
step_variables = step.variables
|
||||
step_export = step.export
|
||||
|
||||
if hasattr(step.testcase, "config") and hasattr(step.testcase, "teststeps"):
|
||||
testcase_cls = step.testcase
|
||||
@@ -174,6 +179,7 @@ class HttpRunner(object):
|
||||
.with_session(self.__session)
|
||||
.with_case_id(self.__case_id)
|
||||
.with_variables(step_variables)
|
||||
.with_export(step_export)
|
||||
.run()
|
||||
)
|
||||
|
||||
@@ -188,6 +194,7 @@ class HttpRunner(object):
|
||||
.with_session(self.__session)
|
||||
.with_case_id(self.__case_id)
|
||||
.with_variables(step_variables)
|
||||
.with_export(step_export)
|
||||
.run_path(ref_testcase_path)
|
||||
)
|
||||
|
||||
@@ -201,6 +208,9 @@ class HttpRunner(object):
|
||||
step_data.success = case_result.success
|
||||
self.success &= case_result.success
|
||||
|
||||
if step_data.export:
|
||||
logger.info(f"export variables: {step_data.export}")
|
||||
|
||||
return step_data
|
||||
|
||||
def __run_step(self, step: TStep) -> Dict:
|
||||
@@ -252,7 +262,6 @@ class HttpRunner(object):
|
||||
self.__step_datas: List[StepData] = []
|
||||
self.__session = self.__session or HttpSession()
|
||||
self.__session_variables = {}
|
||||
self.__export_variables = {}
|
||||
|
||||
# run teststeps
|
||||
for step in self.__teststeps:
|
||||
@@ -274,11 +283,6 @@ class HttpRunner(object):
|
||||
self.__session_variables.update(extract_mapping)
|
||||
|
||||
self.__duration = time.time() - self.__start_at
|
||||
|
||||
self.__export_variables = self.get_export_variables()
|
||||
if self.__export_variables:
|
||||
logger.info(f"export variables: {self.__export_variables}")
|
||||
|
||||
return self
|
||||
|
||||
def run_path(self, path: Text) -> "HttpRunner":
|
||||
@@ -303,11 +307,10 @@ class HttpRunner(object):
|
||||
return self.__step_datas
|
||||
|
||||
def get_export_variables(self) -> Dict:
|
||||
if self.__export_variables:
|
||||
return self.__export_variables
|
||||
|
||||
# override testcase export vars with step export
|
||||
export_var_names = self.__export or self.__config.export
|
||||
export_vars_mapping = {}
|
||||
for var_name in self.__config.export:
|
||||
for var_name in export_var_names:
|
||||
if var_name not in self.__session_variables:
|
||||
raise ParamsError(
|
||||
f"failed to export variable {var_name} from session variables {self.__session_variables}"
|
||||
|
||||
Reference in New Issue
Block a user