mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-04 23:16:57 +08:00
refactor compatibility, learned from requests
This commit is contained in:
@@ -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.3.2'
|
__version__ = '1.3.3'
|
||||||
__author__ = 'debugtalk'
|
__author__ = 'debugtalk'
|
||||||
__author_email__ = 'mail@debugtalk.com'
|
__author_email__ = 'mail@debugtalk.com'
|
||||||
__license__ = 'MIT'
|
__license__ = 'MIT'
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
"""
|
"""
|
||||||
Built-in dependent functions used in YAML/JSON testcases.
|
Built-in dependent functions used in YAML/JSON testcases.
|
||||||
"""
|
"""
|
||||||
import json
|
|
||||||
import datetime
|
import datetime
|
||||||
|
import json
|
||||||
import random
|
import random
|
||||||
import re
|
import re
|
||||||
import string
|
import string
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
from httprunner.compat import basestring
|
||||||
from httprunner.exception import ParamsError
|
from httprunner.exception import ParamsError
|
||||||
from httprunner.utils import string_type
|
|
||||||
|
|
||||||
|
|
||||||
def gen_random_string(str_len):
|
def gen_random_string(str_len):
|
||||||
@@ -76,11 +76,11 @@ def length_less_than_or_equals(check_value, expect_value):
|
|||||||
assert len(check_value) <= expect_value
|
assert len(check_value) <= expect_value
|
||||||
|
|
||||||
def contains(check_value, expect_value):
|
def contains(check_value, expect_value):
|
||||||
assert isinstance(check_value, (list, tuple, dict, string_type))
|
assert isinstance(check_value, (list, tuple, dict, basestring))
|
||||||
assert expect_value in check_value
|
assert expect_value in check_value
|
||||||
|
|
||||||
def contained_by(check_value, expect_value):
|
def contained_by(check_value, expect_value):
|
||||||
assert isinstance(expect_value, (list, tuple, dict, string_type))
|
assert isinstance(expect_value, (list, tuple, dict, basestring))
|
||||||
assert check_value in expect_value
|
assert check_value in expect_value
|
||||||
|
|
||||||
def type_match(check_value, expect_value):
|
def type_match(check_value, expect_value):
|
||||||
@@ -98,8 +98,8 @@ def type_match(check_value, expect_value):
|
|||||||
assert isinstance(check_value, get_type(expect_value))
|
assert isinstance(check_value, get_type(expect_value))
|
||||||
|
|
||||||
def regex_match(check_value, expect_value):
|
def regex_match(check_value, expect_value):
|
||||||
assert isinstance(expect_value, string_type)
|
assert isinstance(expect_value, basestring)
|
||||||
assert isinstance(check_value, string_type)
|
assert isinstance(check_value, basestring)
|
||||||
assert re.match(expect_value, check_value)
|
assert re.match(expect_value, check_value)
|
||||||
|
|
||||||
def startswith(check_value, expect_value):
|
def startswith(check_value, expect_value):
|
||||||
|
|||||||
+3
-2
@@ -7,8 +7,9 @@ import unittest
|
|||||||
from httprunner import logger
|
from httprunner import logger
|
||||||
from httprunner.__about__ import __version__
|
from httprunner.__about__ import __version__
|
||||||
from httprunner.task import HttpRunner
|
from httprunner.task import HttpRunner
|
||||||
from httprunner.utils import (create_scaffold, load_dot_env_file, print_output,
|
from httprunner.utils import (create_scaffold, load_dot_env_file,
|
||||||
string_type, validate_json_file, prettify_json_file)
|
prettify_json_file, print_output,
|
||||||
|
validate_json_file)
|
||||||
|
|
||||||
|
|
||||||
def main_hrun():
|
def main_hrun():
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ import time
|
|||||||
import requests
|
import requests
|
||||||
import urllib3
|
import urllib3
|
||||||
from httprunner import logger
|
from httprunner import logger
|
||||||
|
from httprunner.compat import is_py2
|
||||||
from httprunner.exception import ParamsError
|
from httprunner.exception import ParamsError
|
||||||
from httprunner.utils import PYTHON_VERSION
|
|
||||||
from requests import Request, Response
|
from requests import Request, Response
|
||||||
from requests.exceptions import (InvalidSchema, InvalidURL, MissingSchema,
|
from requests.exceptions import (InvalidSchema, InvalidURL, MissingSchema,
|
||||||
RequestException)
|
RequestException)
|
||||||
@@ -116,7 +116,7 @@ class HttpSession(requests.Session):
|
|||||||
self.meta_data["response_headers"] = response.headers
|
self.meta_data["response_headers"] = response.headers
|
||||||
|
|
||||||
self.meta_data["response_body"] = response.text
|
self.meta_data["response_body"] = response.text
|
||||||
if PYTHON_VERSION == 2 and isinstance(self.meta_data["response_body"], unicode):
|
if is_py2 and isinstance(self.meta_data["response_body"], unicode):
|
||||||
self.meta_data["response_body"] = self.meta_data["response_body"].encode("utf-8")
|
self.meta_data["response_body"] = self.meta_data["response_body"].encode("utf-8")
|
||||||
|
|
||||||
logger.log_debug("response status_code: {}".format(self.meta_data["status_code"]))
|
logger.log_debug("response status_code: {}".format(self.meta_data["status_code"]))
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
"""
|
||||||
|
httprunner.compat
|
||||||
|
~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
This module handles import compatibility issues between Python 2 and
|
||||||
|
Python 3.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import sys
|
||||||
|
|
||||||
|
# -------
|
||||||
|
# Pythons
|
||||||
|
# -------
|
||||||
|
|
||||||
|
# Syntax sugar.
|
||||||
|
_ver = sys.version_info
|
||||||
|
|
||||||
|
#: Python 2.x?
|
||||||
|
is_py2 = (_ver[0] == 2)
|
||||||
|
|
||||||
|
#: Python 3.x?
|
||||||
|
is_py3 = (_ver[0] == 3)
|
||||||
|
|
||||||
|
try:
|
||||||
|
import simplejson as json
|
||||||
|
except ImportError:
|
||||||
|
import json
|
||||||
|
|
||||||
|
# ---------
|
||||||
|
# Specifics
|
||||||
|
# ---------
|
||||||
|
|
||||||
|
if is_py2:
|
||||||
|
from urllib3.packages.ordered_dict import OrderedDict
|
||||||
|
|
||||||
|
builtin_str = str
|
||||||
|
bytes = str
|
||||||
|
str = unicode
|
||||||
|
basestring = basestring
|
||||||
|
numeric_types = (int, long, float)
|
||||||
|
integer_types = (int, long)
|
||||||
|
|
||||||
|
elif is_py3:
|
||||||
|
from collections import OrderedDict
|
||||||
|
|
||||||
|
builtin_str = str
|
||||||
|
str = str
|
||||||
|
bytes = bytes
|
||||||
|
basestring = (str, bytes)
|
||||||
|
numeric_types = (int, float)
|
||||||
|
integer_types = (int,)
|
||||||
@@ -2,9 +2,9 @@ import copy
|
|||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
from collections import OrderedDict
|
|
||||||
|
|
||||||
from httprunner import exception, testcase, utils
|
from httprunner import exception, testcase, utils
|
||||||
|
from httprunner.compat import OrderedDict
|
||||||
|
|
||||||
|
|
||||||
class Context(object):
|
class Context(object):
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import re
|
import re
|
||||||
from collections import OrderedDict
|
|
||||||
|
|
||||||
from httprunner import exception, logger, testcase, utils
|
from httprunner import exception, logger, testcase, utils
|
||||||
|
from httprunner.compat import OrderedDict, basestring
|
||||||
from requests.structures import CaseInsensitiveDict
|
from requests.structures import CaseInsensitiveDict
|
||||||
|
|
||||||
text_extractor_regexp_compile = re.compile(r".*\(.*\).*")
|
text_extractor_regexp_compile = re.compile(r".*\(.*\).*")
|
||||||
@@ -116,7 +116,7 @@ class ResponseObject(object):
|
|||||||
extract_binds_order_dict = utils.convert_to_order_dict(extractors)
|
extract_binds_order_dict = utils.convert_to_order_dict(extractors)
|
||||||
|
|
||||||
for key, field in extract_binds_order_dict.items():
|
for key, field in extract_binds_order_dict.items():
|
||||||
if not isinstance(field, utils.string_type):
|
if not isinstance(field, basestring):
|
||||||
raise exception.ParamsError("invalid extractors in testcase!")
|
raise exception.ParamsError("invalid extractors in testcase!")
|
||||||
|
|
||||||
extracted_variables_mapping[key] = self.extract_field(field)
|
extracted_variables_mapping[key] = self.extract_field(field)
|
||||||
|
|||||||
+2
-1
@@ -3,6 +3,7 @@ import sys
|
|||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from httprunner import exception, logger, runner, testcase, utils
|
from httprunner import exception, logger, runner, testcase, utils
|
||||||
|
from httprunner.compat import is_py3
|
||||||
from httprunner.report import HtmlTestResult, get_summary
|
from httprunner.report import HtmlTestResult, get_summary
|
||||||
|
|
||||||
|
|
||||||
@@ -120,7 +121,7 @@ class TestSuite(unittest.TestSuite):
|
|||||||
return parametered_variables_list
|
return parametered_variables_list
|
||||||
|
|
||||||
def _add_test_to_suite(self, testcase_name, test_runner, testcase_dict):
|
def _add_test_to_suite(self, testcase_name, test_runner, testcase_dict):
|
||||||
if utils.PYTHON_VERSION == 3:
|
if is_py3:
|
||||||
TestCase.runTest.__doc__ = testcase_name
|
TestCase.runTest.__doc__ = testcase_name
|
||||||
else:
|
else:
|
||||||
TestCase.runTest.__func__.__doc__ = testcase_name
|
TestCase.runTest.__func__.__doc__ = testcase_name
|
||||||
|
|||||||
@@ -6,10 +6,10 @@ import json
|
|||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
import re
|
import re
|
||||||
from collections import OrderedDict
|
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
from httprunner import exception, logger, utils
|
from httprunner import exception, logger, utils
|
||||||
|
from httprunner.compat import OrderedDict, numeric_types
|
||||||
|
|
||||||
variable_regexp = r"\$([\w_]+)"
|
variable_regexp = r"\$([\w_]+)"
|
||||||
function_regexp = r"\$\{([\w_]+\([\$\w\.\-_ =,]*\))\}"
|
function_regexp = r"\$\{([\w_]+\([\$\w\.\-_ =,]*\))\}"
|
||||||
@@ -541,7 +541,7 @@ def substitute_variables_with_mapping(content, mapping):
|
|||||||
if isinstance(content, bool):
|
if isinstance(content, bool):
|
||||||
return content
|
return content
|
||||||
|
|
||||||
if isinstance(content, (int, utils.long_type, float, complex)):
|
if isinstance(content, (numeric_types, type)):
|
||||||
return content
|
return content
|
||||||
|
|
||||||
if not content:
|
if not content:
|
||||||
@@ -852,7 +852,7 @@ class TestcaseParser(object):
|
|||||||
|
|
||||||
return evaluated_data
|
return evaluated_data
|
||||||
|
|
||||||
if isinstance(content, (int, utils.long_type, float, complex)):
|
if isinstance(content, (numeric_types, type)):
|
||||||
return content
|
return content
|
||||||
|
|
||||||
# content is in string format here
|
# content is in string format here
|
||||||
|
|||||||
+3
-12
@@ -10,20 +10,11 @@ import random
|
|||||||
import re
|
import re
|
||||||
import string
|
import string
|
||||||
import types
|
import types
|
||||||
from collections import OrderedDict
|
|
||||||
|
|
||||||
from httprunner import exception, logger
|
from httprunner import exception, logger
|
||||||
|
from httprunner.compat import OrderedDict, is_py2, is_py3
|
||||||
from requests.structures import CaseInsensitiveDict
|
from requests.structures import CaseInsensitiveDict
|
||||||
|
|
||||||
try:
|
|
||||||
string_type = basestring
|
|
||||||
long_type = long
|
|
||||||
PYTHON_VERSION = 2
|
|
||||||
except NameError:
|
|
||||||
string_type = str
|
|
||||||
long_type = int
|
|
||||||
PYTHON_VERSION = 3
|
|
||||||
|
|
||||||
SECRET_KEY = "DebugTalk"
|
SECRET_KEY = "DebugTalk"
|
||||||
|
|
||||||
|
|
||||||
@@ -205,7 +196,7 @@ def get_imported_module_from_file(file_path):
|
|||||||
""" import module from python file path and return imported module
|
""" import module from python file path and return imported module
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if PYTHON_VERSION == 3:
|
if is_py3:
|
||||||
imported_module = importlib.machinery.SourceFileLoader(
|
imported_module = importlib.machinery.SourceFileLoader(
|
||||||
'module_name', file_path).load_module()
|
'module_name', file_path).load_module()
|
||||||
else:
|
else:
|
||||||
@@ -355,7 +346,7 @@ def print_output(outputs):
|
|||||||
content = ""
|
content = ""
|
||||||
for variable, value in in_out.items():
|
for variable, value in in_out.items():
|
||||||
|
|
||||||
if PYTHON_VERSION == 2:
|
if is_py2:
|
||||||
if isinstance(variable, unicode):
|
if isinstance(variable, unicode):
|
||||||
variable = variable.encode("utf-8")
|
variable = variable.encode("utf-8")
|
||||||
if isinstance(value, unicode):
|
if isinstance(value, unicode):
|
||||||
|
|||||||
@@ -7,12 +7,8 @@ import string
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
try:
|
try:
|
||||||
string_type = basestring
|
|
||||||
PYTHON_VERSION = 2
|
|
||||||
import urllib
|
import urllib
|
||||||
except NameError:
|
except NameError:
|
||||||
string_type = str
|
|
||||||
PYTHON_VERSION = 3
|
|
||||||
import urllib.parse as urllib
|
import urllib.parse as urllib
|
||||||
|
|
||||||
SECRET_KEY = "DebugTalk"
|
SECRET_KEY = "DebugTalk"
|
||||||
|
|||||||
+4
-4
@@ -1,8 +1,8 @@
|
|||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
from collections import OrderedDict
|
|
||||||
|
|
||||||
from httprunner import exception, utils
|
from httprunner import exception, utils
|
||||||
|
from httprunner.compat import OrderedDict
|
||||||
from tests.base import ApiServerUnittest
|
from tests.base import ApiServerUnittest
|
||||||
|
|
||||||
|
|
||||||
@@ -180,11 +180,11 @@ class TestUtils(ApiServerUnittest):
|
|||||||
|
|
||||||
def test_filter_module_functions(self):
|
def test_filter_module_functions(self):
|
||||||
imported_module = utils.get_imported_module("httprunner.utils")
|
imported_module = utils.get_imported_module("httprunner.utils")
|
||||||
self.assertIn("PYTHON_VERSION", dir(imported_module))
|
self.assertIn("is_py3", dir(imported_module))
|
||||||
|
|
||||||
functions_dict = utils.filter_module(imported_module, "function")
|
functions_dict = utils.filter_module(imported_module, "function")
|
||||||
self.assertIn("filter_module", functions_dict)
|
self.assertIn("filter_module", functions_dict)
|
||||||
self.assertNotIn("PYTHON_VERSION", functions_dict)
|
self.assertNotIn("is_py3", functions_dict)
|
||||||
|
|
||||||
def test_get_imported_module_from_file(self):
|
def test_get_imported_module_from_file(self):
|
||||||
imported_module = utils.get_imported_module_from_file("tests/data/debugtalk.py")
|
imported_module = utils.get_imported_module_from_file("tests/data/debugtalk.py")
|
||||||
@@ -192,7 +192,7 @@ class TestUtils(ApiServerUnittest):
|
|||||||
|
|
||||||
functions_dict = utils.filter_module(imported_module, "function")
|
functions_dict = utils.filter_module(imported_module, "function")
|
||||||
self.assertIn("gen_md5", functions_dict)
|
self.assertIn("gen_md5", functions_dict)
|
||||||
self.assertNotIn("PYTHON_VERSION", functions_dict)
|
self.assertNotIn("urllib", functions_dict)
|
||||||
|
|
||||||
with self.assertRaises(exception.FileNotFoundError):
|
with self.assertRaises(exception.FileNotFoundError):
|
||||||
utils.get_imported_module_from_file("tests/data/debugtalk2.py")
|
utils.get_imported_module_from_file("tests/data/debugtalk2.py")
|
||||||
|
|||||||
Reference in New Issue
Block a user