optimize response validation: if validation failed, then raise ValidationError directly

This commit is contained in:
httprunner
2017-08-02 15:10:38 +08:00
parent a18c7bc62f
commit 4d471d7cda
7 changed files with 41 additions and 55 deletions

View File

@@ -8,7 +8,6 @@ class ResponseObject(object):
@param (requests.Response instance) resp_obj
"""
self.resp_obj = resp_obj
self.success = True
def parsed_body(self):
try:
@@ -95,8 +94,6 @@ class ResponseObject(object):
}
]
"""
diff_content_list = []
for validator_dict in validators:
check_item = validator_dict.get("check")
@@ -117,14 +114,11 @@ class ResponseObject(object):
except exception.ParseResponseError:
raise exception.ParseResponseError("failed to extract check item in response!")
match_expected = utils.match_expected(
utils.match_expected(
validator_dict["actual_value"],
expected,
comparator
comparator,
check_item
)
if not match_expected:
diff_content_list.append(validator_dict)
self.success = False if diff_content_list else True
return diff_content_list
return True

View File

@@ -101,10 +101,9 @@ class Runner(object):
extracted_variables_mapping_list = resp_obj.extract_response(extract_binds)
self.context.bind_variables(extracted_variables_mapping_list, level="testset")
diff_content_list = resp_obj.validate(
validators, self.context.get_testcase_variables_mapping())
resp_obj.validate(validators, self.context.get_testcase_variables_mapping())
return resp_obj.success, diff_content_list
return True
def run_testset(self, testset):
""" run single testset, including one or several testcases.

View File

@@ -14,8 +14,7 @@ class ApiTestCase(unittest.TestCase):
def runTest(self):
""" run testcase and check result.
"""
result = self.test_runner.run_test(self.testcase)
self.assertEqual(result, (True, []))
self.assertTrue(self.test_runner.run_test(self.testcase))
def create_suite(testset):
""" create test suite with a testset, it may include one or several testcases.

View File

@@ -144,11 +144,12 @@ def query_json(json_content, query, delimiter='.'):
return json_content
def match_expected(value, expected, comparator="eq"):
def match_expected(value, expected, comparator="eq", check_item=""):
""" check if value matches expected value.
@param value: value that get from response.
@param value: actual value that get from response.
@param expected: expected result described in testcase
@param comparator: compare method
@param check_item: check item name
"""
try:
if comparator in ["eq", "equals", "=="]:
@@ -192,7 +193,13 @@ def match_expected(value, expected, comparator="eq"):
return True
except AssertionError:
return False
err_msg = "\n".join([
"check item name: %s;" % check_item,
"check item value: %s (%s);" % (value, type(value).__name__),
"comparator: %s;" % comparator,
"expected value: %s (%s)." % (expected, type(expected).__name__)
])
raise exception.ValidationError(err_msg)
def deep_update_dict(origin_dict, override_dict):
""" update origin dict with override dict recursively