bugfix: set repsonse attribute

This commit is contained in:
debugtalk
2018-07-26 22:33:35 +08:00
parent 20015ca10a
commit 4771dd272e
3 changed files with 82 additions and 0 deletions

View File

@@ -18,6 +18,7 @@ class ResponseObject(object):
@param (requests.Response instance) resp_obj
"""
self.resp_obj = resp_obj
self.attributes = {}
def __getattr__(self, key):
try:
@@ -152,6 +153,21 @@ class ResponseObject(object):
logger.log_error(err_msg)
raise exceptions.ExtractFailure(err_msg)
# new set response attributes
elif top_query == "attributes":
if not sub_query:
# extract response attributes
return self.attributes
if sub_query in self.attributes:
return self.attributes[sub_query]
else:
# content = "attributes.new_attribute_not_exist"
err_msg = u"Failed to extract cumstom set attribute! => {}\n".format(field)
err_msg += u"response set attributes: {}\n".format(self.attributes)
logger.log_error(err_msg)
raise exceptions.ExtractFailure(err_msg)
# others
else:
err_msg = u"Failed to extract attribute from response! => {}\n".format(field)

View File

@@ -102,3 +102,4 @@ 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"

View File

@@ -163,6 +163,71 @@ class TestRunner(ApiServerUnittest):
self.assertTrue(summary["success"])
self.assertLess(end_time - start_time, 1)
def test_run_httprunner_with_teardown_hooks_alter_response(self):
testsets = [
{
"name": "test teardown hooks",
"config": {
'path': 'tests/httpbin/hooks.yml',
},
"testcases": [
{
"name": "test teardown hooks",
"request": {
"url": "http://127.0.0.1:3458/headers",
"method": "GET",
"data": "abc"
},
"teardown_hooks": [
"${alter_response($response)}"
],
"validate": [
{"eq": ["status_code", 500]},
{"eq": ["headers.content-type", "html/text"]},
{"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"]}
]
}
]
}
]
runner = HttpRunner().run(testsets)
summary = runner.summary
self.assertTrue(summary["success"])
def test_run_httprunner_with_teardown_hooks_not_exist_attribute(self):
testsets = [
{
"name": "test teardown hooks",
"config": {
'path': 'tests/httpbin/hooks.yml',
},
"testcases": [
{
"name": "test teardown hooks",
"request": {
"url": "http://127.0.0.1:3458/headers",
"method": "GET",
"data": "abc"
},
"teardown_hooks": [
"${alter_response($response)}"
],
"validate": [
{"eq": ["attributes.attribute_not_exist", "new_attribute"]}
]
}
]
}
]
# with self.assertRaises(exceptions.AssertionError):
runner = HttpRunner().run(testsets)
summary = runner.summary
self.assertFalse(summary["success"])
def test_run_testset_with_teardown_hooks_success(self):
test = {
"name": "get token",