mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-08 22:13:13 +08:00
support new validator format, e.g. {'eq': ['status_code', 200]}
This commit is contained in:
@@ -1 +1 @@
|
|||||||
__version__ = '0.8.2'
|
__version__ = '0.8.3'
|
||||||
@@ -124,50 +124,69 @@ class ResponseObject(object):
|
|||||||
|
|
||||||
return extracted_variables_mapping
|
return extracted_variables_mapping
|
||||||
|
|
||||||
def validate(self, validators, variables_mapping):
|
def parse_validator(self, validator, variables_mapping):
|
||||||
""" Bind named validators to value within the context.
|
""" parse validator, validator maybe in two format
|
||||||
@param (list) validators
|
@param (dict) validator
|
||||||
[
|
format1: this is kept for compatiblity with the previous versions.
|
||||||
{"check": "status_code", "comparator": "eq", "expect": 201},
|
{"check": "status_code", "comparator": "eq", "expect": 201}
|
||||||
{"check": "resp_body_success", "comparator": "eq", "expect": True}
|
{"check": "resp_body_success", "comparator": "eq", "expect": True}
|
||||||
]
|
format2: recommended new version
|
||||||
|
{'eq': ['status_code', 201]}
|
||||||
|
{'eq': ['resp_body_success', True]}
|
||||||
@param (dict) variables_mapping
|
@param (dict) variables_mapping
|
||||||
{
|
{
|
||||||
"resp_body_success": True
|
"resp_body_success": True
|
||||||
}
|
}
|
||||||
@return (list) content differences
|
@return validator info
|
||||||
[
|
check_item, check_value, expect_value, comparator
|
||||||
{
|
|
||||||
"check": "status_code",
|
|
||||||
"comparator": "eq", "expect": 201, "value": 200
|
|
||||||
}
|
|
||||||
]
|
|
||||||
"""
|
"""
|
||||||
for validator_dict in validators:
|
if not isinstance(validator, dict):
|
||||||
|
raise exception.ParamsError("invalid validator: {}".format(validator))
|
||||||
|
|
||||||
check_item = validator_dict.get("check")
|
if "check" in validator and len(validator) > 1:
|
||||||
if not check_item:
|
# format1
|
||||||
raise exception.ParamsError("check item invalid: {}".format(check_item))
|
check_item = validator.get("check")
|
||||||
|
|
||||||
if "expect" in validator_dict:
|
if "expect" in validator:
|
||||||
expect_value = validator_dict.get("expect")
|
expect_value = validator.get("expect")
|
||||||
elif "expected" in validator_dict:
|
elif "expected" in validator:
|
||||||
expect_value = validator_dict.get("expected")
|
expect_value = validator.get("expected")
|
||||||
else:
|
else:
|
||||||
raise exception.ParamsError("expected value missed in testcase validator!")
|
raise exception.ParamsError("invalid validator: {}".format(validator))
|
||||||
|
|
||||||
comparator = validator_dict.get("comparator", "eq")
|
comparator = validator.get("comparator", "eq")
|
||||||
|
|
||||||
if check_item in variables_mapping:
|
elif len(validator) == 1:
|
||||||
validator_dict["actual_value"] = variables_mapping[check_item]
|
# format2
|
||||||
else:
|
comparator = list(validator.keys())[0]
|
||||||
try:
|
compare_values = validator[comparator]
|
||||||
validator_dict["actual_value"] = self.extract_field(check_item)
|
|
||||||
except exception.ParseResponseError:
|
if not isinstance(compare_values, list) or len(compare_values) != 2:
|
||||||
raise exception.ParseResponseError("failed to extract check item in response!")
|
raise exception.ParamsError("invalid validator: {}".format(validator))
|
||||||
|
|
||||||
|
check_item, expect_value = compare_values
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise exception.ParamsError("invalid validator: {}".format(validator))
|
||||||
|
|
||||||
|
if check_item in variables_mapping:
|
||||||
|
check_value = variables_mapping[check_item]
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
check_value = self.extract_field(check_item)
|
||||||
|
except exception.ParseResponseError:
|
||||||
|
raise exception.ParseResponseError("failed to extract check item in response!")
|
||||||
|
|
||||||
|
return check_item, check_value, expect_value, comparator
|
||||||
|
|
||||||
|
def validate(self, validators, variables_mapping):
|
||||||
|
""" check validators with the context variable mapping.
|
||||||
|
"""
|
||||||
|
for validator in validators:
|
||||||
|
check_item, check_value, expect_value, comparator = self.parse_validator(validator, variables_mapping)
|
||||||
|
|
||||||
utils.match_expected(
|
utils.match_expected(
|
||||||
validator_dict["actual_value"],
|
check_value,
|
||||||
expect_value,
|
expect_value,
|
||||||
comparator,
|
comparator,
|
||||||
check_item
|
check_item
|
||||||
|
|||||||
@@ -22,6 +22,8 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"validate": [
|
"validate": [
|
||||||
|
{"eq": ["status_code", 200]},
|
||||||
|
{"len_eq": ["content.token", 16]},
|
||||||
{"check": "status_code", "comparator": "eq", "expect": 200},
|
{"check": "status_code", "comparator": "eq", "expect": 200},
|
||||||
{"check": "content.token", "comparator": "len_eq", "expect": 16}
|
{"check": "content.token", "comparator": "len_eq", "expect": 16}
|
||||||
]
|
]
|
||||||
@@ -44,6 +46,8 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"validate": [
|
"validate": [
|
||||||
|
{"eq": ["status_code", 201]},
|
||||||
|
{"eq": ["content.success", true]},
|
||||||
{"check": "status_code", "comparator": "eq", "expect": 201},
|
{"check": "status_code", "comparator": "eq", "expect": 201},
|
||||||
{"check": "content.success", "comparator": "eq", "expect": true}
|
{"check": "content.success", "comparator": "eq", "expect": true}
|
||||||
]
|
]
|
||||||
@@ -66,6 +70,8 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"validate": [
|
"validate": [
|
||||||
|
{"eq": ["status_code", 500]},
|
||||||
|
{"eq": ["content.success", false]},
|
||||||
{"check": "status_code", "comparator": "eq", "expect": 500},
|
{"check": "status_code", "comparator": "eq", "expect": 500},
|
||||||
{"check": "content.success", "comparator": "eq", "expect": false}
|
{"check": "content.success", "comparator": "eq", "expect": false}
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -14,6 +14,8 @@
|
|||||||
extract:
|
extract:
|
||||||
- token: content.token
|
- token: content.token
|
||||||
validate:
|
validate:
|
||||||
|
- eq: ["status_code", 200]
|
||||||
|
- len_eq: ["content.token", 16]
|
||||||
- {"check": "status_code", "comparator": "eq", "expect": 200}
|
- {"check": "status_code", "comparator": "eq", "expect": 200}
|
||||||
- {"check": "content.token", "comparator": "len_eq", "expect": 16}
|
- {"check": "content.token", "comparator": "len_eq", "expect": 16}
|
||||||
|
|
||||||
@@ -30,6 +32,8 @@
|
|||||||
name: "user1"
|
name: "user1"
|
||||||
password: "123456"
|
password: "123456"
|
||||||
validate:
|
validate:
|
||||||
|
- eq: ["status_code", 201]
|
||||||
|
- eq: ["content.success", True]
|
||||||
- {"check": "status_code", "comparator": "eq", "expect": 201}
|
- {"check": "status_code", "comparator": "eq", "expect": 201}
|
||||||
- {"check": "content.success", "comparator": "eq", "expect": true}
|
- {"check": "content.success", "comparator": "eq", "expect": true}
|
||||||
|
|
||||||
@@ -46,5 +50,7 @@
|
|||||||
name: "user1"
|
name: "user1"
|
||||||
password: "123456"
|
password: "123456"
|
||||||
validate:
|
validate:
|
||||||
|
- "eq": ["status_code", 500]
|
||||||
|
- "eq": ["content.success", false]
|
||||||
- {"check": "status_code", "comparator": "eq", "expect": 500}
|
- {"check": "status_code", "comparator": "eq", "expect": 500}
|
||||||
- {"check": "content.success", "comparator": "eq", "expect": false}
|
- {"check": "content.success", "comparator": "eq", "expect": false}
|
||||||
Reference in New Issue
Block a user