mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-09 06:23:52 +08:00
optimize response validation
This commit is contained in:
@@ -5,3 +5,9 @@ class MyBaseError(BaseException):
|
|||||||
|
|
||||||
class ParamsError(MyBaseError):
|
class ParamsError(MyBaseError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class ParseResponseError(MyBaseError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class ValidationError(MyBaseError):
|
||||||
|
pass
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ class ResponseObject(object):
|
|||||||
return json_content
|
return json_content
|
||||||
|
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
raise exception.ParamsError("invalid extract_binds!")
|
raise exception.ParseResponseError("failed to extract bind variable in response!")
|
||||||
|
|
||||||
def extract_response(self, extract_binds):
|
def extract_response(self, extract_binds):
|
||||||
""" extract content from requests.Response
|
""" extract content from requests.Response
|
||||||
@@ -68,7 +68,7 @@ class ResponseObject(object):
|
|||||||
for extract_bind in extract_binds:
|
for extract_bind in extract_binds:
|
||||||
for key, field in extract_bind.items():
|
for key, field in extract_bind.items():
|
||||||
if not isinstance(field, utils.string_type):
|
if not isinstance(field, utils.string_type):
|
||||||
raise exception.ParamsError("invalid extract_binds!")
|
raise exception.ParamsError("invalid extract_binds in testcase extract_binds!")
|
||||||
|
|
||||||
extracted_variables_mapping_list.append(
|
extracted_variables_mapping_list.append(
|
||||||
{key: self.extract_field(field)}
|
{key: self.extract_field(field)}
|
||||||
@@ -99,19 +99,28 @@ class ResponseObject(object):
|
|||||||
|
|
||||||
for validator_dict in validators:
|
for validator_dict in validators:
|
||||||
|
|
||||||
if "expected" not in validator_dict or "check" not in validator_dict:
|
check_item = validator_dict.get("check")
|
||||||
raise exception.ParamsError("expected not specified in validator")
|
if not check_item:
|
||||||
|
raise exception.ParamsError("invalid check item in testcase validators!")
|
||||||
|
|
||||||
validator_key = validator_dict["check"]
|
if "expected" not in validator_dict:
|
||||||
try:
|
raise exception.ParamsError("expected item missed in testcase validators!")
|
||||||
validator_dict["value"] = variables_mapping[validator_key]
|
|
||||||
except KeyError:
|
expected = validator_dict.get("expected")
|
||||||
validator_dict["value"] = self.extract_field(validator_key)
|
comparator = validator_dict.get("comparator", "eq")
|
||||||
|
|
||||||
|
if check_item in variables_mapping:
|
||||||
|
validator_dict["actual_value"] = variables_mapping[check_item]
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
validator_dict["actual_value"] = self.extract_field(check_item)
|
||||||
|
except exception.ParseResponseError:
|
||||||
|
raise exception.ParseResponseError("failed to extract check item in response!")
|
||||||
|
|
||||||
match_expected = utils.match_expected(
|
match_expected = utils.match_expected(
|
||||||
validator_dict["value"],
|
validator_dict["actual_value"],
|
||||||
validator_dict["expected"],
|
expected,
|
||||||
validator_dict.get("comparator", "eq")
|
comparator
|
||||||
)
|
)
|
||||||
|
|
||||||
if not match_expected:
|
if not match_expected:
|
||||||
|
|||||||
@@ -170,7 +170,7 @@ class TestResponse(ApiServerUnittest):
|
|||||||
[
|
[
|
||||||
{
|
{
|
||||||
"check": "resp_status_code",
|
"check": "resp_status_code",
|
||||||
"comparator": "eq", "expected": 201, "value": 200
|
"comparator": "eq", "expected": 201, "actual_value": 200
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ class TestRunner(ApiServerUnittest):
|
|||||||
self.assertFalse(success)
|
self.assertFalse(success)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
diff_content_list[0],
|
diff_content_list[0],
|
||||||
{"check": "status_code", "comparator": "eq", "expected": 205, 'value': 200}
|
{"check": "status_code", "comparator": "eq", "expected": 205, 'actual_value': 200}
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_run_testset_hardcode(self):
|
def test_run_testset_hardcode(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user