From 630b9e7e8a440cf00a2ac9254ff26fb7abfaa960 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Thu, 29 Nov 2018 19:31:11 +0800 Subject: [PATCH] fix base_url: teststep have base_url but is empty --- httprunner/parser.py | 17 ++++++++++------- tests/test_parser.py | 33 +++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/httprunner/parser.py b/httprunner/parser.py index 76f9dc3d..c2001cb3 100644 --- a/httprunner/parser.py +++ b/httprunner/parser.py @@ -616,7 +616,7 @@ def _extend_with_api(test_dict, api_def_dict): test_dict["request"] = api_def_dict.pop("request", {}) # base_url & verify: priority api_def_dict > test_dict - if "base_url" in api_def_dict: + if api_def_dict.get("base_url"): test_dict["base_url"] = api_def_dict["base_url"] if "verify" in api_def_dict: @@ -661,9 +661,11 @@ def _extend_with_testcase(test_dict, testcase_def_dict): # override base_url, verify # priority: testcase config > testsuite tests - test_base_url = test_dict.pop("base_url", None) + test_base_url = test_dict.pop("base_url", "") + if not testcase_def_dict["config"].get("base_url"): + testcase_def_dict["config"]["base_url"] = test_base_url + test_verify = test_dict.pop("verify", True) - testcase_def_dict["config"].setdefault("base_url", test_base_url) testcase_def_dict["config"].setdefault("verify", test_verify) # override testcase config name, output, etc. @@ -738,15 +740,16 @@ def __parse_tests(tests, config, project_mapping): """ config_variables = config.pop("variables", {}) - config_base_url = config.pop("base_url", None) + config_base_url = config.pop("base_url", "") config_verify = config.pop("verify", True) functions = project_mapping.get("functions", {}) for test_dict in tests: # base_url & verify: priority test_dict > config - if config_base_url: - test_dict.setdefault("base_url", config_base_url) + if (not test_dict.get("base_url")) and config_base_url: + test_dict["base_url"] = config_base_url + test_dict.setdefault("verify", config_verify) if "testcase_def" in test_dict: @@ -800,7 +803,7 @@ def __parse_tests(tests, config, project_mapping): api_def_dict = test_dict.pop("api_def") _extend_with_api(test_dict, api_def_dict) - if "base_url" in test_dict: + if test_dict.get("base_url"): base_url = parse_data( test_dict.pop("base_url"), test_dict["variables"], diff --git a/tests/test_parser.py b/tests/test_parser.py index 43f15f55..3b7f613b 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -499,10 +499,6 @@ class TestParser(unittest.TestCase): { 'name': 'testcase1', "base_url": "https://httprunner.org", - "variables": [ - {"creator": "user_test_002"}, - {"username": "$creator"} - ], 'request': {'url': '/api1', 'method': 'GET', "verify": True} } ] @@ -569,6 +565,35 @@ class TestParser(unittest.TestCase): test_dict = tests_mapping["testcases"][0]["tests"][0] self.assertEqual(test_dict["request"]["url"], "https://httprunner.org/api1") + def test_parse_tests_base_url_teststep_empty(self): + """ base_url & verify: priority test_dict > config + """ + tests_mapping = { + 'testcases': [ + { + 'config': { + 'name': '', + "base_url": "$host", + 'variables': { + "host": "https://debugtalk" + }, + "verify": False + }, + 'tests': [ + { + 'name': 'testcase1', + "base_url": "", + 'request': {'url': '/api1', 'method': 'GET', "verify": True} + } + ] + } + ] + } + parser.parse_tests(tests_mapping) + test_dict = tests_mapping["testcases"][0]["tests"][0] + self.assertEqual(test_dict["request"]["url"], "https://debugtalk/api1") + self.assertEqual(test_dict["request"]["verify"], True) + def test_parse_environ(self): os.environ["PROJECT_KEY"] = "ABCDEFGH" content = {