group exceptions to 2 types: failure and error

This commit is contained in:
debugtalk
2018-07-25 10:40:20 +08:00
parent 7a2920fac2
commit 7478331cb5
12 changed files with 60 additions and 54 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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", {}))

View File

@@ -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:

View File

@@ -270,7 +270,7 @@ class VariableBindsUnittest(ApiServerUnittest):
]
self.context.bind_variables(variables)
with self.assertRaises(exception.ValidationError):
with self.assertRaises(exception.ValidationFailure):
self.context.validate(validators, resp_obj)
validators = [
@@ -314,5 +314,5 @@ class VariableBindsUnittest(ApiServerUnittest):
]
self.context.bind_variables(variables)
with self.assertRaises(exception.ValidationError):
with self.assertRaises(exception.ValidationFailure):
self.context.validate(validators, resp_obj)

View File

@@ -2,7 +2,7 @@ import os
import shutil
from httprunner import HttpRunner
from httprunner.exception import FileNotFoundError
from httprunner.exception import FileNotFound
from tests.base import ApiServerUnittest
@@ -163,5 +163,5 @@ class TestHttpRunner(ApiServerUnittest):
self.assertEqual(os.environ["UserName"], "debugtalk")
def test_load_env_path_not_exist(self):
with self.assertRaises(FileNotFoundError):
with self.assertRaises(FileNotFound):
HttpRunner(dot_env_path="not_exist.env").run(self.testset_path)

View File

@@ -138,7 +138,7 @@ class TestResponse(ApiServerUnittest):
]
resp_obj = response.ResponseObject(resp)
with self.assertRaises(exception.ParseResponseError):
with self.assertRaises(exception.ParseResponseFailure):
resp_obj.extract_response(extract_binds_list)
extract_binds_list = [
@@ -146,7 +146,7 @@ class TestResponse(ApiServerUnittest):
]
resp_obj = response.ResponseObject(resp)
with self.assertRaises(exception.ParseResponseError):
with self.assertRaises(exception.ParseResponseFailure):
resp_obj.extract_response(extract_binds_list)
def test_extract_response_json_string(self):
@@ -225,5 +225,5 @@ class TestResponse(ApiServerUnittest):
{"resp_content_body": "content.data.def"}
]
resp_obj = response.ResponseObject(resp)
with self.assertRaises(exception.ParseResponseError):
with self.assertRaises(exception.ParseResponseFailure):
resp_obj.extract_response(extract_binds_list)

View File

@@ -66,7 +66,7 @@ class TestRunner(ApiServerUnittest):
]
}
with self.assertRaises(exception.ValidationError):
with self.assertRaises(exception.ValidationFailure):
self.test_runner.run_test(test)
def test_run_testset_with_hooks(self):

View File

@@ -3,9 +3,7 @@ import time
import unittest
from httprunner import testcase
from httprunner.exception import (ApiNotFound, FileFormatError,
FileNotFoundError, ParamsError,
SuiteNotFound)
from httprunner.exception import ApiNotFound, ParamsError, SuiteNotFound
from httprunner.testcase import TestcaseLoader

View File

@@ -62,7 +62,7 @@ class TestFileUtils(unittest.TestCase):
def test_load_testcases_bad_filepath(self):
testcase_file_path = os.path.join(os.getcwd(), 'tests/data/demo')
with self.assertRaises(exception.FileNotFoundError):
with self.assertRaises(exception.FileNotFound):
FileUtils.load_file(testcase_file_path)
def test_load_json_testcases(self):
@@ -163,11 +163,11 @@ class TestUtils(ApiServerUnittest):
self.assertEqual(result, 3)
query = "ids.str_key"
with self.assertRaises(exception.ParseResponseError):
with self.assertRaises(exception.ParseResponseFailure):
utils.query_json(json_content, query)
query = "ids.5"
with self.assertRaises(exception.ParseResponseError):
with self.assertRaises(exception.ParseResponseFailure):
utils.query_json(json_content, query)
query = "person.age"
@@ -175,7 +175,7 @@ class TestUtils(ApiServerUnittest):
self.assertEqual(result, 29)
query = "person.not_exist_key"
with self.assertRaises(exception.ParseResponseError):
with self.assertRaises(exception.ParseResponseFailure):
utils.query_json(json_content, query)
query = "person.cities.0"
@@ -189,12 +189,12 @@ class TestUtils(ApiServerUnittest):
def test_query_json_content_is_text(self):
json_content = ""
query = "key"
with self.assertRaises(exception.ResponseError):
with self.assertRaises(exception.ResponseFailure):
utils.query_json(json_content, query)
json_content = "<html><body>content</body></html>"
query = "key"
with self.assertRaises(exception.ParseResponseError):
with self.assertRaises(exception.ParseResponseFailure):
utils.query_json(json_content, query)
def test_get_uniform_comparator(self):