From 8b3c4442ae6167274ba57adf51263ff01fdb88a3 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Wed, 27 May 2020 20:58:40 +0800 Subject: [PATCH] fix: ensure pytest files are in python module, generate __init__.py on demand --- httprunner/ext/make/__init__.py | 18 ++++++++++++++---- httprunner/ext/make/make_test.py | 14 ++++++-------- httprunner/response.py | 4 +++- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/httprunner/ext/make/__init__.py b/httprunner/ext/make/__init__.py index 7d2f38d9..e6a1d6ef 100644 --- a/httprunner/ext/make/__init__.py +++ b/httprunner/ext/make/__init__.py @@ -56,10 +56,7 @@ def __ensure_file_name(path: Text) -> Text: """ filename = os.path.basename(path) if filename[0] in string.digits: - path = os.path.join( - os.path.dirname(path), - f"T{filename}" - ) + path = os.path.join(os.path.dirname(path), f"T{filename}") return path @@ -90,6 +87,17 @@ def __ensure_cwd_relative(path: Text) -> Text: return path +def __ensure_testcase_module(path: Text) -> NoReturn: + """ ensure pytest files are in python module, generate __init__.py on demand + """ + init_file = os.path.join(os.path.dirname(path), "__init__.py") + if os.path.isfile(init_file): + return + + with open(init_file, "w", encoding="utf-8") as f: + f.write("# NOTICE: Generated By HttpRunner. DO NOT EDIT!") + + def convert_testcase_path(testcase_path: Text) -> Tuple[Text, Text]: """convert single YAML/JSON testcase path to python file""" if os.path.isdir(testcase_path): @@ -194,6 +202,8 @@ def __make_testcase(testcase: Dict, dir_path: Text = None) -> NoReturn: with open(testcase_python_path, "w", encoding="utf-8") as f: f.write(content) + __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)) diff --git a/httprunner/ext/make/make_test.py b/httprunner/ext/make/make_test.py index b18ab0ee..f0b55818 100644 --- a/httprunner/ext/make/make_test.py +++ b/httprunner/ext/make/make_test.py @@ -31,13 +31,13 @@ class TestLoader(unittest.TestCase): self.assertIn( """ from examples.postman_echo.request_methods.request_with_functions_test import ( - TestCaseRequestWithFunctions, + TestCaseRequestWithFunctions as RequestWithFunctions, ) """, content, ) self.assertIn( - '"testcase": TestCaseRequestWithFunctions,', content, + '"testcase": RequestWithFunctions,', content, ) def test_make_testcase_folder(self): @@ -61,28 +61,26 @@ from examples.postman_echo.request_methods.request_with_functions_test import ( "/path/to 2/mubu_login_test.py", ) self.assertEqual( - convert_testcase_path("/path/to 2/mubu.login.yml")[1], "TestCaseMubuLogin" + convert_testcase_path("/path/to 2/mubu.login.yml")[1], "MubuLogin" ) self.assertEqual( convert_testcase_path("mubu login.yml")[0], "mubu_login_test.py" ) self.assertEqual( - convert_testcase_path("/path/to 2/mubu login.yml")[1], "TestCaseMubuLogin" + convert_testcase_path("/path/to 2/mubu login.yml")[1], "MubuLogin" ) self.assertEqual( convert_testcase_path("/path/to 2/mubu-login.yml")[0], "/path/to 2/mubu_login_test.py", ) self.assertEqual( - convert_testcase_path("/path/to 2/mubu-login.yml")[1], "TestCaseMubuLogin" + convert_testcase_path("/path/to 2/mubu-login.yml")[1], "MubuLogin" ) self.assertEqual( convert_testcase_path("/path/to 2/幕布login.yml")[0], "/path/to 2/幕布login_test.py", ) - self.assertEqual( - convert_testcase_path("/path/to/幕布login.yml")[1], "TestCase幕布Login" - ) + self.assertEqual(convert_testcase_path("/path/to/幕布login.yml")[1], "幕布Login") def test_make_testsuite(self): path = ["examples/postman_echo/request_methods/demo_testsuite.yml"] diff --git a/httprunner/response.py b/httprunner/response.py index 4c709e94..8c698542 100644 --- a/httprunner/response.py +++ b/httprunner/response.py @@ -166,7 +166,9 @@ class ResponseObject(object): check_item = u_validator["check"] if "$" in check_item: # check_item is variable or function - check_value = parse_data(check_item, variables_mapping, functions_mapping) + check_value = parse_data( + check_item, variables_mapping, functions_mapping + ) else: check_value = jmespath.search(check_item, self.resp_obj_meta)