mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-21 04:22:30 +08:00
group exceptions to 2 types: failure and error
This commit is contained in:
@@ -8,6 +8,7 @@ This module handles import compatibility issues between Python 2 and
|
||||
Python 3.
|
||||
"""
|
||||
|
||||
import json
|
||||
import sys
|
||||
|
||||
# -------
|
||||
@@ -23,10 +24,6 @@ is_py2 = (_ver[0] == 2)
|
||||
#: Python 3.x?
|
||||
is_py3 = (_ver[0] == 3)
|
||||
|
||||
try:
|
||||
import simplejson as json
|
||||
except ImportError:
|
||||
import json
|
||||
|
||||
# ---------
|
||||
# Specifics
|
||||
@@ -42,6 +39,9 @@ if is_py2:
|
||||
numeric_types = (int, long, float)
|
||||
integer_types = (int, long)
|
||||
|
||||
FileNotFoundError = IOError
|
||||
JSONDecodeError = json.decoder.JSONDecodeError
|
||||
|
||||
elif is_py3:
|
||||
from collections import OrderedDict
|
||||
|
||||
@@ -51,3 +51,6 @@ elif is_py3:
|
||||
basestring = (str, bytes)
|
||||
numeric_types = (int, float)
|
||||
integer_types = (int,)
|
||||
|
||||
FileNotFoundError = FileNotFoundError
|
||||
JSONDecodeError = ValueError
|
||||
|
||||
@@ -204,10 +204,10 @@ class Context(object):
|
||||
try:
|
||||
# format 4/5
|
||||
check_value = resp_obj.extract_field(check_item)
|
||||
except exception.ParseResponseError:
|
||||
except exception.ParseResponseFailure:
|
||||
msg = "failed to extract check item from response!\n"
|
||||
msg += "response content: {}".format(resp_obj.content)
|
||||
raise exception.ParseResponseError(msg)
|
||||
raise exception.ParseResponseFailure(msg)
|
||||
|
||||
validator["check_value"] = check_value
|
||||
|
||||
@@ -260,7 +260,7 @@ class Context(object):
|
||||
)
|
||||
logger.log_error(validate_msg)
|
||||
validator_dict["check_result"] = "fail"
|
||||
raise exception.ValidationError(validate_msg)
|
||||
raise exception.ValidationFailure(validate_msg)
|
||||
|
||||
def validate(self, validators, resp_obj):
|
||||
""" make validations
|
||||
@@ -277,10 +277,10 @@ class Context(object):
|
||||
|
||||
try:
|
||||
self.do_validation(evaluated_validator)
|
||||
except exception.ValidationError:
|
||||
except exception.ValidationFailure:
|
||||
validate_pass = False
|
||||
|
||||
self.evaluated_validators.append(evaluated_validator)
|
||||
|
||||
if not validate_pass:
|
||||
raise exception.ValidationError
|
||||
raise exception.ValidationFailure
|
||||
|
||||
@@ -1,16 +1,27 @@
|
||||
# encoding: utf-8
|
||||
|
||||
import json
|
||||
from httprunner.compat import JSONDecodeError, FileNotFoundError
|
||||
|
||||
try:
|
||||
FileNotFoundError = FileNotFoundError
|
||||
except NameError:
|
||||
FileNotFoundError = IOError
|
||||
""" failure type exceptions
|
||||
these exceptions will mark test as failure
|
||||
"""
|
||||
|
||||
try:
|
||||
JSONDecodeError = json.decoder.JSONDecodeError
|
||||
except AttributeError:
|
||||
JSONDecodeError = ValueError
|
||||
class MyBaseFailure(BaseException):
|
||||
pass
|
||||
|
||||
class ValidationFailure(MyBaseFailure):
|
||||
pass
|
||||
|
||||
class ResponseFailure(MyBaseFailure):
|
||||
pass
|
||||
|
||||
class ParseResponseFailure(MyBaseFailure):
|
||||
pass
|
||||
|
||||
|
||||
""" error type exceptions
|
||||
these exceptions will mark test as error
|
||||
"""
|
||||
|
||||
class MyBaseError(BaseException):
|
||||
pass
|
||||
@@ -21,18 +32,12 @@ class FileFormatError(MyBaseError):
|
||||
class ParamsError(MyBaseError):
|
||||
pass
|
||||
|
||||
class ResponseError(MyBaseError):
|
||||
pass
|
||||
|
||||
class ParseResponseError(MyBaseError):
|
||||
pass
|
||||
|
||||
class ValidationError(MyBaseError):
|
||||
pass
|
||||
|
||||
class NotFoundError(MyBaseError):
|
||||
pass
|
||||
|
||||
class FileNotFound(FileNotFoundError, NotFoundError):
|
||||
pass
|
||||
|
||||
class FunctionNotFound(NotFoundError):
|
||||
pass
|
||||
|
||||
|
||||
@@ -111,7 +111,7 @@ class ResponseObject(object):
|
||||
top_query_content = top_query_content.__dict__
|
||||
else:
|
||||
top_query_content = json.loads(top_query_content)
|
||||
except json.decoder.JSONDecodeError:
|
||||
except exception.JSONDecodeError:
|
||||
err_msg = u"Failed to extract data with delimiter!\n"
|
||||
err_msg += u"response content: {}\n".format(self.content)
|
||||
err_msg += u"regex: {}\n".format(field)
|
||||
@@ -145,8 +145,8 @@ class ResponseObject(object):
|
||||
msg += "\t=> {}".format(value)
|
||||
logger.log_debug(msg)
|
||||
|
||||
# TODO: unify ParseResponseError type
|
||||
except (exception.ParseResponseError, TypeError):
|
||||
# TODO: unify ParseResponseFailure type
|
||||
except (exception.ParseResponseFailure, TypeError):
|
||||
logger.log_error("failed to extract field: {}".format(field))
|
||||
raise
|
||||
|
||||
|
||||
@@ -183,8 +183,8 @@ class Runner(object):
|
||||
validators = testcase_dict.get("validate", []) or testcase_dict.get("validators", [])
|
||||
try:
|
||||
self.context.validate(validators, resp_obj)
|
||||
except (exception.ParamsError, exception.ResponseError, \
|
||||
exception.ValidationError, exception.ParseResponseError):
|
||||
except (exception.ParamsError, exception.ResponseFailure, \
|
||||
exception.ValidationFailure, exception.ParseResponseFailure):
|
||||
# log request
|
||||
err_req_msg = "request: \n"
|
||||
err_req_msg += "headers: {}\n".format(parsed_request.pop("headers", {}))
|
||||
|
||||
@@ -117,7 +117,7 @@ class FileUtils(object):
|
||||
@staticmethod
|
||||
def load_file(file_path):
|
||||
if not os.path.isfile(file_path):
|
||||
raise exception.FileNotFoundError("{} does not exist.".format(file_path))
|
||||
raise exception.FileNotFound("{} does not exist.".format(file_path))
|
||||
|
||||
file_suffix = os.path.splitext(file_path)[1].lower()
|
||||
if file_suffix == '.json':
|
||||
@@ -190,7 +190,7 @@ def query_json(json_content, query, delimiter='.'):
|
||||
@return queried result
|
||||
"""
|
||||
if json_content == "":
|
||||
raise exception.ResponseError("response content is empty!")
|
||||
raise exception.ResponseFailure("response content is empty!")
|
||||
|
||||
try:
|
||||
for key in query.split(delimiter):
|
||||
@@ -199,10 +199,10 @@ def query_json(json_content, query, delimiter='.'):
|
||||
elif isinstance(json_content, (dict, CaseInsensitiveDict)):
|
||||
json_content = json_content[key]
|
||||
else:
|
||||
raise exception.ParseResponseError(
|
||||
raise exception.ParseResponseFailure(
|
||||
"response content is in text format! failed to query key {}!".format(key))
|
||||
except (KeyError, ValueError, IndexError):
|
||||
raise exception.ParseResponseError("failed to query json when extracting response!")
|
||||
raise exception.ParseResponseFailure("failed to query json when extracting response!")
|
||||
|
||||
return json_content
|
||||
|
||||
@@ -507,7 +507,7 @@ def load_dot_env_file(path):
|
||||
return
|
||||
else:
|
||||
if not os.path.isfile(path):
|
||||
raise exception.FileNotFoundError("env file not exist: {}".format(path))
|
||||
raise exception.FileNotFound("env file not exist: {}".format(path))
|
||||
|
||||
logger.log_info("Loading environment variables from {}".format(path))
|
||||
with io.open(path, 'r', encoding='utf-8') as fp:
|
||||
|
||||
Reference in New Issue
Block a user