From 1ac4bafd396213d5dd7ec9e2b97d4d34ddc3f68e Mon Sep 17 00:00:00 2001 From: debugtalk Date: Thu, 5 Dec 2019 23:35:33 +0800 Subject: [PATCH] refactor: change function name to avoid conflict with unittest loader --- httprunner/api.py | 2 +- httprunner/loader/__init__.py | 6 ++--- httprunner/loader/cases.py | 6 ++--- httprunner/loader/locate.py | 4 ++-- httprunner/plugins/locusts/utils.py | 2 +- tests/test_api.py | 26 +++++++++++----------- tests/test_context.py | 2 +- tests/test_loader/test_cases.py | 34 ++++++++++++++--------------- tests/test_parser.py | 8 +++---- tests/test_runner.py | 6 ++--- 10 files changed, 48 insertions(+), 48 deletions(-) diff --git a/httprunner/api.py b/httprunner/api.py index 04ae785a..99b10aa9 100644 --- a/httprunner/api.py +++ b/httprunner/api.py @@ -257,7 +257,7 @@ class HttpRunner(object): """ # load tests self.exception_stage = "load tests" - tests_mapping = loader.load_tests(path, dot_env_path) + tests_mapping = loader.load_cases(path, dot_env_path) if mapping: tests_mapping["project_mapping"]["variables"] = mapping diff --git a/httprunner/loader/__init__.py b/httprunner/loader/__init__.py index 0c4aba48..9cfcea4f 100644 --- a/httprunner/loader/__init__.py +++ b/httprunner/loader/__init__.py @@ -1,4 +1,4 @@ -from httprunner.loader.cases import load_tests, load_project_tests +from httprunner.loader.cases import load_cases, load_project_data from httprunner.loader.check import is_testcase_path, is_testcases, validate_json_file from httprunner.loader.load import load_csv_file, load_builtin_functions @@ -8,6 +8,6 @@ __all__ = [ "validate_json_file", "load_csv_file", "load_builtin_functions", - "load_project_tests", - "load_tests" + "load_project_data", + "load_cases" ] diff --git a/httprunner/loader/cases.py b/httprunner/loader/cases.py index d273582c..14c7b3bd 100644 --- a/httprunner/loader/cases.py +++ b/httprunner/loader/cases.py @@ -448,7 +448,7 @@ def load_api_folder(api_folder_path): return api_definition_mapping -def load_project_tests(test_path, dot_env_path=None): +def load_project_data(test_path, dot_env_path=None): """ load api, testcases, .env, debugtalk.py functions. api/testcases folder is relative to project_working_directory @@ -491,7 +491,7 @@ def load_project_tests(test_path, dot_env_path=None): return project_mapping -def load_tests(path, dot_env_path=None): +def load_cases(path, dot_env_path=None): """ load testcases from file path, extend and merge with api/testcase definitions. Args: @@ -546,7 +546,7 @@ def load_tests(path, dot_env_path=None): """ tests_mapping = { - "project_mapping": load_project_tests(path, dot_env_path) + "project_mapping": load_project_data(path, dot_env_path) } def __load_file_content(path): diff --git a/httprunner/loader/locate.py b/httprunner/loader/locate.py index 6dfb2d6c..0ac21161 100644 --- a/httprunner/loader/locate.py +++ b/httprunner/loader/locate.py @@ -62,7 +62,7 @@ def locate_debugtalk_py(start_path): def init_project_working_directory(test_path): """ this should be called at startup - init_project_working_directory <- load_project_tests <- load_tests <- run + init_project_working_directory <- load_project_data <- load_cases <- run Args: test_path: specified testfile path @@ -105,6 +105,6 @@ def init_project_working_directory(test_path): def get_project_working_directory(): global project_working_directory if project_working_directory is None: - raise exceptions.MyBaseFailure("loader.load_tests() has not been called!") + raise exceptions.MyBaseFailure("loader.load_cases() has not been called!") return project_working_directory diff --git a/httprunner/plugins/locusts/utils.py b/httprunner/plugins/locusts/utils.py index ab7cef91..e1d5d881 100644 --- a/httprunner/plugins/locusts/utils.py +++ b/httprunner/plugins/locusts/utils.py @@ -16,7 +16,7 @@ def prepare_locust_tests(path): ] """ - tests_mapping = loader.load_tests(path) + tests_mapping = loader.load_cases(path) testcases = parser.parse_tests(tests_mapping) locust_tests = [] diff --git a/tests/test_api.py b/tests/test_api.py index 05ffc3aa..cca20309 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -333,7 +333,7 @@ class TestHttpRunner(ApiServerUnittest): ] tests_mapping = { - "project_mapping": loader.load_project_tests("tests"), + "project_mapping": loader.load_project_data("tests"), "testcases": testcases } summary = self.runner.run_tests(tests_mapping) @@ -364,7 +364,7 @@ class TestHttpRunner(ApiServerUnittest): } ] tests_mapping = { - "project_mapping": loader.load_project_tests("tests"), + "project_mapping": loader.load_project_data("tests"), "testcases": testcases } summary = self.runner.run_tests(tests_mapping) @@ -393,7 +393,7 @@ class TestHttpRunner(ApiServerUnittest): } ] tests_mapping = { - "project_mapping": loader.load_project_tests("tests"), + "project_mapping": loader.load_project_data("tests"), "testcases": testcases } summary = self.runner.run_tests(tests_mapping) @@ -602,7 +602,7 @@ class TestApi(ApiServerUnittest): def test_testcase_loader(self): testcase_path = "tests/testcases/setup.yml" - tests_mapping = loader.load_tests(testcase_path) + tests_mapping = loader.load_cases(testcase_path) project_mapping = tests_mapping["project_mapping"] self.assertIsInstance(project_mapping, dict) @@ -627,7 +627,7 @@ class TestApi(ApiServerUnittest): def test_testcase_parser(self): testcase_path = "tests/testcases/setup.yml" - tests_mapping = loader.load_tests(testcase_path) + tests_mapping = loader.load_cases(testcase_path) parsed_testcases = parser.parse_tests(tests_mapping) @@ -648,7 +648,7 @@ class TestApi(ApiServerUnittest): def test_testcase_add_tests(self): testcase_path = "tests/testcases/setup.yml" - tests_mapping = loader.load_tests(testcase_path) + tests_mapping = loader.load_cases(testcase_path) testcases = parser.parse_tests(tests_mapping) runner = HttpRunner() @@ -662,7 +662,7 @@ class TestApi(ApiServerUnittest): def test_testcase_complex_verify(self): testcase_path = "tests/testcases/create_user.yml" - tests_mapping = loader.load_tests(testcase_path) + tests_mapping = loader.load_cases(testcase_path) testcases = parser.parse_tests(tests_mapping) teststeps = testcases[0]["teststeps"] @@ -679,7 +679,7 @@ class TestApi(ApiServerUnittest): def test_testcase_simple_run_suite(self): testcase_path = "tests/testcases/setup.yml" - tests_mapping = loader.load_tests(testcase_path) + tests_mapping = loader.load_cases(testcase_path) testcases = parser.parse_tests(tests_mapping) runner = HttpRunner() test_suite = runner._add_tests(testcases) @@ -693,7 +693,7 @@ class TestApi(ApiServerUnittest): "tests/testcases/create_user.json", "tests/testcases/create_user.v2.json" ]: - tests_mapping = loader.load_tests(testcase_path) + tests_mapping = loader.load_cases(testcase_path) testcases = parser.parse_tests(tests_mapping) runner = HttpRunner() test_suite = runner._add_tests(testcases) @@ -712,7 +712,7 @@ class TestApi(ApiServerUnittest): def test_testsuite_loader(self): testcase_path = "tests/testsuites/create_users.yml" - tests_mapping = loader.load_tests(testcase_path) + tests_mapping = loader.load_cases(testcase_path) project_mapping = tests_mapping["project_mapping"] self.assertIsInstance(project_mapping, dict) @@ -744,7 +744,7 @@ class TestApi(ApiServerUnittest): def test_testsuite_parser(self): testcase_path = "tests/testsuites/create_users.yml" - tests_mapping = loader.load_tests(testcase_path) + tests_mapping = loader.load_cases(testcase_path) parsed_testcases = parser.parse_tests(tests_mapping) self.assertEqual(len(parsed_testcases), 2) @@ -762,7 +762,7 @@ class TestApi(ApiServerUnittest): def test_testsuite_add_tests(self): testcase_path = "tests/testsuites/create_users.yml" - tests_mapping = loader.load_tests(testcase_path) + tests_mapping = loader.load_cases(testcase_path) testcases = parser.parse_tests(tests_mapping) runner = HttpRunner() @@ -774,7 +774,7 @@ class TestApi(ApiServerUnittest): def test_testsuite_run_suite(self): testcase_path = "tests/testsuites/create_users.yml" - tests_mapping = loader.load_tests(testcase_path) + tests_mapping = loader.load_cases(testcase_path) testcases = parser.parse_tests(tests_mapping) diff --git a/tests/test_context.py b/tests/test_context.py index a0c6b203..46bbf343 100644 --- a/tests/test_context.py +++ b/tests/test_context.py @@ -8,7 +8,7 @@ from tests.base import ApiServerUnittest, gen_random_string class TestContext(ApiServerUnittest): def setUp(self): - loader.load_project_tests(os.path.join(os.getcwd(), "tests")) + loader.load_project_data(os.path.join(os.getcwd(), "tests")) self.context = context.SessionContext( variables={"SECRET_KEY": "DebugTalk"} ) diff --git a/tests/test_loader/test_cases.py b/tests/test_loader/test_cases.py index 8aff6396..d3940c40 100644 --- a/tests/test_loader/test_cases.py +++ b/tests/test_loader/test_cases.py @@ -14,10 +14,10 @@ class TestModuleLoader(unittest.TestCase): self.assertNotIn("is_py3", module_functions) def test_load_debugtalk_module(self): - project_mapping = cases.load_project_tests(os.path.join(os.getcwd(), "httprunner")) + project_mapping = cases.load_project_data(os.path.join(os.getcwd(), "httprunner")) self.assertNotIn("alter_response", project_mapping["functions"]) - project_mapping = cases.load_project_tests(os.path.join(os.getcwd(), "tests")) + project_mapping = cases.load_project_data(os.path.join(os.getcwd(), "tests")) self.assertIn("alter_response", project_mapping["functions"]) is_status_code_200 = project_mapping["functions"]["is_status_code_200"] @@ -25,7 +25,7 @@ class TestModuleLoader(unittest.TestCase): self.assertFalse(is_status_code_200(500)) def test_load_debugtalk_py(self): - project_mapping = cases.load_project_tests("tests/data/demo_testcase.yml") + project_mapping = cases.load_project_data("tests/data/demo_testcase.yml") project_working_directory = project_mapping["PWD"] debugtalk_functions = project_mapping["functions"] self.assertEqual( @@ -34,7 +34,7 @@ class TestModuleLoader(unittest.TestCase): ) self.assertIn("gen_md5", debugtalk_functions) - project_mapping = cases.load_project_tests("tests/base.py") + project_mapping = cases.load_project_data("tests/base.py") project_working_directory = project_mapping["PWD"] debugtalk_functions = project_mapping["functions"] self.assertEqual( @@ -43,7 +43,7 @@ class TestModuleLoader(unittest.TestCase): ) self.assertIn("gen_md5", debugtalk_functions) - project_mapping = cases.load_project_tests("httprunner/__init__.py") + project_mapping = cases.load_project_data("httprunner/__init__.py") project_working_directory = project_mapping["PWD"] debugtalk_functions = project_mapping["functions"] self.assertEqual( @@ -57,7 +57,7 @@ class TestSuiteLoader(unittest.TestCase): @classmethod def setUpClass(cls): - cls.project_mapping = cases.load_project_tests(os.path.join(os.getcwd(), "tests")) + cls.project_mapping = cases.load_project_data(os.path.join(os.getcwd(), "tests")) cls.tests_def_mapping = cases.tests_def_mapping def test_load_teststep_api(self): @@ -162,7 +162,7 @@ class TestSuiteLoader(unittest.TestCase): def test_load_tests_api_file(self): path = os.path.join( os.getcwd(), 'tests/api/create_user.yml') - tests_mapping = loader.load_tests(path) + tests_mapping = loader.load_cases(path) project_mapping = tests_mapping["project_mapping"] api_list = tests_mapping["apis"] self.assertEqual(len(api_list), 1) @@ -172,7 +172,7 @@ class TestSuiteLoader(unittest.TestCase): # absolute file path path = os.path.join( os.getcwd(), 'tests/data/demo_testcase_hardcode.json') - tests_mapping = loader.load_tests(path) + tests_mapping = loader.load_cases(path) project_mapping = tests_mapping["project_mapping"] testcases_list = tests_mapping["testcases"] self.assertEqual(len(testcases_list), 1) @@ -181,7 +181,7 @@ class TestSuiteLoader(unittest.TestCase): # relative file path path = 'tests/data/demo_testcase_hardcode.yml' - tests_mapping = loader.load_tests(path) + tests_mapping = loader.load_cases(path) project_mapping = tests_mapping["project_mapping"] testcases_list = tests_mapping["testcases"] self.assertEqual(len(testcases_list), 1) @@ -191,7 +191,7 @@ class TestSuiteLoader(unittest.TestCase): def test_load_tests_testcase_file_2(self): testcase_file_path = os.path.join( os.getcwd(), 'tests/data/demo_testcase.yml') - tests_mapping = loader.load_tests(testcase_file_path) + tests_mapping = loader.load_cases(testcase_file_path) testcases = tests_mapping["testcases"] self.assertIsInstance(testcases, list) self.assertEqual(testcases[0]["config"]["name"], '123t$var_a') @@ -211,7 +211,7 @@ class TestSuiteLoader(unittest.TestCase): def test_load_tests_testcase_file_with_api_ref(self): path = os.path.join( os.getcwd(), 'tests/data/demo_testcase_layer.yml') - tests_mapping = loader.load_tests(path) + tests_mapping = loader.load_cases(path) project_mapping = tests_mapping["project_mapping"] testcases_list = tests_mapping["testcases"] self.assertIn('device_sn', testcases_list[0]["config"]["variables"]) @@ -231,7 +231,7 @@ class TestSuiteLoader(unittest.TestCase): def test_load_tests_testsuite_file_with_testcase_ref(self): path = os.path.join( os.getcwd(), 'tests/testsuites/create_users.yml') - tests_mapping = loader.load_tests(path) + tests_mapping = loader.load_cases(path) project_mapping = tests_mapping["project_mapping"] testsuites_list = tests_mapping["testsuites"] @@ -256,13 +256,13 @@ class TestSuiteLoader(unittest.TestCase): def test_load_tests_folder_path(self): # absolute folder path path = os.path.join(os.getcwd(), 'tests/data') - tests_mapping = loader.load_tests(path) + tests_mapping = loader.load_cases(path) testcase_list_1 = tests_mapping["testcases"] self.assertGreater(len(testcase_list_1), 4) # relative folder path path = 'tests/data/' - tests_mapping = loader.load_tests(path) + tests_mapping = loader.load_cases(path) testcase_list_2 = tests_mapping["testcases"] self.assertEqual(len(testcase_list_1), len(testcase_list_2)) @@ -270,12 +270,12 @@ class TestSuiteLoader(unittest.TestCase): # absolute folder path path = os.path.join(os.getcwd(), 'tests/data_not_exist') with self.assertRaises(exceptions.FileNotFound): - loader.load_tests(path) + loader.load_cases(path) # relative folder path path = 'tests/data_not_exist' with self.assertRaises(exceptions.FileNotFound): - loader.load_tests(path) + loader.load_cases(path) def test_load_api_folder(self): path = os.path.join(os.getcwd(), "tests", "api") @@ -285,7 +285,7 @@ class TestSuiteLoader(unittest.TestCase): self.assertIn("request", api_definition_mapping[api_file_path]) def test_load_project_tests(self): - cases.load_project_tests(os.path.join(os.getcwd(), "tests")) + cases.load_project_data(os.path.join(os.getcwd(), "tests")) api_file_path = os.path.join(os.getcwd(), "tests", "api", "get_token.yml") self.assertIn(api_file_path, self.tests_def_mapping["api"]) self.assertEqual(self.project_mapping["env"]["PROJECT_KEY"], "ABCDEFGH") diff --git a/tests/test_parser.py b/tests/test_parser.py index 1195e551..400cdcbb 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -955,7 +955,7 @@ class TestParser(unittest.TestCase): ) def test_parse_parameters_parameterize(self): - loader.load_project_tests(os.path.join(os.getcwd(), "tests")) + loader.load_project_data(os.path.join(os.getcwd(), "tests")) parameters = [ {"app_version": "${parameterize(data/app_version.csv)}"}, {"username-password": "${parameterize(data/account.csv)}"} @@ -967,7 +967,7 @@ class TestParser(unittest.TestCase): ) def test_parse_parameters_mix(self): - project_mapping = loader.load_project_tests(os.path.join(os.getcwd(), "tests")) + project_mapping = loader.load_project_data(os.path.join(os.getcwd(), "tests")) parameters = [ {"user_agent": ["iOS/10.1", "iOS/10.2", "iOS/10.3"]}, @@ -984,7 +984,7 @@ class TestParser(unittest.TestCase): def test_parse_tests_testcase(self): testcase_file_path = os.path.join( os.getcwd(), 'tests/data/demo_testcase.yml') - tests_mapping = loader.load_tests(testcase_file_path) + tests_mapping = loader.load_cases(testcase_file_path) testcases = tests_mapping["testcases"] self.assertEqual( testcases[0]["config"]["variables"]["var_c"], @@ -1370,7 +1370,7 @@ class TestParser(unittest.TestCase): parser.eval_lazy_data(content) def test_extend_with_api(self): - loader.load_project_tests(os.path.join(os.getcwd(), "tests")) + loader.load_project_data(os.path.join(os.getcwd(), "tests")) raw_testinfo = { "name": "get token", "base_url": "https://github.com", diff --git a/tests/test_runner.py b/tests/test_runner.py index db8e83b5..a3ec6b9a 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -10,7 +10,7 @@ from tests.base import ApiServerUnittest class TestRunner(ApiServerUnittest): def setUp(self): - project_mapping = cases.load_project_tests(os.path.join(os.getcwd(), "tests")) + project_mapping = cases.load_project_data(os.path.join(os.getcwd(), "tests")) self.debugtalk_functions = project_mapping["functions"] config = { @@ -35,7 +35,7 @@ class TestRunner(ApiServerUnittest): ] for testcase_file_path in testcase_file_path_list: - tests_mapping = loader.load_tests(testcase_file_path) + tests_mapping = loader.load_cases(testcase_file_path) parsed_testcases = parser.parse_tests(tests_mapping) parsed_testcase = parsed_testcases[0] test_runner = runner.Runner(parsed_testcase["config"]) @@ -289,7 +289,7 @@ class TestRunner(ApiServerUnittest): def test_bugfix_type_match(self): testcase_file_path = os.path.join( os.getcwd(), 'tests/data/bugfix_type_match.yml') - tests_mapping = loader.load_tests(testcase_file_path) + tests_mapping = loader.load_cases(testcase_file_path) parsed_testcases = parser.parse_tests(tests_mapping) parsed_testcase = parsed_testcases[0] test_runner = runner.Runner(parsed_testcase["config"])