validate all validators even if failed

This commit is contained in:
debugtalk
2018-07-24 18:04:10 +08:00
parent e2a46501fb
commit 6ae173d637
6 changed files with 51 additions and 39 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
__title__ = 'HttpRunner' __title__ = 'HttpRunner'
__description__ = 'One-stop solution for HTTP(S) testing.' __description__ = 'One-stop solution for HTTP(S) testing.'
__url__ = 'https://github.com/HttpRunner/HttpRunner' __url__ = 'https://github.com/HttpRunner/HttpRunner'
__version__ = '1.5.6' __version__ = '1.5.7'
__author__ = 'debugtalk' __author__ = 'debugtalk'
__author_email__ = 'mail@debugtalk.com' __author_email__ = 'mail@debugtalk.com'
__license__ = 'MIT' __license__ = 'MIT'
+42 -24
View File
@@ -5,7 +5,7 @@ import os
import re import re
import sys import sys
from httprunner import exception, testcase, utils from httprunner import exception, logger, testcase, utils
from httprunner.compat import OrderedDict from httprunner.compat import OrderedDict
@@ -237,32 +237,50 @@ class Context(object):
and comparator not in ["is", "eq", "equals", "=="]: and comparator not in ["is", "eq", "equals", "=="]:
raise exception.ParamsError("Null value can only be compared with comparator: eq/equals/==") raise exception.ParamsError("Null value can only be compared with comparator: eq/equals/==")
try: validate_msg = "validate: {} {} {}({})".format(
validator_dict["check_result"] = "passed" check_item,
validate_func(validator_dict["check_value"], validator_dict["expect"]) comparator,
except (AssertionError, TypeError): expect_value,
err_msg = "\n" + "\n".join([ type(expect_value).__name__
"\tcheck item name: %s;" % check_item, )
"\tcheck item value: %s (%s);" % (check_value, type(check_value).__name__),
"\tcomparator: %s;" % comparator,
"\texpected value: %s (%s)." % (expect_value, type(expect_value).__name__)
])
validator_dict["check_result"] = "failed"
raise exception.ValidationError(err_msg)
def eval_validators(self, validators, resp_obj): try:
""" evaluate validators with context variable mapping. validator_dict["check_result"] = "pass"
validate_func(check_value, expect_value)
validate_msg += "\t==> pass"
logger.log_debug(validate_msg)
except (AssertionError, TypeError):
validate_msg += "\t==> fail"
validate_msg += "\n{}({}) {} {}({})".format(
check_value,
type(check_value).__name__,
comparator,
expect_value,
type(expect_value).__name__
)
logger.log_error(validate_msg)
validator_dict["check_result"] = "fail"
raise exception.ValidationError(validate_msg)
def validate(self, validators, resp_obj):
""" make validations
""" """
return [ self.evaluated_validators = []
self.eval_check_item( validate_pass = True
for validator in validators:
# evaluate validators with context variable mapping.
evaluated_validator = self.eval_check_item(
testcase.parse_validator(validator), testcase.parse_validator(validator),
resp_obj resp_obj
) )
for validator in validators
]
def validate(self, validators): try:
""" make validations self.do_validation(evaluated_validator)
""" except exception.ValidationError:
for validator_dict in validators: validate_pass = False
self.do_validation(validator_dict)
self.evaluated_validators.append(evaluated_validator)
if not validate_pass:
raise exception.ValidationError
+1 -3
View File
@@ -11,7 +11,6 @@ class Runner(object):
def __init__(self, config_dict=None, http_client_session=None): def __init__(self, config_dict=None, http_client_session=None):
self.http_client_session = http_client_session self.http_client_session = http_client_session
self.evaluated_validators = []
self.context = Context() self.context = Context()
config_dict = config_dict or {} config_dict = config_dict or {}
@@ -183,8 +182,7 @@ class Runner(object):
# validate # validate
validators = testcase_dict.get("validate", []) or testcase_dict.get("validators", []) validators = testcase_dict.get("validate", []) or testcase_dict.get("validators", [])
try: try:
self.evaluated_validators = self.context.eval_validators(validators, resp_obj) self.context.validate(validators, resp_obj)
self.context.validate(self.evaluated_validators)
except (exception.ParamsError, exception.ResponseError, \ except (exception.ParamsError, exception.ResponseError, \
exception.ValidationError, exception.ParseResponseError): exception.ValidationError, exception.ParseResponseError):
# log request # log request
+1 -1
View File
@@ -28,7 +28,7 @@ class TestCase(unittest.TestCase):
finally: finally:
if hasattr(self.test_runner.http_client_session, "meta_data"): if hasattr(self.test_runner.http_client_session, "meta_data"):
self.meta_data = self.test_runner.http_client_session.meta_data self.meta_data = self.test_runner.http_client_session.meta_data
self.meta_data["validators"] = self.test_runner.evaluated_validators self.meta_data["validators"] = self.test_runner.context.evaluated_validators
self.test_runner.http_client_session.init_meta_data() self.test_runner.http_client_session.init_meta_data()
@@ -307,9 +307,9 @@
</tr> </tr>
{% for validator in record.meta_data.validators %} {% for validator in record.meta_data.validators %}
<tr> <tr>
{% if validator.check_result == "passed" %} {% if validator.check_result == "pass" %}
<td class="passed"> <td class="passed">
{% elif validator.check_result == "failed" %} {% elif validator.check_result == "fail" %}
<td class="failed"> <td class="failed">
{% elif validator.check_result == "unchecked" %} {% elif validator.check_result == "unchecked" %}
<td class="unchecked"> <td class="unchecked">
+4 -8
View File
@@ -271,8 +271,7 @@ class VariableBindsUnittest(ApiServerUnittest):
self.context.bind_variables(variables) self.context.bind_variables(variables)
with self.assertRaises(exception.ValidationError): with self.assertRaises(exception.ValidationError):
evaluated_validators = self.context.eval_validators(validators, resp_obj) self.context.validate(validators, resp_obj)
self.context.validate(evaluated_validators)
validators = [ validators = [
{"eq": ["$resp_status_code", 201]}, {"eq": ["$resp_status_code", 201]},
@@ -291,8 +290,7 @@ class VariableBindsUnittest(ApiServerUnittest):
} }
self.context.bind_functions(functions) self.context.bind_functions(functions)
evaluated_validators = self.context.eval_validators(validators, resp_obj) self.context.validate(validators, resp_obj)
self.context.validate(evaluated_validators)
def test_validate_exception(self): def test_validate_exception(self):
url = "http://127.0.0.1:5000/" url = "http://127.0.0.1:5000/"
@@ -308,8 +306,7 @@ class VariableBindsUnittest(ApiServerUnittest):
self.context.bind_variables(variables) self.context.bind_variables(variables)
with self.assertRaises(exception.ParamsError): with self.assertRaises(exception.ParamsError):
evaluated_validators = self.context.eval_validators(validators, resp_obj) self.context.validate(validators, resp_obj)
self.context.validate(evaluated_validators)
# expected value missed in variables mapping # expected value missed in variables mapping
variables = [ variables = [
@@ -318,5 +315,4 @@ class VariableBindsUnittest(ApiServerUnittest):
self.context.bind_variables(variables) self.context.bind_variables(variables)
with self.assertRaises(exception.ValidationError): with self.assertRaises(exception.ValidationError):
evaluated_validators = self.context.eval_validators(validators, resp_obj) self.context.validate(validators, resp_obj)
self.context.validate(evaluated_validators)