【python+unittest 接口自动化测试实战 (一)】前置方法:random生成随机数字、字符串

"""
使用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)
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值