mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-12 16:01:27 +08:00
change validators from dict to list, as there may be several tests on one filed
This commit is contained in:
@@ -16,7 +16,7 @@ class ApiTestCase(unittest.TestCase):
|
||||
""" run testcase and check result.
|
||||
"""
|
||||
result = self.test_runner.run_test(self.testcase)
|
||||
self.assertEqual(result, (True, {}))
|
||||
self.assertEqual(result, (True, []))
|
||||
|
||||
def create_suite(testset):
|
||||
""" create test suite with a testset, it may include one or several testcases.
|
||||
|
||||
@@ -74,35 +74,36 @@ class ResponseObject(object):
|
||||
|
||||
def validate(self, validators, variables_mapping):
|
||||
""" Bind named validators to value within the context.
|
||||
@param (dict) validators
|
||||
{
|
||||
"resp_status_code": {"comparator": "eq", "expected": 201},
|
||||
"resp_body_success": {"comparator": "eq", "expected": True}
|
||||
}
|
||||
@param (list) validators
|
||||
[
|
||||
{"check": "status_code", "comparator": "eq", "expected": 201},
|
||||
{"check": "resp_body_success", "comparator": "eq", "expected": True}
|
||||
]
|
||||
@param (dict) variables_mapping
|
||||
{
|
||||
"resp_status_code": 200,
|
||||
"resp_body_success": True
|
||||
}
|
||||
@return (dict) content differences
|
||||
{
|
||||
"resp_status_code": {
|
||||
@return (list) content differences
|
||||
[
|
||||
{
|
||||
"check": "status_code",
|
||||
"comparator": "eq", "expected": 201, "value": 200
|
||||
}
|
||||
}
|
||||
]
|
||||
"""
|
||||
diff_content_dict = {}
|
||||
diff_content_list = []
|
||||
|
||||
for validator_key, validator_dict in validators.items():
|
||||
for validator_dict in validators:
|
||||
|
||||
if "expected" not in validator_dict or "check" not in validator_dict:
|
||||
raise exception.ParamsError("expected not specified in validator")
|
||||
|
||||
validator_key = validator_dict["check"]
|
||||
try:
|
||||
validator_dict["value"] = variables_mapping[validator_key]
|
||||
except KeyError:
|
||||
validator_dict["value"] = self.extract_field(validator_key)
|
||||
|
||||
if "expected" not in validator_dict:
|
||||
raise exception.ParamsError("expected not specified in validator")
|
||||
|
||||
match_expected = utils.match_expected(
|
||||
validator_dict["value"],
|
||||
validator_dict["expected"],
|
||||
@@ -110,7 +111,7 @@ class ResponseObject(object):
|
||||
)
|
||||
|
||||
if not match_expected:
|
||||
diff_content_dict[validator_key] = validator_dict
|
||||
diff_content_list.append(validator_dict)
|
||||
|
||||
self.success = False if diff_content_dict else True
|
||||
return diff_content_dict
|
||||
self.success = False if diff_content_list else True
|
||||
return diff_content_list
|
||||
|
||||
@@ -59,10 +59,10 @@ class TestRunner(object):
|
||||
"body": '{"name": "user", "password": "123456"}'
|
||||
},
|
||||
"extract_binds": {},
|
||||
"validators": {}
|
||||
"validators": []
|
||||
}
|
||||
@return (tuple) test result of single testcase
|
||||
(success, diff_content)
|
||||
(success, diff_content_list)
|
||||
"""
|
||||
self.update_context(testcase)
|
||||
parsed_request = parse_template(testcase["request"], self.context.variables)
|
||||
@@ -80,10 +80,10 @@ class TestRunner(object):
|
||||
extracted_variables_mapping = resp_obj.extract_response(extract_binds)
|
||||
self.context.update_variables(extracted_variables_mapping)
|
||||
|
||||
validators = testcase.get("validators", {})
|
||||
diff_content_dict = resp_obj.validate(validators, self.context.variables)
|
||||
validators = testcase.get("validators", [])
|
||||
diff_content_list = resp_obj.validate(validators, self.context.variables)
|
||||
|
||||
return resp_obj.success, diff_content_dict
|
||||
return resp_obj.success, diff_content_list
|
||||
|
||||
def run_testset(self, testset):
|
||||
""" run single testset, including one or several testcases.
|
||||
|
||||
Reference in New Issue
Block a user