From 52448357d0a7d5f892141304530b5cec15d73e93 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Fri, 27 Jul 2018 12:10:45 +0800 Subject: [PATCH] refactor teardown_hooks set attribute: remove response attributes interlayer --- httprunner/response.py | 19 ++++++++++++------- tests/debugtalk.py | 5 ++++- tests/test_runner.py | 7 ++++--- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/httprunner/response.py b/httprunner/response.py index 869fe950..547cae9f 100644 --- a/httprunner/response.py +++ b/httprunner/response.py @@ -18,7 +18,6 @@ class ResponseObject(object): @param (requests.Response instance) resp_obj """ self.resp_obj = resp_obj - self.attributes = {} def __getattr__(self, key): try: @@ -154,17 +153,23 @@ class ResponseObject(object): raise exceptions.ExtractFailure(err_msg) # new set response attributes in teardown_hooks - elif top_query == "attributes": + elif top_query in self.__dict__: + attributes = self.__dict__[top_query] + if not sub_query: # extract response attributes - return self.attributes + return attributes - if sub_query in self.attributes: - return self.attributes[sub_query] + if isinstance(attributes, (dict, list)): + # attributes = {"xxx": 123}, content.xxx + return utils.query_json(attributes, sub_query) + elif sub_query.isdigit(): + # attributes = "abcdefg", attributes.3 => d + return utils.query_json(attributes, sub_query) else: # content = "attributes.new_attribute_not_exist" err_msg = u"Failed to extract cumstom set attribute from teardown hooks! => {}\n".format(field) - err_msg += u"response set attributes: {}\n".format(self.attributes) + err_msg += u"response set attributes: {}\n".format(attributes) logger.log_error(err_msg) raise exceptions.TeardownHooksFailure(err_msg) @@ -173,7 +178,7 @@ class ResponseObject(object): err_msg = u"Failed to extract attribute from response! => {}\n".format(field) err_msg += u"available response attributes: status_code, cookies, elapsed, headers, content, text, json, encoding, ok, reason, url.\n\n" err_msg += u"If you want to set attribute in teardown_hooks, take the following example as reference:\n" - err_msg += u"response.attributes['new_attribute'] = 'new_attribute'\n" + err_msg += u"response.new_attribute = 'new_attribute_value'\n" logger.log_error(err_msg) raise exceptions.ParamsError(err_msg) diff --git a/tests/debugtalk.py b/tests/debugtalk.py index c6029c3e..da6d41ae 100644 --- a/tests/debugtalk.py +++ b/tests/debugtalk.py @@ -102,7 +102,10 @@ def alter_response(response): response.status_code = 500 response.headers["Content-Type"] = "html/text" response.json["headers"]["Host"] = "127.0.0.1:8888" - response.attributes["new_attribute"] = "new_attribute" + response.new_attribute = "new_attribute_value" + response.new_attribute_dict = { + "key": 123 + } def alter_response_error(response): # NameError diff --git a/tests/test_runner.py b/tests/test_runner.py index cd406211..a1ad5194 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -187,7 +187,8 @@ class TestRunner(ApiServerUnittest): {"eq": ["json.headers.Host", "127.0.0.1:8888"]}, {"eq": ["content.headers.Host", "127.0.0.1:8888"]}, {"eq": ["text.headers.Host", "127.0.0.1:8888"]}, - {"eq": ["attributes.new_attribute", "new_attribute"]} + {"eq": ["new_attribute", "new_attribute_value"]}, + {"eq": ["new_attribute_dict.key", 123]} ] } ] @@ -216,7 +217,7 @@ class TestRunner(ApiServerUnittest): "${alter_response($response)}" ], "validate": [ - {"eq": ["attributes.attribute_not_exist", "new_attribute"]} + {"eq": ["attribute_not_exist", "new_attribute"]} ] } ] @@ -225,7 +226,7 @@ class TestRunner(ApiServerUnittest): runner = HttpRunner().run(testsets) summary = runner.summary self.assertFalse(summary["success"]) - self.assertEqual(summary["stat"]["failures"], 1) + self.assertEqual(summary["stat"]["errors"], 1) def test_run_httprunner_with_teardown_hooks_error(self): testsets = [