zoukankan      html  css  js  c++  java
  • 接口测试系列:工作中所用(九:测试数据的生成:比如:整数、随机数、uuid等)

    import string
    import random
    import time
    import uuid
    
    
    class GenUtil:
        """
        生成测试数据。
        """
        __instance = None  # 实例
    
        def __new__(cls, *args, **kwargs):
            '''
            单实例
            :param args:
            :param kwargs:
            :return:
            '''
            if not cls.__instance:
                cls.__instance = super(GenUtil, cls).__new__(cls, *args)
            return cls.__instance
    
        def gen_phones(self, count = 1):
            '''
            生成手机号 1[3,4,5,7,8][1~9]
            :param count: 生成数量
            :return:list
            '''
            try:
                list = []
                for i in range(count):
                    list.append(
                            '1' + ''.join(random.choice("34578")) + ''.join(random.choice( "123456789")) + ''.join(
                                    random.sample( string.digits, 9)))
                if count == 1:
                    return list[0]
                else:
                    return list
            except Exception as e:
                print( "生成手机号出错!----" + str(e))
    
        def gen_digits(self, length = 10, count = 1):
            '''
            生成正整数
            :param length:正整数的位数
            :param count:生成数量
            :return:list
            '''
            try:
                list = []
                if length <= 10:
                    for i in range(count):
                        list.append(''.join(random.sample(string.digits, length)))
                elif length > 10:
                    for i in range(count):
                        list.append(''.join([random.choice(string.digits) for _ in range(length)]))
                if count == 1:
                    return list[0]
                else:
                    return list
            except Exception as e:
                print( "生成正整数出错!----" + str(e))
    
        def gen_chinese_name(self, count = 1):
            '''
            生成中文+时间(年月日时分秒)
            :param count:生成姓名的个数
            :return:
            '''
            try:
                list = []
                for i in range(count):
                    list.append('测试' + ''.join(time.strftime('%Y%m%d%H%M%S', time.localtime())))
                if count == 1:
                    return list[0]
                else:
                    return list
            except Exception as e:
                print( "生成中文+4位随机数出错!----" + str(e))
    
        def gen_characters(self, length = 10, count = 1):
            '''
            生成随机字符(字母+数字)
            :param length:单个字符串的长度
            :param count:生成字符串的个数
            :return:
            '''
            try:
                chars = string.ascii_letters + string.digits
                list = []
                if length <= 62:
                    for i in range(count):
                        list.append( ''.join(random.sample(chars, length)))
                    if count == 1:
                        return list[0]
                    else:
                        return list
                else:
                    for i in range(count):
                        list.append(''.join([random.choice(chars) for i in range(length)]))
                    if count == 1:
                        return list[0]
                    else:
                        return list
            except Exception as e:
                print( "生成随机字符(字母+数字)出错!----" + str(e))
    
        def gen_uuid(self, id):
            '''
            生成uuid
            '''
            name = 'test'
            namespace = uuid.NAMESPACE_URL
            if id == 1:
                return str(uuid.uuid1()).replace('-', '')
            if id == 3:
                return uuid.uuid3(namespace, name)
            if id == 4:
                return str(uuid.uuid4()).replace('-', '')
            if id == 5:
                return uuid.uuid5(namespace, name)
    View Code
  • 相关阅读:
    【Linux】项目部署
    【架构师之路】【MQ】消息队列
    【数据库】【Python】mysql
    【算法】【Python】找出字符串中重复出现的字符 并求出重复次数 且根据重复次数从大到小排列
    【Python】排序 按照list中的字典的某key排序
    Kettle Post请求webservice
    python+pytest+allure接口自动化测试框架
    Python+unittest+requests+htmlTestRunner+excel完整的接口自动化框架
    python实现栈的基本操作
    展示博客园顶部的随笔、文章、评论、阅读量统计数据
  • 原文地址:https://www.cnblogs.com/by170628/p/10096800.html
Copyright © 2011-2022 走看看