mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-07 15:31:23 +08:00
refactor teardown_hooks set attribute: remove response attributes interlayer
This commit is contained in:
@@ -18,7 +18,6 @@ class ResponseObject(object):
|
|||||||
@param (requests.Response instance) resp_obj
|
@param (requests.Response instance) resp_obj
|
||||||
"""
|
"""
|
||||||
self.resp_obj = resp_obj
|
self.resp_obj = resp_obj
|
||||||
self.attributes = {}
|
|
||||||
|
|
||||||
def __getattr__(self, key):
|
def __getattr__(self, key):
|
||||||
try:
|
try:
|
||||||
@@ -154,17 +153,23 @@ class ResponseObject(object):
|
|||||||
raise exceptions.ExtractFailure(err_msg)
|
raise exceptions.ExtractFailure(err_msg)
|
||||||
|
|
||||||
# new set response attributes in teardown_hooks
|
# 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:
|
if not sub_query:
|
||||||
# extract response attributes
|
# extract response attributes
|
||||||
return self.attributes
|
return attributes
|
||||||
|
|
||||||
if sub_query in self.attributes:
|
if isinstance(attributes, (dict, list)):
|
||||||
return self.attributes[sub_query]
|
# 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:
|
else:
|
||||||
# content = "attributes.new_attribute_not_exist"
|
# content = "attributes.new_attribute_not_exist"
|
||||||
err_msg = u"Failed to extract cumstom set attribute from teardown hooks! => {}\n".format(field)
|
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)
|
logger.log_error(err_msg)
|
||||||
raise exceptions.TeardownHooksFailure(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"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"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"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)
|
logger.log_error(err_msg)
|
||||||
raise exceptions.ParamsError(err_msg)
|
raise exceptions.ParamsError(err_msg)
|
||||||
|
|
||||||
|
|||||||
@@ -102,7 +102,10 @@ def alter_response(response):
|
|||||||
response.status_code = 500
|
response.status_code = 500
|
||||||
response.headers["Content-Type"] = "html/text"
|
response.headers["Content-Type"] = "html/text"
|
||||||
response.json["headers"]["Host"] = "127.0.0.1:8888"
|
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):
|
def alter_response_error(response):
|
||||||
# NameError
|
# NameError
|
||||||
|
|||||||
@@ -187,7 +187,8 @@ class TestRunner(ApiServerUnittest):
|
|||||||
{"eq": ["json.headers.Host", "127.0.0.1:8888"]},
|
{"eq": ["json.headers.Host", "127.0.0.1:8888"]},
|
||||||
{"eq": ["content.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": ["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)}"
|
"${alter_response($response)}"
|
||||||
],
|
],
|
||||||
"validate": [
|
"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)
|
runner = HttpRunner().run(testsets)
|
||||||
summary = runner.summary
|
summary = runner.summary
|
||||||
self.assertFalse(summary["success"])
|
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):
|
def test_run_httprunner_with_teardown_hooks_error(self):
|
||||||
testsets = [
|
testsets = [
|
||||||
|
|||||||
Reference in New Issue
Block a user