diff --git a/httprunner/__about__.py b/httprunner/__about__.py index 4193ecce..8164a250 100644 --- a/httprunner/__about__.py +++ b/httprunner/__about__.py @@ -1,7 +1,7 @@ __title__ = 'HttpRunner' __description__ = 'One-stop solution for HTTP(S) testing.' __url__ = 'https://github.com/HttpRunner/HttpRunner' -__version__ = '2.0.2' +__version__ = '2.0.3' __author__ = 'debugtalk' __author_email__ = 'mail@debugtalk.com' __license__ = 'Apache-2.0' diff --git a/httprunner/parser.py b/httprunner/parser.py index cd033299..94d7d822 100644 --- a/httprunner/parser.py +++ b/httprunner/parser.py @@ -794,9 +794,12 @@ def __parse_testcase_tests(tests, config, project_mapping): variables priority: testcase config > testcase test > testcase_def config > testcase_def test > api - base_url/verify priority: + base_url priority: testcase test > testcase config > testsuite test > testsuite config > api + verify priority: + testcase teststep (api) > testcase config > testsuite config + Args: tests (list): config (dict): @@ -814,8 +817,6 @@ def __parse_testcase_tests(tests, config, project_mapping): if (not test_dict.get("base_url")) and config_base_url: test_dict["base_url"] = config_base_url - test_dict.setdefault("verify", config_verify) - # 1, testcase config => testcase tests # override test_dict variables test_dict["variables"] = utils.extend_variables( @@ -875,6 +876,10 @@ def __parse_testcase_tests(tests, config, project_mapping): request_url ) + # verify priority: testcase teststep > testcase config + if "request" in test_dict and "verify" not in test_dict["request"]: + test_dict["request"]["verify"] = config_verify + def _parse_testcase(testcase, project_mapping): """ parse testcase diff --git a/tests/data/test_bugfix.yml b/tests/data/bugfix_type_match.yml similarity index 51% rename from tests/data/test_bugfix.yml rename to tests/data/bugfix_type_match.yml index 2297e119..5d3bd3d2 100644 --- a/tests/data/test_bugfix.yml +++ b/tests/data/bugfix_type_match.yml @@ -1,16 +1,6 @@ - config: name: "bugfix testcases." - request: - base_url: http://127.0.0.1:5000 - headers: - Content-Type: application/json - -- test: - name: get headers from config - request: - url: /api/users/1000 - method: GET - headers: + base_url: http://127.0.0.1:5000 - test: name: bugfix type_match #84 diff --git a/tests/test_api.py b/tests/test_api.py index ba2b0ad0..4915dcd1 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -491,6 +491,10 @@ class TestApi(ApiServerUnittest): self.assertNotIn("api_def", test_dict1) self.assertEqual(test_dict1["variables"]["device_sn"], "TESTCASE_SETUP_XXX") self.assertEqual(test_dict1["request"]["url"], "http://127.0.0.1:5000/api/get-token") + self.assertEqual(test_dict1["request"]["verify"], False) + + test_dict2 = parsed_testcases[0]["teststeps"][1] + self.assertEqual(test_dict2["request"]["verify"], False) def test_testcase_add_tests(self): testcase_path = "tests/testcases/setup.yml" @@ -506,6 +510,22 @@ class TestApi(ApiServerUnittest): self.assertEqual(teststeps[0]["variables"]["device_sn"], "TESTCASE_SETUP_XXX") self.assertIn("api", teststeps[0]) + def test_testcase_complex_verify(self): + testcase_path = "tests/testcases/create_and_check.yml" + tests_mapping = loader.load_tests(testcase_path) + parsed_tests_mapping = parser.parse_tests(tests_mapping) + teststeps = parsed_tests_mapping["testcases"][0]["teststeps"] + + # testcases/setup.yml + teststep1 = teststeps[0] + self.assertEqual(teststep1["teststeps"][0]["request"]["verify"], False) + self.assertEqual(teststep1["teststeps"][1]["request"]["verify"], False) + + # testcases/create_and_check.yml teststep 2/3/4 + self.assertEqual(teststeps[1]["request"]["verify"], True) + self.assertEqual(teststeps[2]["request"]["verify"], True) + self.assertEqual(teststeps[3]["request"]["verify"], True) + def test_testcase_simple_run_suite(self): testcase_path = "tests/testcases/setup.yml" tests_mapping = loader.load_tests(testcase_path) diff --git a/tests/test_parser.py b/tests/test_parser.py index 153086fa..8f97ea30 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -732,6 +732,77 @@ class TestParser(unittest.TestCase): self.assertEqual(test_dict["request"]["url"], "https://debugtalk.com/api1") self.assertEqual(test_dict["request"]["verify"], True) + def test_parse_tests_verify_config_set(self): + """ verify priority: test_dict > config + """ + tests_mapping = { + 'testcases': [ + { + "config": { + 'name': 'bugfix verify', + "base_url": "https://httpbin.org/", + "verify": False + }, + "teststeps": [ + { + 'name': 'testcase1', + 'request': {'url': '/headers', 'method': 'GET'} + } + ] + } + ] + } + parsed_tests_mapping = parser.parse_tests(tests_mapping) + test_dict = parsed_tests_mapping["testcases"][0]["teststeps"][0] + self.assertEqual(test_dict["request"]["verify"], False) + + def test_parse_tests_verify_config_unset(self): + """ verify priority: test_dict > config + """ + tests_mapping = { + 'testcases': [ + { + "config": { + 'name': 'bugfix verify', + "base_url": "https://httpbin.org/", + }, + "teststeps": [ + { + 'name': 'testcase1', + 'request': {'url': '/headers', 'method': 'GET'} + } + ] + } + ] + } + parsed_tests_mapping = parser.parse_tests(tests_mapping) + test_dict = parsed_tests_mapping["testcases"][0]["teststeps"][0] + self.assertEqual(test_dict["request"]["verify"], True) + + def test_parse_tests_verify_step_set_false(self): + """ verify priority: test_dict > config + """ + tests_mapping = { + 'testcases': [ + { + "config": { + 'name': 'bugfix verify', + "base_url": "https://httpbin.org/", + "verify": True + }, + "teststeps": [ + { + 'name': 'testcase1', + 'request': {'url': '/headers', 'method': 'GET', "verify": False} + } + ] + } + ] + } + parsed_tests_mapping = parser.parse_tests(tests_mapping) + test_dict = parsed_tests_mapping["testcases"][0]["teststeps"][0] + self.assertEqual(test_dict["request"]["verify"], False) + def test_parse_environ(self): os.environ["PROJECT_KEY"] = "ABCDEFGH" content = { diff --git a/tests/test_runner.py b/tests/test_runner.py index 38d5f508..936c879b 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -238,25 +238,12 @@ class TestRunner(ApiServerUnittest): # check if teardown function executed self.assertGreater(end_time - start_time, 2) - def test_run_testcase_with_empty_header(self): - testcase_file_path = os.path.join( - os.getcwd(), 'tests/data/test_bugfix.yml') - tests_mapping = loader.load_tests(testcase_file_path) - testcase = tests_mapping["testcases"][0] - config_dict_headers = testcase["config"]["request"]["headers"] - test_dict_headers = testcase["teststeps"][0]["request"]["headers"] - headers = deep_update_dict( - config_dict_headers, - test_dict_headers - ) - self.assertEqual(headers["Content-Type"], "application/json") - def test_bugfix_type_match(self): testcase_file_path = os.path.join( - os.getcwd(), 'tests/data/test_bugfix.yml') + os.getcwd(), 'tests/data/bugfix_type_match.yml') testcases = loader.load_file(testcase_file_path) - test = testcases[2]["test"] + test = testcases[1]["test"] self.test_runner.run_test(test) def test_run_validate_elapsed(self): diff --git a/tests/testcases/create_and_check.yml b/tests/testcases/create_and_check.yml index 2126e778..0afd37c7 100644 --- a/tests/testcases/create_and_check.yml +++ b/tests/testcases/create_and_check.yml @@ -2,6 +2,7 @@ - config: name: "create user and check result." id: create_and_check + base_url: "http://127.0.0.1:5000" variables: uid: 9001 device_sn: "TESTCASE_CREATE_XXX"