mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-06 16:29:37 +08:00
change: make built_in comparators and functions as submodule
This commit is contained in:
@@ -1,206 +0,0 @@
|
|||||||
# encoding: utf-8
|
|
||||||
|
|
||||||
"""
|
|
||||||
Built-in dependent functions used in YAML/JSON testcases.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import datetime
|
|
||||||
import os
|
|
||||||
import random
|
|
||||||
import re
|
|
||||||
import string
|
|
||||||
import time
|
|
||||||
|
|
||||||
import filetype
|
|
||||||
from requests_toolbelt import MultipartEncoder
|
|
||||||
|
|
||||||
from httprunner.compat import basestring, builtin_str, integer_types
|
|
||||||
from httprunner.exceptions import ParamsError
|
|
||||||
|
|
||||||
PWD = os.getcwd()
|
|
||||||
|
|
||||||
|
|
||||||
###############################################################################
|
|
||||||
## built-in functions
|
|
||||||
###############################################################################
|
|
||||||
|
|
||||||
def gen_random_string(str_len):
|
|
||||||
""" generate random string with specified length
|
|
||||||
"""
|
|
||||||
return ''.join(
|
|
||||||
random.choice(string.ascii_letters + string.digits) for _ in range(str_len))
|
|
||||||
|
|
||||||
|
|
||||||
def get_timestamp(str_len=13):
|
|
||||||
""" get timestamp string, length can only between 0 and 16
|
|
||||||
"""
|
|
||||||
if isinstance(str_len, integer_types) and 0 < str_len < 17:
|
|
||||||
return builtin_str(time.time()).replace(".", "")[:str_len]
|
|
||||||
|
|
||||||
raise ParamsError("timestamp length can only between 0 and 16.")
|
|
||||||
|
|
||||||
|
|
||||||
def get_current_date(fmt="%Y-%m-%d"):
|
|
||||||
""" get current date, default format is %Y-%m-%d
|
|
||||||
"""
|
|
||||||
return datetime.datetime.now().strftime(fmt)
|
|
||||||
|
|
||||||
|
|
||||||
def sleep(n_secs):
|
|
||||||
""" sleep n seconds
|
|
||||||
"""
|
|
||||||
time.sleep(n_secs)
|
|
||||||
|
|
||||||
|
|
||||||
###############################################################################
|
|
||||||
## upload files with requests-toolbelt
|
|
||||||
# e.g.
|
|
||||||
# - test:
|
|
||||||
# name: upload file
|
|
||||||
# variables:
|
|
||||||
# file_path: "data/test.env"
|
|
||||||
# multipart_encoder: ${multipart_encoder(file=$file_path)}
|
|
||||||
# request:
|
|
||||||
# url: /post
|
|
||||||
# method: POST
|
|
||||||
# headers:
|
|
||||||
# Content-Type: ${multipart_content_type($multipart_encoder)}
|
|
||||||
# data: $multipart_encoder
|
|
||||||
# validate:
|
|
||||||
# - eq: ["status_code", 200]
|
|
||||||
# - startswith: ["content.files.file", "UserName=test"]
|
|
||||||
###############################################################################
|
|
||||||
|
|
||||||
def multipart_encoder(**kwargs):
|
|
||||||
""" initialize MultipartEncoder with uploading fields.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def get_filetype(file_path):
|
|
||||||
file_type = filetype.guess(file_path)
|
|
||||||
if file_type:
|
|
||||||
return file_type.mime
|
|
||||||
else:
|
|
||||||
return "text/html"
|
|
||||||
|
|
||||||
fields_dict = {}
|
|
||||||
for key, value in kwargs.items():
|
|
||||||
|
|
||||||
if os.path.isabs(value):
|
|
||||||
_file_path = value
|
|
||||||
is_file = True
|
|
||||||
else:
|
|
||||||
global PWD
|
|
||||||
_file_path = os.path.join(PWD, value)
|
|
||||||
is_file = os.path.isfile(_file_path)
|
|
||||||
|
|
||||||
if is_file:
|
|
||||||
filename = os.path.basename(_file_path)
|
|
||||||
with open(_file_path, 'rb') as f:
|
|
||||||
mime_type = get_filetype(_file_path)
|
|
||||||
fields_dict[key] = (filename, f.read(), mime_type)
|
|
||||||
else:
|
|
||||||
fields_dict[key] = value
|
|
||||||
|
|
||||||
return MultipartEncoder(fields=fields_dict)
|
|
||||||
|
|
||||||
|
|
||||||
def multipart_content_type(multipart_encoder):
|
|
||||||
""" prepare Content-Type for request headers
|
|
||||||
"""
|
|
||||||
return multipart_encoder.content_type
|
|
||||||
|
|
||||||
|
|
||||||
###############################################################################
|
|
||||||
## built-in comparators
|
|
||||||
###############################################################################
|
|
||||||
|
|
||||||
def equals(check_value, expect_value):
|
|
||||||
assert check_value == expect_value
|
|
||||||
|
|
||||||
|
|
||||||
def less_than(check_value, expect_value):
|
|
||||||
assert check_value < expect_value
|
|
||||||
|
|
||||||
|
|
||||||
def less_than_or_equals(check_value, expect_value):
|
|
||||||
assert check_value <= expect_value
|
|
||||||
|
|
||||||
|
|
||||||
def greater_than(check_value, expect_value):
|
|
||||||
assert check_value > expect_value
|
|
||||||
|
|
||||||
|
|
||||||
def greater_than_or_equals(check_value, expect_value):
|
|
||||||
assert check_value >= expect_value
|
|
||||||
|
|
||||||
|
|
||||||
def not_equals(check_value, expect_value):
|
|
||||||
assert check_value != expect_value
|
|
||||||
|
|
||||||
|
|
||||||
def string_equals(check_value, expect_value):
|
|
||||||
assert builtin_str(check_value) == builtin_str(expect_value)
|
|
||||||
|
|
||||||
|
|
||||||
def length_equals(check_value, expect_value):
|
|
||||||
assert isinstance(expect_value, integer_types)
|
|
||||||
assert len(check_value) == expect_value
|
|
||||||
|
|
||||||
|
|
||||||
def length_greater_than(check_value, expect_value):
|
|
||||||
assert isinstance(expect_value, integer_types)
|
|
||||||
assert len(check_value) > expect_value
|
|
||||||
|
|
||||||
|
|
||||||
def length_greater_than_or_equals(check_value, expect_value):
|
|
||||||
assert isinstance(expect_value, integer_types)
|
|
||||||
assert len(check_value) >= expect_value
|
|
||||||
|
|
||||||
|
|
||||||
def length_less_than(check_value, expect_value):
|
|
||||||
assert isinstance(expect_value, integer_types)
|
|
||||||
assert len(check_value) < expect_value
|
|
||||||
|
|
||||||
|
|
||||||
def length_less_than_or_equals(check_value, expect_value):
|
|
||||||
assert isinstance(expect_value, integer_types)
|
|
||||||
assert len(check_value) <= expect_value
|
|
||||||
|
|
||||||
|
|
||||||
def contains(check_value, expect_value):
|
|
||||||
assert isinstance(check_value, (list, tuple, dict, basestring))
|
|
||||||
assert expect_value in check_value
|
|
||||||
|
|
||||||
|
|
||||||
def contained_by(check_value, expect_value):
|
|
||||||
assert isinstance(expect_value, (list, tuple, dict, basestring))
|
|
||||||
assert check_value in expect_value
|
|
||||||
|
|
||||||
|
|
||||||
def type_match(check_value, expect_value):
|
|
||||||
def get_type(name):
|
|
||||||
if isinstance(name, type):
|
|
||||||
return name
|
|
||||||
elif isinstance(name, basestring):
|
|
||||||
try:
|
|
||||||
return __builtins__[name]
|
|
||||||
except KeyError:
|
|
||||||
raise ValueError(name)
|
|
||||||
else:
|
|
||||||
raise ValueError(name)
|
|
||||||
|
|
||||||
assert isinstance(check_value, get_type(expect_value))
|
|
||||||
|
|
||||||
|
|
||||||
def regex_match(check_value, expect_value):
|
|
||||||
assert isinstance(expect_value, basestring)
|
|
||||||
assert isinstance(check_value, basestring)
|
|
||||||
assert re.match(expect_value, check_value)
|
|
||||||
|
|
||||||
|
|
||||||
def startswith(check_value, expect_value):
|
|
||||||
assert builtin_str(check_value).startswith(builtin_str(expect_value))
|
|
||||||
|
|
||||||
|
|
||||||
def endswith(check_value, expect_value):
|
|
||||||
assert builtin_str(check_value).endswith(builtin_str(expect_value))
|
|
||||||
0
httprunner/builtin/__init__.py
Normal file
0
httprunner/builtin/__init__.py
Normal file
99
httprunner/builtin/comparators.py
Normal file
99
httprunner/builtin/comparators.py
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
"""
|
||||||
|
Built-in validate comparators.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
|
from httprunner.compat import basestring, builtin_str, integer_types
|
||||||
|
|
||||||
|
|
||||||
|
def equals(check_value, expect_value):
|
||||||
|
assert check_value == expect_value
|
||||||
|
|
||||||
|
|
||||||
|
def less_than(check_value, expect_value):
|
||||||
|
assert check_value < expect_value
|
||||||
|
|
||||||
|
|
||||||
|
def less_than_or_equals(check_value, expect_value):
|
||||||
|
assert check_value <= expect_value
|
||||||
|
|
||||||
|
|
||||||
|
def greater_than(check_value, expect_value):
|
||||||
|
assert check_value > expect_value
|
||||||
|
|
||||||
|
|
||||||
|
def greater_than_or_equals(check_value, expect_value):
|
||||||
|
assert check_value >= expect_value
|
||||||
|
|
||||||
|
|
||||||
|
def not_equals(check_value, expect_value):
|
||||||
|
assert check_value != expect_value
|
||||||
|
|
||||||
|
|
||||||
|
def string_equals(check_value, expect_value):
|
||||||
|
assert builtin_str(check_value) == builtin_str(expect_value)
|
||||||
|
|
||||||
|
|
||||||
|
def length_equals(check_value, expect_value):
|
||||||
|
assert isinstance(expect_value, integer_types)
|
||||||
|
assert len(check_value) == expect_value
|
||||||
|
|
||||||
|
|
||||||
|
def length_greater_than(check_value, expect_value):
|
||||||
|
assert isinstance(expect_value, integer_types)
|
||||||
|
assert len(check_value) > expect_value
|
||||||
|
|
||||||
|
|
||||||
|
def length_greater_than_or_equals(check_value, expect_value):
|
||||||
|
assert isinstance(expect_value, integer_types)
|
||||||
|
assert len(check_value) >= expect_value
|
||||||
|
|
||||||
|
|
||||||
|
def length_less_than(check_value, expect_value):
|
||||||
|
assert isinstance(expect_value, integer_types)
|
||||||
|
assert len(check_value) < expect_value
|
||||||
|
|
||||||
|
|
||||||
|
def length_less_than_or_equals(check_value, expect_value):
|
||||||
|
assert isinstance(expect_value, integer_types)
|
||||||
|
assert len(check_value) <= expect_value
|
||||||
|
|
||||||
|
|
||||||
|
def contains(check_value, expect_value):
|
||||||
|
assert isinstance(check_value, (list, tuple, dict, basestring))
|
||||||
|
assert expect_value in check_value
|
||||||
|
|
||||||
|
|
||||||
|
def contained_by(check_value, expect_value):
|
||||||
|
assert isinstance(expect_value, (list, tuple, dict, basestring))
|
||||||
|
assert check_value in expect_value
|
||||||
|
|
||||||
|
|
||||||
|
def type_match(check_value, expect_value):
|
||||||
|
def get_type(name):
|
||||||
|
if isinstance(name, type):
|
||||||
|
return name
|
||||||
|
elif isinstance(name, basestring):
|
||||||
|
try:
|
||||||
|
return __builtins__[name]
|
||||||
|
except KeyError:
|
||||||
|
raise ValueError(name)
|
||||||
|
else:
|
||||||
|
raise ValueError(name)
|
||||||
|
|
||||||
|
assert isinstance(check_value, get_type(expect_value))
|
||||||
|
|
||||||
|
|
||||||
|
def regex_match(check_value, expect_value):
|
||||||
|
assert isinstance(expect_value, basestring)
|
||||||
|
assert isinstance(check_value, basestring)
|
||||||
|
assert re.match(expect_value, check_value)
|
||||||
|
|
||||||
|
|
||||||
|
def startswith(check_value, expect_value):
|
||||||
|
assert builtin_str(check_value).startswith(builtin_str(expect_value))
|
||||||
|
|
||||||
|
|
||||||
|
def endswith(check_value, expect_value):
|
||||||
|
assert builtin_str(check_value).endswith(builtin_str(expect_value))
|
||||||
105
httprunner/builtin/functions.py
Normal file
105
httprunner/builtin/functions.py
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
"""
|
||||||
|
Built-in functions used in YAML/JSON testcases.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import datetime
|
||||||
|
import os
|
||||||
|
import random
|
||||||
|
import string
|
||||||
|
import time
|
||||||
|
|
||||||
|
import filetype
|
||||||
|
from requests_toolbelt import MultipartEncoder
|
||||||
|
|
||||||
|
from httprunner.compat import builtin_str, integer_types
|
||||||
|
from httprunner.exceptions import ParamsError
|
||||||
|
|
||||||
|
PWD = os.getcwd()
|
||||||
|
|
||||||
|
|
||||||
|
def gen_random_string(str_len):
|
||||||
|
""" generate random string with specified length
|
||||||
|
"""
|
||||||
|
return ''.join(
|
||||||
|
random.choice(string.ascii_letters + string.digits) for _ in range(str_len))
|
||||||
|
|
||||||
|
|
||||||
|
def get_timestamp(str_len=13):
|
||||||
|
""" get timestamp string, length can only between 0 and 16
|
||||||
|
"""
|
||||||
|
if isinstance(str_len, integer_types) and 0 < str_len < 17:
|
||||||
|
return builtin_str(time.time()).replace(".", "")[:str_len]
|
||||||
|
|
||||||
|
raise ParamsError("timestamp length can only between 0 and 16.")
|
||||||
|
|
||||||
|
|
||||||
|
def get_current_date(fmt="%Y-%m-%d"):
|
||||||
|
""" get current date, default format is %Y-%m-%d
|
||||||
|
"""
|
||||||
|
return datetime.datetime.now().strftime(fmt)
|
||||||
|
|
||||||
|
|
||||||
|
def sleep(n_secs):
|
||||||
|
""" sleep n seconds
|
||||||
|
"""
|
||||||
|
time.sleep(n_secs)
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
upload files with requests-toolbelt
|
||||||
|
e.g.
|
||||||
|
|
||||||
|
- test:
|
||||||
|
name: upload file
|
||||||
|
variables:
|
||||||
|
file_path: "data/test.env"
|
||||||
|
multipart_encoder: ${multipart_encoder(file=$file_path)}
|
||||||
|
request:
|
||||||
|
url: /post
|
||||||
|
method: POST
|
||||||
|
headers:
|
||||||
|
Content-Type: ${multipart_content_type($multipart_encoder)}
|
||||||
|
data: $multipart_encoder
|
||||||
|
validate:
|
||||||
|
- eq: ["status_code", 200]
|
||||||
|
- startswith: ["content.files.file", "UserName=test"]
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def multipart_encoder(**kwargs):
|
||||||
|
""" initialize MultipartEncoder with uploading fields.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def get_filetype(file_path):
|
||||||
|
file_type = filetype.guess(file_path)
|
||||||
|
if file_type:
|
||||||
|
return file_type.mime
|
||||||
|
else:
|
||||||
|
return "text/html"
|
||||||
|
|
||||||
|
fields_dict = {}
|
||||||
|
for key, value in kwargs.items():
|
||||||
|
|
||||||
|
if os.path.isabs(value):
|
||||||
|
_file_path = value
|
||||||
|
is_file = True
|
||||||
|
else:
|
||||||
|
global PWD
|
||||||
|
_file_path = os.path.join(PWD, value)
|
||||||
|
is_file = os.path.isfile(_file_path)
|
||||||
|
|
||||||
|
if is_file:
|
||||||
|
filename = os.path.basename(_file_path)
|
||||||
|
with open(_file_path, 'rb') as f:
|
||||||
|
mime_type = get_filetype(_file_path)
|
||||||
|
fields_dict[key] = (filename, f.read(), mime_type)
|
||||||
|
else:
|
||||||
|
fields_dict[key] = value
|
||||||
|
|
||||||
|
return MultipartEncoder(fields=fields_dict)
|
||||||
|
|
||||||
|
|
||||||
|
def multipart_content_type(multipart_encoder):
|
||||||
|
""" prepare Content-Type for request headers
|
||||||
|
"""
|
||||||
|
return multipart_encoder.content_type
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
import importlib
|
import importlib
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from httprunner import built_in, exceptions, logger, utils
|
from httprunner import exceptions, logger, utils
|
||||||
|
from httprunner.builtin import functions
|
||||||
from httprunner.loader.load import load_module_functions, load_folder_content, load_file, load_dot_env_file, \
|
from httprunner.loader.load import load_module_functions, load_folder_content, load_file, load_dot_env_file, \
|
||||||
load_folder_files
|
load_folder_files
|
||||||
from httprunner.loader.locate import init_project_working_directory, get_project_working_directory
|
from httprunner.loader.locate import init_project_working_directory, get_project_working_directory
|
||||||
@@ -481,7 +482,7 @@ def load_project_data(test_path, dot_env_path=None):
|
|||||||
# locate PWD and load debugtalk.py functions
|
# locate PWD and load debugtalk.py functions
|
||||||
|
|
||||||
project_mapping["PWD"] = project_working_directory
|
project_mapping["PWD"] = project_working_directory
|
||||||
built_in.PWD = project_working_directory
|
functions.PWD = project_working_directory # TODO: remove
|
||||||
project_mapping["functions"] = debugtalk_functions
|
project_mapping["functions"] = debugtalk_functions
|
||||||
project_mapping["test_path"] = test_path
|
project_mapping["test_path"] = test_path
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import os
|
|||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from httprunner import built_in
|
from httprunner.builtin import functions
|
||||||
from httprunner import exceptions, logger, utils
|
from httprunner import exceptions, logger, utils
|
||||||
from httprunner.loader.check import check_testcase_format, is_function
|
from httprunner.loader.check import check_testcase_format, is_function
|
||||||
from httprunner.loader.locate import get_project_working_directory
|
from httprunner.loader.locate import get_project_working_directory
|
||||||
@@ -235,7 +235,7 @@ def load_module_functions(module):
|
|||||||
|
|
||||||
|
|
||||||
def load_builtin_functions():
|
def load_builtin_functions():
|
||||||
""" load built_in module functions
|
""" load builtin module functions
|
||||||
"""
|
"""
|
||||||
return load_module_functions(built_in)
|
return load_module_functions(functions)
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from functools import wraps
|
|||||||
|
|
||||||
from flask import Flask, make_response, request
|
from flask import Flask, make_response, request
|
||||||
|
|
||||||
from httprunner.built_in import gen_random_string
|
from httprunner.builtin.functions import gen_random_string
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from httpbin import app as httpbin_app
|
from httpbin import app as httpbin_app
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import requests
|
import requests
|
||||||
|
|
||||||
from httprunner import built_in, exceptions, loader, response
|
from httprunner import exceptions, response
|
||||||
from httprunner.compat import basestring, bytes
|
from httprunner.compat import basestring, bytes
|
||||||
from tests.api_server import HTTPBIN_SERVER
|
from tests.api_server import HTTPBIN_SERVER
|
||||||
from tests.base import ApiServerUnittest
|
from tests.base import ApiServerUnittest
|
||||||
@@ -8,9 +8,6 @@ from tests.base import ApiServerUnittest
|
|||||||
|
|
||||||
class TestResponse(ApiServerUnittest):
|
class TestResponse(ApiServerUnittest):
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
self.functions_mapping = loader.load.load_module_functions(built_in)
|
|
||||||
|
|
||||||
def test_parse_response_object_json(self):
|
def test_parse_response_object_json(self):
|
||||||
url = "http://127.0.0.1:5000/api/users"
|
url = "http://127.0.0.1:5000/api/users"
|
||||||
resp = requests.get(url)
|
resp = requests.get(url)
|
||||||
|
|||||||
@@ -62,8 +62,8 @@ class TestUtils(ApiServerUnittest):
|
|||||||
self.assertEqual(result, "L")
|
self.assertEqual(result, "L")
|
||||||
|
|
||||||
def current_validators(self):
|
def current_validators(self):
|
||||||
from httprunner import built_in
|
from httprunner.builtin import comparators
|
||||||
functions_mapping = loader.load.load_module_functions(built_in)
|
functions_mapping = loader.load.load_module_functions(comparators)
|
||||||
|
|
||||||
functions_mapping["equals"](None, None)
|
functions_mapping["equals"](None, None)
|
||||||
functions_mapping["equals"](1, 1)
|
functions_mapping["equals"](1, 1)
|
||||||
|
|||||||
Reference in New Issue
Block a user