feat: testsuite config get variables by call function

This commit is contained in:
debugtalk
2020-05-18 22:37:12 +08:00
parent 5b7bcea3d0
commit 7ebb1696c7
4 changed files with 23 additions and 10 deletions

View File

@@ -7,3 +7,9 @@ def get_httprunner_version():
def sum_two(m, n):
return m + n
def get_variables():
return {
"foo1": "session_bar1"
}

View File

@@ -1,6 +1,6 @@
config:
name: "demo testsuite"
# variables: ${get_variable()}
variables: ${get_variables()}
testcases:
-

View File

@@ -11,8 +11,9 @@ from httprunner.loader import (
load_test_file,
load_testcase,
load_testsuite,
get_project_working_directory,
load_project_meta,
)
from httprunner.parser import parse_data
__TMPL__ = """# NOTICE: Generated By HttpRunner. DO'NOT EDIT!
# FROM: {{ testcase_path }}
@@ -112,10 +113,19 @@ def make_testsuite(testsuite: Dict) -> List[Text]:
config = testsuite["config"]
testsuite_path = config["path"]
project_meta = load_project_meta(testsuite_path)
project_working_directory = project_meta.PWD
testsuite_variables = config.get("variables", {})
if isinstance(testsuite_variables, Text):
# get variables by function, e.g. ${get_variables()}
testsuite_variables = parse_data(
testsuite_variables, {}, project_meta.functions
)
logger.info(f"start to make testsuite: {testsuite_path}")
testcase_files = []
project_working_directory = get_project_working_directory(testsuite_path)
for testcase in testsuite["testcases"]:
# get referenced testcase content
@@ -132,12 +142,8 @@ def make_testsuite(testsuite: Dict) -> List[Text]:
testcase_dict["config"]["base_url"] = base_url
# override variables
testcase_dict["config"].setdefault("variables", {})
testcase_dict["config"]["variables"].update(
testcase.get("variables", {})
)
testcase_dict["config"]["variables"].update(
testsuite["config"].get("variables", {})
)
testcase_dict["config"]["variables"].update(testcase.get("variables", {}))
testcase_dict["config"]["variables"].update(testsuite_variables)
# create directory with testsuite file name, put its testcases under this directory
testcase_dict["config"]["path"] = os.path.join(

View File

@@ -36,7 +36,8 @@ class TConfig(BaseModel):
name: Name
verify: Verify = False
base_url: BaseUrl = ""
variables: VariablesMapping = {}
# Text: prepare variables in debugtalk.py, ${get_variable()}
variables: Union[VariablesMapping, Text] = {}
setup_hooks: Hook = []
teardown_hooks: Hook = []
export: Export = []