refactor: remove ref_flag

This commit is contained in:
debugtalk
2020-06-05 14:27:14 +08:00
parent b7e2f37ff0
commit 26f5313172
3 changed files with 43 additions and 30 deletions
@@ -26,7 +26,7 @@ class TestCaseRequestWithTestcaseReference(HttpRunner):
RunTestCase("request with functions") RunTestCase("request with functions")
.with_variables(**{"foo1": "override_bar1"}) .with_variables(**{"foo1": "override_bar1"})
.call(RequestWithFunctions) .call(RequestWithFunctions)
.extract(*["session_foo2"]) .export(*["session_foo2"])
), ),
Step( Step(
RunRequest("post form data") RunRequest("post form data")
+15 -18
View File
@@ -21,10 +21,11 @@ from httprunner.response import uniform_validator
""" cache converted pytest files, avoid duplicate making """ 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 """ save generated pytest files to run, except referenced testcase
""" """
pytest_files_set: Set = set() pytest_files_run_set: Set = set()
__TEMPLATE__ = jinja2.Template( __TEMPLATE__ = jinja2.Template(
"""# NOTICE: Generated By HttpRunner v{{ version }} """# 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) dir_path, os.path.basename(testcase_python_path)
) )
global make_files_cache_set global pytest_files_made_cache_mapping
if testcase_python_path in make_files_cache_set: if testcase_python_path in pytest_files_made_cache_mapping:
return testcase_python_path return testcase_python_path
config = testcase["config"] config = testcase["config"]
@@ -297,12 +298,10 @@ def make_testcase(testcase: Dict, dir_path: Text = None) -> Text:
ref_testcase_path = __ensure_absolute(teststep["testcase"]) ref_testcase_path = __ensure_absolute(teststep["testcase"])
test_content = load_test_file(ref_testcase_path) test_content = load_test_file(ref_testcase_path)
test_content.setdefault("config", {})["path"] = 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 # prepare ref testcase class name
ref_testcase_python_path, ref_testcase_cls_name = convert_testcase_path( ref_testcase_cls_name = pytest_files_made_cache_mapping[ref_testcase_python_path]
ref_testcase_path
)
teststep["testcase"] = ref_testcase_cls_name teststep["testcase"] = ref_testcase_cls_name
# prepare import ref testcase # 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: with open(testcase_python_path, "w", encoding="utf-8") as f:
f.write(content) f.write(content)
pytest_files_made_cache_mapping[testcase_python_path] = testcase_cls_name
__ensure_testcase_module(testcase_python_path) __ensure_testcase_module(testcase_python_path)
logger.info(f"generated testcase: {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 return testcase_python_path
@@ -386,12 +384,12 @@ def make_testsuite(testsuite: Dict) -> NoReturn:
# make testcase # make testcase
testcase_pytest_path = make_testcase(testcase_dict, testsuite_dir) 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: def __make(tests_path: Text) -> NoReturn:
""" make testcase(s) with testcase/testsuite/folder absolute path """ 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: Args:
tests_path: should be in absolute path tests_path: should be in absolute path
@@ -408,8 +406,7 @@ def __make(tests_path: Text) -> NoReturn:
for test_file in test_files: for test_file in test_files:
if test_file.lower().endswith("_test.py"): if test_file.lower().endswith("_test.py"):
pytest_files_set.add(test_file) pytest_files_run_set.add(test_file)
make_files_cache_set.add(test_file)
continue continue
try: try:
@@ -428,7 +425,7 @@ def __make(tests_path: Text) -> NoReturn:
if "teststeps" in test_content: if "teststeps" in test_content:
try: try:
testcase_pytest_path = make_testcase(test_content) 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: except exceptions.TestCaseFormatError:
continue continue
@@ -455,10 +452,10 @@ def main_make(tests_paths: List[Text]) -> List[Text]:
__make(tests_path) __make(tests_path)
# format pytest files # format pytest files
make_files_list = list(make_files_cache_set) pytest_files_format_list = pytest_files_made_cache_mapping.keys()
format_pytest_with_black(*make_files_list) format_pytest_with_black(*pytest_files_format_list)
return list(pytest_files_set) return list(pytest_files_run_set)
def init_make_parser(subparsers): def init_make_parser(subparsers):
+27 -11
View File
@@ -1,12 +1,13 @@
import os
import unittest import unittest
from httprunner.make import ( from httprunner.make import (
main_make, main_make,
convert_testcase_path, convert_testcase_path,
make_files_cache_set, pytest_files_made_cache_mapping,
make_config_chain_style, make_config_chain_style,
make_teststep_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) testcase_python_list = main_make(path)
self.assertEqual( self.assertEqual(
testcase_python_list[0], 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): def test_make_testcase_with_ref(self):
path = [ path = [
"examples/postman_echo/request_methods/request_with_testcase_reference.yml" "examples/postman_echo/request_methods/request_with_testcase_reference.yml"
] ]
make_files_cache_set.clear() pytest_files_made_cache_mapping.clear()
pytest_files_set.clear() pytest_files_run_set.clear()
testcase_python_list = main_make(path) testcase_python_list = main_make(path)
self.assertEqual(len(testcase_python_list), 1) self.assertEqual(len(testcase_python_list), 1)
self.assertIn( 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, testcase_python_list,
) )
@@ -52,7 +59,10 @@ from examples.postman_echo.request_methods.request_with_functions_test import (
path = ["examples/postman_echo/request_methods/"] path = ["examples/postman_echo/request_methods/"]
testcase_python_list = main_make(path) testcase_python_list = main_make(path)
self.assertIn( 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, testcase_python_list,
) )
@@ -92,16 +102,22 @@ from examples.postman_echo.request_methods.request_with_functions_test import (
def test_make_testsuite(self): def test_make_testsuite(self):
path = ["examples/postman_echo/request_methods/demo_testsuite.yml"] path = ["examples/postman_echo/request_methods/demo_testsuite.yml"]
make_files_cache_set.clear() pytest_files_made_cache_mapping.clear()
pytest_files_set.clear() pytest_files_run_set.clear()
testcase_python_list = main_make(path) testcase_python_list = main_make(path)
self.assertEqual(len(testcase_python_list), 2) self.assertEqual(len(testcase_python_list), 2)
self.assertIn( 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, testcase_python_list,
) )
self.assertIn( 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, testcase_python_list,
) )