mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-11 18:11:21 +08:00
refactor: remove ref_flag
This commit is contained in:
@@ -26,7 +26,7 @@ class TestCaseRequestWithTestcaseReference(HttpRunner):
|
||||
RunTestCase("request with functions")
|
||||
.with_variables(**{"foo1": "override_bar1"})
|
||||
.call(RequestWithFunctions)
|
||||
.extract(*["session_foo2"])
|
||||
.export(*["session_foo2"])
|
||||
),
|
||||
Step(
|
||||
RunRequest("post form data")
|
||||
|
||||
@@ -21,10 +21,11 @@ from httprunner.response import uniform_validator
|
||||
|
||||
""" cache converted pytest files, avoid duplicate making
|
||||
"""
|
||||
make_files_cache_set: Set = set()
|
||||
pytest_files_made_cache_mapping: Dict[Text, Text] = {}
|
||||
|
||||
""" save generated pytest files to run, except referenced testcase
|
||||
"""
|
||||
pytest_files_set: Set = set()
|
||||
pytest_files_run_set: Set = set()
|
||||
|
||||
__TEMPLATE__ = jinja2.Template(
|
||||
"""# NOTICE: Generated By HttpRunner v{{ version }}
|
||||
@@ -270,8 +271,8 @@ def make_testcase(testcase: Dict, dir_path: Text = None) -> Text:
|
||||
dir_path, os.path.basename(testcase_python_path)
|
||||
)
|
||||
|
||||
global make_files_cache_set
|
||||
if testcase_python_path in make_files_cache_set:
|
||||
global pytest_files_made_cache_mapping
|
||||
if testcase_python_path in pytest_files_made_cache_mapping:
|
||||
return testcase_python_path
|
||||
|
||||
config = testcase["config"]
|
||||
@@ -297,12 +298,10 @@ def make_testcase(testcase: Dict, dir_path: Text = None) -> Text:
|
||||
ref_testcase_path = __ensure_absolute(teststep["testcase"])
|
||||
test_content = load_test_file(ref_testcase_path)
|
||||
test_content.setdefault("config", {})["path"] = ref_testcase_path
|
||||
make_testcase(test_content)
|
||||
ref_testcase_python_path = make_testcase(test_content)
|
||||
|
||||
# prepare ref testcase class name
|
||||
ref_testcase_python_path, ref_testcase_cls_name = convert_testcase_path(
|
||||
ref_testcase_path
|
||||
)
|
||||
ref_testcase_cls_name = pytest_files_made_cache_mapping[ref_testcase_python_path]
|
||||
teststep["testcase"] = ref_testcase_cls_name
|
||||
|
||||
# prepare import ref testcase
|
||||
@@ -328,12 +327,11 @@ def make_testcase(testcase: Dict, dir_path: Text = None) -> Text:
|
||||
with open(testcase_python_path, "w", encoding="utf-8") as f:
|
||||
f.write(content)
|
||||
|
||||
pytest_files_made_cache_mapping[testcase_python_path] = testcase_cls_name
|
||||
__ensure_testcase_module(testcase_python_path)
|
||||
|
||||
logger.info(f"generated testcase: {testcase_python_path}")
|
||||
|
||||
make_files_cache_set.add(__ensure_cwd_relative(testcase_python_path))
|
||||
|
||||
return testcase_python_path
|
||||
|
||||
|
||||
@@ -386,12 +384,12 @@ def make_testsuite(testsuite: Dict) -> NoReturn:
|
||||
|
||||
# make testcase
|
||||
testcase_pytest_path = make_testcase(testcase_dict, testsuite_dir)
|
||||
pytest_files_set.add(testcase_pytest_path)
|
||||
pytest_files_run_set.add(testcase_pytest_path)
|
||||
|
||||
|
||||
def __make(tests_path: Text) -> NoReturn:
|
||||
""" make testcase(s) with testcase/testsuite/folder absolute path
|
||||
generated pytest file path will be cached in make_files_cache_set
|
||||
generated pytest file path will be cached in pytest_files_made_cache_mapping
|
||||
|
||||
Args:
|
||||
tests_path: should be in absolute path
|
||||
@@ -408,8 +406,7 @@ def __make(tests_path: Text) -> NoReturn:
|
||||
|
||||
for test_file in test_files:
|
||||
if test_file.lower().endswith("_test.py"):
|
||||
pytest_files_set.add(test_file)
|
||||
make_files_cache_set.add(test_file)
|
||||
pytest_files_run_set.add(test_file)
|
||||
continue
|
||||
|
||||
try:
|
||||
@@ -428,7 +425,7 @@ def __make(tests_path: Text) -> NoReturn:
|
||||
if "teststeps" in test_content:
|
||||
try:
|
||||
testcase_pytest_path = make_testcase(test_content)
|
||||
pytest_files_set.add(testcase_pytest_path)
|
||||
pytest_files_run_set.add(testcase_pytest_path)
|
||||
except exceptions.TestCaseFormatError:
|
||||
continue
|
||||
|
||||
@@ -455,10 +452,10 @@ def main_make(tests_paths: List[Text]) -> List[Text]:
|
||||
__make(tests_path)
|
||||
|
||||
# format pytest files
|
||||
make_files_list = list(make_files_cache_set)
|
||||
format_pytest_with_black(*make_files_list)
|
||||
pytest_files_format_list = pytest_files_made_cache_mapping.keys()
|
||||
format_pytest_with_black(*pytest_files_format_list)
|
||||
|
||||
return list(pytest_files_set)
|
||||
return list(pytest_files_run_set)
|
||||
|
||||
|
||||
def init_make_parser(subparsers):
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import os
|
||||
import unittest
|
||||
|
||||
from httprunner.make import (
|
||||
main_make,
|
||||
convert_testcase_path,
|
||||
make_files_cache_set,
|
||||
pytest_files_made_cache_mapping,
|
||||
make_config_chain_style,
|
||||
make_teststep_chain_style,
|
||||
pytest_files_set,
|
||||
pytest_files_run_set,
|
||||
)
|
||||
|
||||
|
||||
@@ -16,19 +17,25 @@ class TestMake(unittest.TestCase):
|
||||
testcase_python_list = main_make(path)
|
||||
self.assertEqual(
|
||||
testcase_python_list[0],
|
||||
"examples/postman_echo/request_methods/request_with_variables_test.py",
|
||||
os.path.join(
|
||||
os.getcwd(),
|
||||
"examples/postman_echo/request_methods/request_with_variables_test.py",
|
||||
),
|
||||
)
|
||||
|
||||
def test_make_testcase_with_ref(self):
|
||||
path = [
|
||||
"examples/postman_echo/request_methods/request_with_testcase_reference.yml"
|
||||
]
|
||||
make_files_cache_set.clear()
|
||||
pytest_files_set.clear()
|
||||
pytest_files_made_cache_mapping.clear()
|
||||
pytest_files_run_set.clear()
|
||||
testcase_python_list = main_make(path)
|
||||
self.assertEqual(len(testcase_python_list), 1)
|
||||
self.assertIn(
|
||||
"examples/postman_echo/request_methods/request_with_testcase_reference_test.py",
|
||||
os.path.join(
|
||||
os.getcwd(),
|
||||
"examples/postman_echo/request_methods/request_with_testcase_reference_test.py",
|
||||
),
|
||||
testcase_python_list,
|
||||
)
|
||||
|
||||
@@ -52,7 +59,10 @@ from examples.postman_echo.request_methods.request_with_functions_test import (
|
||||
path = ["examples/postman_echo/request_methods/"]
|
||||
testcase_python_list = main_make(path)
|
||||
self.assertIn(
|
||||
"examples/postman_echo/request_methods/request_with_functions_test.py",
|
||||
os.path.join(
|
||||
os.getcwd(),
|
||||
"examples/postman_echo/request_methods/request_with_functions_test.py",
|
||||
),
|
||||
testcase_python_list,
|
||||
)
|
||||
|
||||
@@ -92,16 +102,22 @@ from examples.postman_echo.request_methods.request_with_functions_test import (
|
||||
|
||||
def test_make_testsuite(self):
|
||||
path = ["examples/postman_echo/request_methods/demo_testsuite.yml"]
|
||||
make_files_cache_set.clear()
|
||||
pytest_files_set.clear()
|
||||
pytest_files_made_cache_mapping.clear()
|
||||
pytest_files_run_set.clear()
|
||||
testcase_python_list = main_make(path)
|
||||
self.assertEqual(len(testcase_python_list), 2)
|
||||
self.assertIn(
|
||||
"examples/postman_echo/request_methods/demo_testsuite_yml/request_with_functions_test.py",
|
||||
os.path.join(
|
||||
os.getcwd(),
|
||||
"examples/postman_echo/request_methods/demo_testsuite_yml/request_with_functions_test.py",
|
||||
),
|
||||
testcase_python_list,
|
||||
)
|
||||
self.assertIn(
|
||||
"examples/postman_echo/request_methods/demo_testsuite_yml/request_with_testcase_reference_test.py",
|
||||
os.path.join(
|
||||
os.getcwd(),
|
||||
"examples/postman_echo/request_methods/demo_testsuite_yml/request_with_testcase_reference_test.py",
|
||||
),
|
||||
testcase_python_list,
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user