mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-30 04:49:33 +08:00
#29: refactor validator:
1, relocate validate functions; 2, add unittest for custom defined validators.
This commit is contained in:
@@ -230,70 +230,3 @@ class TestResponse(ApiServerUnittest):
|
||||
resp_obj = response.ResponseObject(resp)
|
||||
with self.assertRaises(exception.ParamsError):
|
||||
resp_obj.extract_response(extract_binds_list)
|
||||
|
||||
def test_do_validation(self):
|
||||
url = "http://127.0.0.1:5000/"
|
||||
resp = requests.get(url)
|
||||
resp_obj = response.ResponseObject(resp)
|
||||
|
||||
resp_obj.do_validation(
|
||||
{"check_item": "check_item", "check_value": 1, "expect_value": 1, "comparator": "eq"},
|
||||
self.functions_mapping
|
||||
)
|
||||
resp_obj.do_validation(
|
||||
{"check_item": "check_item", "check_value": "abc", "expect_value": "abc", "comparator": "=="},
|
||||
self.functions_mapping
|
||||
)
|
||||
|
||||
def test_validate(self):
|
||||
url = "http://127.0.0.1:5000/"
|
||||
resp = requests.get(url)
|
||||
resp_obj = response.ResponseObject(resp)
|
||||
|
||||
validators = [
|
||||
{"check": "resp_status_code", "comparator": "eq", "expect": 201},
|
||||
{"check": "resp_body_success", "comparator": "eq", "expect": True}
|
||||
]
|
||||
variables_mapping = {
|
||||
"resp_status_code": 200,
|
||||
"resp_body_success": True
|
||||
}
|
||||
|
||||
with self.assertRaises(exception.ValidationError):
|
||||
resp_obj.validate(validators, variables_mapping, self.functions_mapping)
|
||||
|
||||
validators = [
|
||||
{"check": "resp_status_code", "comparator": "eq", "expect": 201},
|
||||
{"check": "resp_body_success", "comparator": "eq", "expect": True}
|
||||
]
|
||||
variables_mapping = {
|
||||
"resp_status_code": 201,
|
||||
"resp_body_success": True
|
||||
}
|
||||
|
||||
self.assertTrue(resp_obj.validate(validators, variables_mapping, self.functions_mapping))
|
||||
|
||||
def test_validate_exception(self):
|
||||
url = "http://127.0.0.1:5000/"
|
||||
resp = requests.get(url)
|
||||
resp_obj = response.ResponseObject(resp)
|
||||
|
||||
# expected value missed in validators
|
||||
validators = [
|
||||
{"check": "status_code", "comparator": "eq", "expect": 201},
|
||||
{"check": "body_success", "comparator": "eq"}
|
||||
]
|
||||
variables_mapping = {}
|
||||
with self.assertRaises(exception.ValidationError):
|
||||
resp_obj.validate(validators, variables_mapping, self.functions_mapping)
|
||||
|
||||
# expected value missed in variables mapping
|
||||
validators = [
|
||||
{"check": "resp_status_code", "comparator": "eq", "expect": 201},
|
||||
{"check": "body_success", "comparator": "eq"}
|
||||
]
|
||||
variables_mapping = {
|
||||
"resp_status_code": 200
|
||||
}
|
||||
with self.assertRaises(exception.ValidationError):
|
||||
resp_obj.validate(validators, variables_mapping, self.functions_mapping)
|
||||
|
||||
Reference in New Issue
Block a user