mirror of
https://github.com/httprunner/httprunner.git
synced 2026-08-25 01:59:52 +08:00
change method name
This commit is contained in:
@@ -9,22 +9,22 @@ class ResponseObject(object):
|
|||||||
"""
|
"""
|
||||||
self.resp_obj = resp_obj
|
self.resp_obj = resp_obj
|
||||||
|
|
||||||
def parse_response_body(self):
|
def parsed_body(self):
|
||||||
try:
|
try:
|
||||||
return self.resp_obj.json()
|
return self.resp_obj.json()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return self.resp_obj.text
|
return self.resp_obj.text
|
||||||
|
|
||||||
def parse_response_object(self):
|
def parsed_dict(self):
|
||||||
return {
|
return {
|
||||||
'status_code': self.resp_obj.status_code,
|
'status_code': self.resp_obj.status_code,
|
||||||
'headers': self.resp_obj.headers,
|
'headers': self.resp_obj.headers,
|
||||||
'body': self.parse_response_body()
|
'body': self.parsed_body()
|
||||||
}
|
}
|
||||||
|
|
||||||
def diff_response(self, expected_resp_json):
|
def diff_response(self, expected_resp_json):
|
||||||
diff_content = {}
|
diff_content = {}
|
||||||
resp_info = self.parse_response_object()
|
resp_info = self.parsed_dict()
|
||||||
|
|
||||||
expected_status_code = expected_resp_json.get('status_code', 200)
|
expected_status_code = expected_resp_json.get('status_code', 200)
|
||||||
if resp_info['status_code'] != int(expected_status_code):
|
if resp_info['status_code'] != int(expected_status_code):
|
||||||
@@ -81,7 +81,7 @@ class ResponseObject(object):
|
|||||||
top_query, sub_query = value.split(delimiter, 1)
|
top_query, sub_query = value.split(delimiter, 1)
|
||||||
|
|
||||||
if top_query in ["body", "content", "text"]:
|
if top_query in ["body", "content", "text"]:
|
||||||
json_content = self.parse_response_body()
|
json_content = self.parsed_body()
|
||||||
else:
|
else:
|
||||||
json_content = getattr(self.resp_obj, top_query)
|
json_content = getattr(self.resp_obj, top_query)
|
||||||
|
|
||||||
@@ -98,3 +98,8 @@ class ResponseObject(object):
|
|||||||
|
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
raise exception.ParamsError("invalid extract_binds!")
|
raise exception.ParamsError("invalid extract_binds!")
|
||||||
|
|
||||||
|
def validate(self, expected_resp_json):
|
||||||
|
diff_content = self.diff_response(expected_resp_json)
|
||||||
|
success = False if diff_content else True
|
||||||
|
return success, diff_content
|
||||||
|
|||||||
@@ -107,9 +107,7 @@ class TestRunner(object):
|
|||||||
|
|
||||||
resp_obj = response.ResponseObject(resp)
|
resp_obj = response.ResponseObject(resp)
|
||||||
resp_obj.extract_response(self.context)
|
resp_obj.extract_response(self.context)
|
||||||
diff_content = resp_obj.diff_response(testcase['response'])
|
return resp_obj.validate(testcase['response'])
|
||||||
success = False if diff_content else True
|
|
||||||
return success, diff_content
|
|
||||||
|
|
||||||
def run_testset(self, testset):
|
def run_testset(self, testset):
|
||||||
""" run single testset, including one or several testcases.
|
""" run single testset, including one or several testcases.
|
||||||
|
|||||||
@@ -9,25 +9,25 @@ class TestResponse(ApiServerUnittest):
|
|||||||
url = "http://127.0.0.1:5000/api/users"
|
url = "http://127.0.0.1:5000/api/users"
|
||||||
resp = requests.get(url)
|
resp = requests.get(url)
|
||||||
resp_obj = response.ResponseObject(resp)
|
resp_obj = response.ResponseObject(resp)
|
||||||
parse_result = resp_obj.parse_response_object()
|
parsed_dict = resp_obj.parsed_dict()
|
||||||
self.assertIn('status_code', parse_result)
|
self.assertIn('status_code', parsed_dict)
|
||||||
self.assertIn('headers', parse_result)
|
self.assertIn('headers', parsed_dict)
|
||||||
self.assertIn('body', parse_result)
|
self.assertIn('body', parsed_dict)
|
||||||
self.assertIn('Content-Type', parse_result['headers'])
|
self.assertIn('Content-Type', parsed_dict['headers'])
|
||||||
self.assertIn('Content-Length', parse_result['headers'])
|
self.assertIn('Content-Length', parsed_dict['headers'])
|
||||||
self.assertIn('success', parse_result['body'])
|
self.assertIn('success', parsed_dict['body'])
|
||||||
|
|
||||||
def test_parse_response_object_text(self):
|
def test_parse_response_object_text(self):
|
||||||
url = "http://127.0.0.1:5000/"
|
url = "http://127.0.0.1:5000/"
|
||||||
resp = requests.get(url)
|
resp = requests.get(url)
|
||||||
resp_obj = response.ResponseObject(resp)
|
resp_obj = response.ResponseObject(resp)
|
||||||
parse_result = resp_obj.parse_response_object()
|
parsed_dict = resp_obj.parsed_dict()
|
||||||
self.assertIn('status_code', parse_result)
|
self.assertIn('status_code', parsed_dict)
|
||||||
self.assertIn('headers', parse_result)
|
self.assertIn('headers', parsed_dict)
|
||||||
self.assertIn('body', parse_result)
|
self.assertIn('body', parsed_dict)
|
||||||
self.assertIn('Content-Type', parse_result['headers'])
|
self.assertIn('Content-Type', parsed_dict['headers'])
|
||||||
self.assertIn('Content-Length', parse_result['headers'])
|
self.assertIn('Content-Length', parsed_dict['headers'])
|
||||||
self.assertTrue(str, type(parse_result['body']))
|
self.assertTrue(str, type(parsed_dict['body']))
|
||||||
|
|
||||||
def test_diff_response_status_code_equal(self):
|
def test_diff_response_status_code_equal(self):
|
||||||
status_code = random.randint(200, 511)
|
status_code = random.randint(200, 511)
|
||||||
|
|||||||
Reference in New Issue
Block a user