[Python+unittest interface automated test combat (1)] Pre-method: random generate random numbers and strings

"""
使用random生成随机数字、字符串
"""
# coding=UTF-8
import random
import string

 

def random_strs(i):
    # 生成长度i的随机字符串
    strings = ''.join(random.sample(
        # "'\/\n\t
        """abcdefghijklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ!@#$%^&*()_+-=<>?:;|,.1234567890"""*100, i
        )
    )
    return strings


def random_int_number(m, n):
    # 生成从m到n的随机整数
    int_number = random.randint(m, n)
    return int_number


def random_float_number(m, n, x):
    # 生成从m到n的随机浮点数,小数位保留x位
    float_number = round(random.uniform(m, n), x)
    return float_number

"""
其他生成随机字符串、数字方式
"""
# 从a-zA-Z0-9生成指定数量的随机字符
random_str = ''.join(random.sample(string.ascii_letters + string.digits, 5))
# 从多个字符中选取指定数量的字符组成新字符串:
random_str1 = ''.join(random.sample(['z', 'y', 'x', 'w', 'v', 'u', 't', 's', 'r', 'q', 'p', 'o',
                                     'n', 'm', 'l', 'k', 'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'print.log'], 5))
# 生成一个随机字符
random_str2 = random.choice('''abcdefghijklmnopqrstuvwxyz!@#$%^&*()_+-=<>?:;"'|,.\/''')
# 生成指定数量的随机特殊字符:
random_str3 = ''.join(random.sample("""abcdefghijklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ!@#$%^&*()_+-=<>?:;"'|,.\/'""", 2))
# # 随机整数
# random_int = random.randint(1, 100)
# # 随机浮点数
# random_float = round(random.uniform(100, 1000), 2)
# # 打乱排序
# items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
# random.shuffle(items)

Guess you like

Origin blog.csdn.net/kk_gods/article/details/109053179