change: override variables strategy, step variables > extracted variables from previous steps

This commit is contained in:
debugtalk
2020-07-30 10:56:38 +08:00
parent 07190ec6c9
commit de20ad8d2b
5 changed files with 25 additions and 23 deletions

View File

@@ -1,5 +1,11 @@
# Release History
## 3.1.4 (2020-07-30)
**Changed**
- change: override variables strategy, step variables > extracted variables from previous steps
## 3.1.3 (2020-07-06)
**Added**

View File

@@ -24,7 +24,7 @@ from httprunner.loader import (
convert_relative_project_root_dir,
)
from httprunner.response import uniform_validator
from httprunner.utils import override_config_variables, is_support_multiprocessing
from httprunner.utils import merge_variables, is_support_multiprocessing
""" cache converted pytest files, avoid duplicate making
"""
@@ -485,9 +485,7 @@ def make_testsuite(testsuite: Dict) -> NoReturn:
testcase_variables = convert_variables(
testcase.get("variables", {}), testcase_path
)
testcase_variables = override_config_variables(
testcase_variables, testsuite_variables
)
testcase_variables = merge_variables(testcase_variables, testsuite_variables)
# testsuite testcase variables > testcase config variables
testcase_dict["config"]["variables"] = convert_variables(
testcase_dict["config"].get("variables", {}), testcase_path

View File

@@ -21,7 +21,7 @@ from httprunner.loader import load_project_meta, load_testcase_file
from httprunner.parser import build_url, parse_data, parse_variables_mapping
from httprunner.response import ResponseObject
from httprunner.testcase import Config, Step
from httprunner.utils import override_config_variables
from httprunner.utils import merge_variables
from httprunner.models import (
TConfig,
TStep,
@@ -335,17 +335,16 @@ class HttpRunner(object):
self.__start_at = time.time()
self.__step_datas: List[StepData] = []
self.__session = self.__session or HttpSession()
self.__session_variables = {}
# save extracted variables of teststeps
extracted_variables: VariablesMapping = {}
# run teststeps
for step in self.__teststeps:
# override variables
# session variables (extracted from pre step) > step variables
step.variables.update(self.__session_variables)
# step variables > extracted variables from previous steps
step.variables = merge_variables(step.variables, extracted_variables)
# step variables > testcase config variables
step.variables = override_config_variables(
step.variables, self.__config.variables
)
step.variables = merge_variables(step.variables, self.__config.variables)
# parse variables
step.variables = parse_variables_mapping(
@@ -360,8 +359,9 @@ class HttpRunner(object):
extract_mapping = self.__run_step(step)
# save extracted variables to session variables
self.__session_variables.update(extract_mapping)
extracted_variables.update(extract_mapping)
self.__session_variables.update(extract_mapping)
self.__duration = time.time() - self.__start_at
return self

View File

@@ -193,15 +193,13 @@ class ExtendJSONEncoder(json.JSONEncoder):
return repr(obj)
def override_config_variables(
step_variables: VariablesMapping, config_variables: VariablesMapping
def merge_variables(
variables: VariablesMapping, variables_to_be_overridden: VariablesMapping
) -> VariablesMapping:
""" override variables:
testcase step variables > testcase config variables
testsuite testcase variables > testsuite config variables
""" merge two variables mapping, the first variables have higher priority
"""
step_new_variables = {}
for key, value in step_variables.items():
for key, value in variables.items():
if f"${key}" == value or "${" + key + "}" == value:
# e.g. {"base_url": "$base_url"}
# or {"base_url": "${base_url}"}
@@ -209,9 +207,9 @@ def override_config_variables(
step_new_variables[key] = value
variables = copy.deepcopy(config_variables)
variables.update(step_new_variables)
return variables
merged_variables = copy.deepcopy(variables_to_be_overridden)
merged_variables.update(step_new_variables)
return merged_variables
def is_support_multiprocessing() -> bool:

View File

@@ -6,7 +6,7 @@ import unittest
from httprunner import loader, utils
from httprunner.utils import (
ExtendJSONEncoder,
override_config_variables,
merge_variables,
)
@@ -122,7 +122,7 @@ class TestUtils(unittest.TestCase):
step_variables = {"base_url": "$base_url", "foo1": "bar1"}
config_variables = {"base_url": "https://httpbin.org", "foo1": "bar111"}
self.assertEqual(
override_config_variables(step_variables, config_variables),
merge_variables(step_variables, config_variables),
{"base_url": "https://httpbin.org", "foo1": "bar1"},
)