mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-13 19:59:37 +08:00
change: generate pytest in chain style by default
This commit is contained in:
@@ -29,35 +29,6 @@ __TEMPLATE__ = jinja2.Template(
|
||||
import os
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, os.getcwd())
|
||||
{% endif %}
|
||||
from httprunner import HttpRunner, TConfig, TStep
|
||||
{% for import_str in imports_list %}
|
||||
{{ import_str }}
|
||||
{% endfor %}
|
||||
|
||||
class {{ class_name }}(HttpRunner):
|
||||
config = TConfig(**{{ config }})
|
||||
|
||||
teststeps = [
|
||||
{% for teststep in teststeps %}
|
||||
TStep(**{{ teststep }}),
|
||||
{% endfor %}
|
||||
]
|
||||
|
||||
if __name__ == "__main__":
|
||||
{{ class_name }}().test_start()
|
||||
|
||||
"""
|
||||
)
|
||||
|
||||
__TEMPLATE_CHAIN_STYLE__ = jinja2.Template(
|
||||
"""# NOTICE: Generated By HttpRunner. DO NOT EDIT!
|
||||
# FROM: {{ testcase_path }}
|
||||
{% if imports_list %}
|
||||
import os
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, os.getcwd())
|
||||
{% endif %}
|
||||
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
|
||||
@@ -231,7 +202,7 @@ def make_teststep_chain_style(teststep: Dict) -> Text:
|
||||
if teststep.get("request"):
|
||||
step_info += make_request_chain_style(teststep["request"])
|
||||
elif teststep.get("testcase"):
|
||||
testcase = teststep["testcase"].replace("CLS_LB(", "").replace(")CLS_RB", "")
|
||||
testcase = teststep["testcase"]
|
||||
call_ref_testcase = f".call({testcase})"
|
||||
step_info += call_ref_testcase
|
||||
|
||||
@@ -248,19 +219,21 @@ def make_teststep_chain_style(teststep: Dict) -> Text:
|
||||
validator = uniform_validator(v)
|
||||
assert_method = validator["assert"]
|
||||
check = validator["check"]
|
||||
if '"' in check:
|
||||
# e.g. body."user-agent" => 'body."user-agent"'
|
||||
check = f"'{check}'"
|
||||
else:
|
||||
check = f'"{check}"'
|
||||
expect = validator["expect"]
|
||||
if isinstance(expect, Text):
|
||||
expect = f'"{expect}"'
|
||||
step_info += f'.assert_{assert_method}("{check}", {expect})'
|
||||
step_info += f'.assert_{assert_method}({check}, {expect})'
|
||||
|
||||
return f"Step({step_info})"
|
||||
|
||||
|
||||
def __make_testcase(
|
||||
testcase: Dict,
|
||||
dir_path: Text = None,
|
||||
ref_flag: bool = False,
|
||||
chain_style: bool = False,
|
||||
testcase: Dict, dir_path: Text = None, ref_flag: bool = False,
|
||||
) -> NoReturn:
|
||||
"""convert valid testcase dict to pytest file path"""
|
||||
# ensure compatibility with testcase format v2
|
||||
@@ -309,7 +282,7 @@ def __make_testcase(
|
||||
ref_testcase_python_path, ref_testcase_cls_name = convert_testcase_path(
|
||||
ref_testcase_path
|
||||
)
|
||||
teststep["testcase"] = f"CLS_LB({ref_testcase_cls_name})CLS_RB"
|
||||
teststep["testcase"] = ref_testcase_cls_name
|
||||
|
||||
# prepare import ref testcase
|
||||
ref_testcase_python_path = ref_testcase_python_path[len(os.getcwd()) + 1 :]
|
||||
@@ -323,19 +296,12 @@ def __make_testcase(
|
||||
"testcase_path": __ensure_cwd_relative(testcase_path),
|
||||
"class_name": f"TestCase{testcase_cls_name}",
|
||||
"imports_list": imports_list,
|
||||
}
|
||||
|
||||
if chain_style:
|
||||
data["config_chain_style"] = make_config_chain_style(config)
|
||||
data["teststeps_chain_style"] = [
|
||||
"config_chain_style": make_config_chain_style(config),
|
||||
"teststeps_chain_style": [
|
||||
make_teststep_chain_style(step) for step in teststeps
|
||||
]
|
||||
content = __TEMPLATE_CHAIN_STYLE__.render(data)
|
||||
else:
|
||||
data["config"] = config
|
||||
data["teststeps"] = teststeps
|
||||
content = __TEMPLATE__.render(data)
|
||||
content = content.replace("'CLS_LB(", "").replace(")CLS_RB'", "")
|
||||
],
|
||||
}
|
||||
content = __TEMPLATE__.render(data)
|
||||
|
||||
with open(testcase_python_path, "w", encoding="utf-8") as f:
|
||||
f.write(content)
|
||||
@@ -348,7 +314,7 @@ def __make_testcase(
|
||||
make_files_cache_set.add(__ensure_cwd_relative(testcase_python_path))
|
||||
|
||||
|
||||
def __make_testsuite(testsuite: Dict, chain_style: bool = False) -> NoReturn:
|
||||
def __make_testsuite(testsuite: Dict) -> NoReturn:
|
||||
"""convert valid testsuite dict to pytest folder with testcases"""
|
||||
# validate testsuite format
|
||||
load_testsuite(testsuite)
|
||||
@@ -393,12 +359,10 @@ def __make_testsuite(testsuite: Dict, chain_style: bool = False) -> NoReturn:
|
||||
testcase_dict["config"]["variables"].update(testsuite_variables)
|
||||
|
||||
# make testcase
|
||||
__make_testcase(testcase_dict, testsuite_dir, chain_style=chain_style)
|
||||
__make_testcase(testcase_dict, testsuite_dir)
|
||||
|
||||
|
||||
def __make(
|
||||
tests_path: Text, ref_flag: bool = False, chain_style: bool = False
|
||||
) -> NoReturn:
|
||||
def __make(tests_path: Text, ref_flag: bool = False) -> NoReturn:
|
||||
""" make testcase(s) with testcase/testsuite/folder absolute path
|
||||
generated pytest file path will be cached in make_files_cache_set
|
||||
|
||||
@@ -432,16 +396,14 @@ def __make(
|
||||
# testcase
|
||||
if "teststeps" in test_content:
|
||||
try:
|
||||
__make_testcase(
|
||||
test_content, ref_flag=ref_flag, chain_style=chain_style
|
||||
)
|
||||
__make_testcase(test_content, ref_flag=ref_flag)
|
||||
except exceptions.TestCaseFormatError:
|
||||
continue
|
||||
|
||||
# testsuite
|
||||
elif "testcases" in test_content:
|
||||
try:
|
||||
__make_testsuite(test_content, chain_style=chain_style)
|
||||
__make_testsuite(test_content)
|
||||
except exceptions.TestSuiteFormatError:
|
||||
continue
|
||||
|
||||
@@ -452,12 +414,12 @@ def __make(
|
||||
)
|
||||
|
||||
|
||||
def main_make(tests_paths: List[Text], chain_style: bool = False) -> List[Text]:
|
||||
def main_make(tests_paths: List[Text]) -> List[Text]:
|
||||
for tests_path in tests_paths:
|
||||
if not os.path.isabs(tests_path):
|
||||
tests_path = os.path.join(os.getcwd(), tests_path)
|
||||
|
||||
__make(tests_path, chain_style=chain_style)
|
||||
__make(tests_path)
|
||||
|
||||
testcase_path_list = list(make_files_cache_set)
|
||||
__format_pytest_with_black(testcase_path_list)
|
||||
@@ -473,11 +435,5 @@ def init_make_parser(subparsers):
|
||||
parser.add_argument(
|
||||
"testcase_path", nargs="*", help="Specify YAML/JSON testcase file/folder path"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--chain-style",
|
||||
dest="chain_style",
|
||||
action="store_true",
|
||||
help="Convert pytest files in chain-style.",
|
||||
)
|
||||
|
||||
return parser
|
||||
|
||||
Reference in New Issue
Block a user