mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-05 15:37:35 +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.
|
Python 3.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import json
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
# -------
|
# -------
|
||||||
@@ -23,10 +24,6 @@ is_py2 = (_ver[0] == 2)
|
|||||||
#: Python 3.x?
|
#: Python 3.x?
|
||||||
is_py3 = (_ver[0] == 3)
|
is_py3 = (_ver[0] == 3)
|
||||||
|
|
||||||
try:
|
|
||||||
import simplejson as json
|
|
||||||
except ImportError:
|
|
||||||
import json
|
|
||||||
|
|
||||||
# ---------
|
# ---------
|
||||||
# Specifics
|
# Specifics
|
||||||
@@ -42,6 +39,9 @@ if is_py2:
|
|||||||
numeric_types = (int, long, float)
|
numeric_types = (int, long, float)
|
||||||
integer_types = (int, long)
|
integer_types = (int, long)
|
||||||
|
|
||||||
|
FileNotFoundError = IOError
|
||||||
|
JSONDecodeError = json.decoder.JSONDecodeError
|
||||||
|
|
||||||
elif is_py3:
|
elif is_py3:
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
|
||||||
@@ -51,3 +51,6 @@ elif is_py3:
|
|||||||
basestring = (str, bytes)
|
basestring = (str, bytes)
|
||||||
numeric_types = (int, float)
|
numeric_types = (int, float)
|
||||||
integer_types = (int,)
|
integer_types = (int,)
|
||||||
|
|
||||||
|
FileNotFoundError = FileNotFoundError
|
||||||
|
JSONDecodeError = ValueError
|
||||||
|
|||||||
@@ -204,10 +204,10 @@ class Context(object):
|
|||||||
try:
|
try:
|
||||||
# format 4/5
|
# format 4/5
|
||||||
check_value = resp_obj.extract_field(check_item)
|
check_value = resp_obj.extract_field(check_item)
|
||||||
except exception.ParseResponseError:
|
except exception.ParseResponseFailure:
|
||||||
msg = "failed to extract check item from response!\n"
|
msg = "failed to extract check item from response!\n"
|
||||||
msg += "response content: {}".format(resp_obj.content)
|
msg += "response content: {}".format(resp_obj.content)
|
||||||
raise exception.ParseResponseError(msg)
|
raise exception.ParseResponseFailure(msg)
|
||||||
|
|
||||||
validator["check_value"] = check_value
|
validator["check_value"] = check_value
|
||||||
|
|
||||||
@@ -260,7 +260,7 @@ class Context(object):
|
|||||||
)
|
)
|
||||||
logger.log_error(validate_msg)
|
logger.log_error(validate_msg)
|
||||||
validator_dict["check_result"] = "fail"
|
validator_dict["check_result"] = "fail"
|
||||||
raise exception.ValidationError(validate_msg)
|
raise exception.ValidationFailure(validate_msg)
|
||||||
|
|
||||||
def validate(self, validators, resp_obj):
|
def validate(self, validators, resp_obj):
|
||||||
""" make validations
|
""" make validations
|
||||||
@@ -277,10 +277,10 @@ class Context(object):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
self.do_validation(evaluated_validator)
|
self.do_validation(evaluated_validator)
|
||||||
except exception.ValidationError:
|
except exception.ValidationFailure:
|
||||||
validate_pass = False
|
validate_pass = False
|
||||||
|
|
||||||
self.evaluated_validators.append(evaluated_validator)
|
self.evaluated_validators.append(evaluated_validator)
|
||||||
|
|
||||||
if not validate_pass:
|
if not validate_pass:
|
||||||
raise exception.ValidationError
|
raise exception.ValidationFailure
|
||||||
|
|||||||
+23
-18
@@ -1,16 +1,27 @@
|
|||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
|
||||||
import json
|
from httprunner.compat import JSONDecodeError, FileNotFoundError
|
||||||
|
|
||||||
try:
|
""" failure type exceptions
|
||||||
FileNotFoundError = FileNotFoundError
|
these exceptions will mark test as failure
|
||||||
except NameError:
|
"""
|
||||||
FileNotFoundError = IOError
|
|
||||||
|
|
||||||
try:
|
class MyBaseFailure(BaseException):
|
||||||
JSONDecodeError = json.decoder.JSONDecodeError
|
pass
|
||||||
except AttributeError:
|
|
||||||
JSONDecodeError = ValueError
|
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):
|
class MyBaseError(BaseException):
|
||||||
pass
|
pass
|
||||||
@@ -21,18 +32,12 @@ class FileFormatError(MyBaseError):
|
|||||||
class ParamsError(MyBaseError):
|
class ParamsError(MyBaseError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class ResponseError(MyBaseError):
|
|
||||||
pass
|
|
||||||
|
|
||||||
class ParseResponseError(MyBaseError):
|
|
||||||
pass
|
|
||||||
|
|
||||||
class ValidationError(MyBaseError):
|
|
||||||
pass
|
|
||||||
|
|
||||||
class NotFoundError(MyBaseError):
|
class NotFoundError(MyBaseError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class FileNotFound(FileNotFoundError, NotFoundError):
|
||||||
|
pass
|
||||||
|
|
||||||
class FunctionNotFound(NotFoundError):
|
class FunctionNotFound(NotFoundError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
@@ -111,7 +111,7 @@ class ResponseObject(object):
|
|||||||
top_query_content = top_query_content.__dict__
|
top_query_content = top_query_content.__dict__
|
||||||
else:
|
else:
|
||||||
top_query_content = json.loads(top_query_content)
|
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"Failed to extract data with delimiter!\n"
|
||||||
err_msg += u"response content: {}\n".format(self.content)
|
err_msg += u"response content: {}\n".format(self.content)
|
||||||
err_msg += u"regex: {}\n".format(field)
|
err_msg += u"regex: {}\n".format(field)
|
||||||
@@ -145,8 +145,8 @@ class ResponseObject(object):
|
|||||||
msg += "\t=> {}".format(value)
|
msg += "\t=> {}".format(value)
|
||||||
logger.log_debug(msg)
|
logger.log_debug(msg)
|
||||||
|
|
||||||
# TODO: unify ParseResponseError type
|
# TODO: unify ParseResponseFailure type
|
||||||
except (exception.ParseResponseError, TypeError):
|
except (exception.ParseResponseFailure, TypeError):
|
||||||
logger.log_error("failed to extract field: {}".format(field))
|
logger.log_error("failed to extract field: {}".format(field))
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|||||||
@@ -183,8 +183,8 @@ class Runner(object):
|
|||||||
validators = testcase_dict.get("validate", []) or testcase_dict.get("validators", [])
|
validators = testcase_dict.get("validate", []) or testcase_dict.get("validators", [])
|
||||||
try:
|
try:
|
||||||
self.context.validate(validators, resp_obj)
|
self.context.validate(validators, resp_obj)
|
||||||
except (exception.ParamsError, exception.ResponseError, \
|
except (exception.ParamsError, exception.ResponseFailure, \
|
||||||
exception.ValidationError, exception.ParseResponseError):
|
exception.ValidationFailure, exception.ParseResponseFailure):
|
||||||
# log request
|
# log request
|
||||||
err_req_msg = "request: \n"
|
err_req_msg = "request: \n"
|
||||||
err_req_msg += "headers: {}\n".format(parsed_request.pop("headers", {}))
|
err_req_msg += "headers: {}\n".format(parsed_request.pop("headers", {}))
|
||||||
|
|||||||
+5
-5
@@ -117,7 +117,7 @@ class FileUtils(object):
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def load_file(file_path):
|
def load_file(file_path):
|
||||||
if not os.path.isfile(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()
|
file_suffix = os.path.splitext(file_path)[1].lower()
|
||||||
if file_suffix == '.json':
|
if file_suffix == '.json':
|
||||||
@@ -190,7 +190,7 @@ def query_json(json_content, query, delimiter='.'):
|
|||||||
@return queried result
|
@return queried result
|
||||||
"""
|
"""
|
||||||
if json_content == "":
|
if json_content == "":
|
||||||
raise exception.ResponseError("response content is empty!")
|
raise exception.ResponseFailure("response content is empty!")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
for key in query.split(delimiter):
|
for key in query.split(delimiter):
|
||||||
@@ -199,10 +199,10 @@ def query_json(json_content, query, delimiter='.'):
|
|||||||
elif isinstance(json_content, (dict, CaseInsensitiveDict)):
|
elif isinstance(json_content, (dict, CaseInsensitiveDict)):
|
||||||
json_content = json_content[key]
|
json_content = json_content[key]
|
||||||
else:
|
else:
|
||||||
raise exception.ParseResponseError(
|
raise exception.ParseResponseFailure(
|
||||||
"response content is in text format! failed to query key {}!".format(key))
|
"response content is in text format! failed to query key {}!".format(key))
|
||||||
except (KeyError, ValueError, IndexError):
|
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
|
return json_content
|
||||||
|
|
||||||
@@ -507,7 +507,7 @@ def load_dot_env_file(path):
|
|||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
if not os.path.isfile(path):
|
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))
|
logger.log_info("Loading environment variables from {}".format(path))
|
||||||
with io.open(path, 'r', encoding='utf-8') as fp:
|
with io.open(path, 'r', encoding='utf-8') as fp:
|
||||||
|
|||||||
@@ -270,7 +270,7 @@ class VariableBindsUnittest(ApiServerUnittest):
|
|||||||
]
|
]
|
||||||
self.context.bind_variables(variables)
|
self.context.bind_variables(variables)
|
||||||
|
|
||||||
with self.assertRaises(exception.ValidationError):
|
with self.assertRaises(exception.ValidationFailure):
|
||||||
self.context.validate(validators, resp_obj)
|
self.context.validate(validators, resp_obj)
|
||||||
|
|
||||||
validators = [
|
validators = [
|
||||||
@@ -314,5 +314,5 @@ class VariableBindsUnittest(ApiServerUnittest):
|
|||||||
]
|
]
|
||||||
self.context.bind_variables(variables)
|
self.context.bind_variables(variables)
|
||||||
|
|
||||||
with self.assertRaises(exception.ValidationError):
|
with self.assertRaises(exception.ValidationFailure):
|
||||||
self.context.validate(validators, resp_obj)
|
self.context.validate(validators, resp_obj)
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import os
|
|||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
from httprunner import HttpRunner
|
from httprunner import HttpRunner
|
||||||
from httprunner.exception import FileNotFoundError
|
from httprunner.exception import FileNotFound
|
||||||
from tests.base import ApiServerUnittest
|
from tests.base import ApiServerUnittest
|
||||||
|
|
||||||
|
|
||||||
@@ -163,5 +163,5 @@ class TestHttpRunner(ApiServerUnittest):
|
|||||||
self.assertEqual(os.environ["UserName"], "debugtalk")
|
self.assertEqual(os.environ["UserName"], "debugtalk")
|
||||||
|
|
||||||
def test_load_env_path_not_exist(self):
|
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)
|
HttpRunner(dot_env_path="not_exist.env").run(self.testset_path)
|
||||||
|
|||||||
@@ -138,7 +138,7 @@ class TestResponse(ApiServerUnittest):
|
|||||||
]
|
]
|
||||||
resp_obj = response.ResponseObject(resp)
|
resp_obj = response.ResponseObject(resp)
|
||||||
|
|
||||||
with self.assertRaises(exception.ParseResponseError):
|
with self.assertRaises(exception.ParseResponseFailure):
|
||||||
resp_obj.extract_response(extract_binds_list)
|
resp_obj.extract_response(extract_binds_list)
|
||||||
|
|
||||||
extract_binds_list = [
|
extract_binds_list = [
|
||||||
@@ -146,7 +146,7 @@ class TestResponse(ApiServerUnittest):
|
|||||||
]
|
]
|
||||||
resp_obj = response.ResponseObject(resp)
|
resp_obj = response.ResponseObject(resp)
|
||||||
|
|
||||||
with self.assertRaises(exception.ParseResponseError):
|
with self.assertRaises(exception.ParseResponseFailure):
|
||||||
resp_obj.extract_response(extract_binds_list)
|
resp_obj.extract_response(extract_binds_list)
|
||||||
|
|
||||||
def test_extract_response_json_string(self):
|
def test_extract_response_json_string(self):
|
||||||
@@ -225,5 +225,5 @@ class TestResponse(ApiServerUnittest):
|
|||||||
{"resp_content_body": "content.data.def"}
|
{"resp_content_body": "content.data.def"}
|
||||||
]
|
]
|
||||||
resp_obj = response.ResponseObject(resp)
|
resp_obj = response.ResponseObject(resp)
|
||||||
with self.assertRaises(exception.ParseResponseError):
|
with self.assertRaises(exception.ParseResponseFailure):
|
||||||
resp_obj.extract_response(extract_binds_list)
|
resp_obj.extract_response(extract_binds_list)
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ class TestRunner(ApiServerUnittest):
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
with self.assertRaises(exception.ValidationError):
|
with self.assertRaises(exception.ValidationFailure):
|
||||||
self.test_runner.run_test(test)
|
self.test_runner.run_test(test)
|
||||||
|
|
||||||
def test_run_testset_with_hooks(self):
|
def test_run_testset_with_hooks(self):
|
||||||
|
|||||||
@@ -3,9 +3,7 @@ import time
|
|||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from httprunner import testcase
|
from httprunner import testcase
|
||||||
from httprunner.exception import (ApiNotFound, FileFormatError,
|
from httprunner.exception import ApiNotFound, ParamsError, SuiteNotFound
|
||||||
FileNotFoundError, ParamsError,
|
|
||||||
SuiteNotFound)
|
|
||||||
from httprunner.testcase import TestcaseLoader
|
from httprunner.testcase import TestcaseLoader
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+6
-6
@@ -62,7 +62,7 @@ class TestFileUtils(unittest.TestCase):
|
|||||||
|
|
||||||
def test_load_testcases_bad_filepath(self):
|
def test_load_testcases_bad_filepath(self):
|
||||||
testcase_file_path = os.path.join(os.getcwd(), 'tests/data/demo')
|
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)
|
FileUtils.load_file(testcase_file_path)
|
||||||
|
|
||||||
def test_load_json_testcases(self):
|
def test_load_json_testcases(self):
|
||||||
@@ -163,11 +163,11 @@ class TestUtils(ApiServerUnittest):
|
|||||||
self.assertEqual(result, 3)
|
self.assertEqual(result, 3)
|
||||||
|
|
||||||
query = "ids.str_key"
|
query = "ids.str_key"
|
||||||
with self.assertRaises(exception.ParseResponseError):
|
with self.assertRaises(exception.ParseResponseFailure):
|
||||||
utils.query_json(json_content, query)
|
utils.query_json(json_content, query)
|
||||||
|
|
||||||
query = "ids.5"
|
query = "ids.5"
|
||||||
with self.assertRaises(exception.ParseResponseError):
|
with self.assertRaises(exception.ParseResponseFailure):
|
||||||
utils.query_json(json_content, query)
|
utils.query_json(json_content, query)
|
||||||
|
|
||||||
query = "person.age"
|
query = "person.age"
|
||||||
@@ -175,7 +175,7 @@ class TestUtils(ApiServerUnittest):
|
|||||||
self.assertEqual(result, 29)
|
self.assertEqual(result, 29)
|
||||||
|
|
||||||
query = "person.not_exist_key"
|
query = "person.not_exist_key"
|
||||||
with self.assertRaises(exception.ParseResponseError):
|
with self.assertRaises(exception.ParseResponseFailure):
|
||||||
utils.query_json(json_content, query)
|
utils.query_json(json_content, query)
|
||||||
|
|
||||||
query = "person.cities.0"
|
query = "person.cities.0"
|
||||||
@@ -189,12 +189,12 @@ class TestUtils(ApiServerUnittest):
|
|||||||
def test_query_json_content_is_text(self):
|
def test_query_json_content_is_text(self):
|
||||||
json_content = ""
|
json_content = ""
|
||||||
query = "key"
|
query = "key"
|
||||||
with self.assertRaises(exception.ResponseError):
|
with self.assertRaises(exception.ResponseFailure):
|
||||||
utils.query_json(json_content, query)
|
utils.query_json(json_content, query)
|
||||||
|
|
||||||
json_content = "<html><body>content</body></html>"
|
json_content = "<html><body>content</body></html>"
|
||||||
query = "key"
|
query = "key"
|
||||||
with self.assertRaises(exception.ParseResponseError):
|
with self.assertRaises(exception.ParseResponseFailure):
|
||||||
utils.query_json(json_content, query)
|
utils.query_json(json_content, query)
|
||||||
|
|
||||||
def test_get_uniform_comparator(self):
|
def test_get_uniform_comparator(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user