diff --git a/httprunner/compat.py b/httprunner/compat.py index 3753affc..607c8b3d 100644 --- a/httprunner/compat.py +++ b/httprunner/compat.py @@ -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 diff --git a/httprunner/context.py b/httprunner/context.py index 1739e741..93e3176e 100644 --- a/httprunner/context.py +++ b/httprunner/context.py @@ -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 diff --git a/httprunner/exception.py b/httprunner/exception.py index 4bcef4d8..0e717f89 100644 --- a/httprunner/exception.py +++ b/httprunner/exception.py @@ -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 diff --git a/httprunner/response.py b/httprunner/response.py index ead684f5..e9f3e7b4 100644 --- a/httprunner/response.py +++ b/httprunner/response.py @@ -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 diff --git a/httprunner/runner.py b/httprunner/runner.py index 72dd7a4d..820b2a97 100644 --- a/httprunner/runner.py +++ b/httprunner/runner.py @@ -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", {})) diff --git a/httprunner/utils.py b/httprunner/utils.py index 3a508d86..c4401f37 100644 --- a/httprunner/utils.py +++ b/httprunner/utils.py @@ -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: diff --git a/tests/test_context.py b/tests/test_context.py index 585a45cf..c6511845 100644 --- a/tests/test_context.py +++ b/tests/test_context.py @@ -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) diff --git a/tests/test_httprunner.py b/tests/test_httprunner.py index cadcae15..869c6cdc 100644 --- a/tests/test_httprunner.py +++ b/tests/test_httprunner.py @@ -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) diff --git a/tests/test_response.py b/tests/test_response.py index f0879b3e..12fe3c0e 100644 --- a/tests/test_response.py +++ b/tests/test_response.py @@ -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) diff --git a/tests/test_runner.py b/tests/test_runner.py index 390956eb..7d829320 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -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): diff --git a/tests/test_testcase.py b/tests/test_testcase.py index 2e4af88c..443cf4fd 100644 --- a/tests/test_testcase.py +++ b/tests/test_testcase.py @@ -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 diff --git a/tests/test_utils.py b/tests/test_utils.py index 76bd64d8..a8dc781e 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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 = "content" query = "key" - with self.assertRaises(exception.ParseResponseError): + with self.assertRaises(exception.ParseResponseFailure): utils.query_json(json_content, query) def test_get_uniform_comparator(self):