Files
httprunner/httprunner/built_in.py

104 lines
3.0 KiB
Python

"""
Built-in dependent functions used in YAML/JSON testcases.
"""
import datetime
import random
import re
import string
import time
from httprunner.exception import ParamsError
from httprunner.utils import string_type
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, int) and 0 < str_len < 17:
return 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(sec):
""" sleep specified seconds
"""
time.sleep(sec)
""" 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 str(check_value) == str(expect_value)
def length_equals(check_value, expect_value):
assert isinstance(expect_value, int)
assert len(check_value) == expect_value
def length_greater_than(check_value, expect_value):
assert isinstance(expect_value, int)
assert len(check_value) > expect_value
def length_greater_than_or_equals(check_value, expect_value):
assert isinstance(expect_value, int)
assert len(check_value) >= expect_value
def length_less_than(check_value, expect_value):
assert isinstance(expect_value, int)
assert len(check_value) < expect_value
def length_less_than_or_equals(check_value, expect_value):
assert isinstance(expect_value, int)
assert len(check_value) <= expect_value
def contains(check_value, expect_value):
assert isinstance(check_value, (list, tuple, dict, string_type))
assert expect_value in check_value
def contained_by(check_value, expect_value):
assert isinstance(expect_value, (list, tuple, dict, string_type))
assert check_value in expect_value
def type_match(check_value, expect_value):
assert isinstance(check_value, expect_value)
def regex_match(check_value, expect_value):
assert isinstance(expect_value, string_type)
assert isinstance(check_value, string_type)
assert re.match(expect_value, check_value)
def startswith(check_value, expect_value):
assert str(check_value).startswith(str(expect_value))
def endswith(check_value, expect_value):
assert str(check_value).endswith(str(expect_value))