欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用 Python 中 re 模块对测试用例参数化,进行搜索 search、替换 sub

程序员文章站 2023-09-29 08:10:33
参数化的目的:运行自动化测试用例的时候参数都不需要改变,直接使用封装好的类进行参数化,发起请求时直接使用替换后参数; 自动化测试用例,如果一百个接口要在Excel写100个sheet表单,每个接口有10个字段,里面有5个都可能是变化的,需要使用参数化,先试用特定的字符在用例中进行站位,在发起请求构造 ......

  参数化的目的:运行自动化测试用例的时候参数都不需要改变,直接使用封装好的类进行参数化,发起请求时直接使用替换后参数;

  自动化测试用例,如果一百个接口要在excel写100个sheet表单,每个接口有10个字段,里面有5个都可能是变化的,需要使用参数化,先试用特定的字符在用例中进行站位,在发起请求构造参数时在进行替换占位符;

一、用力中手机号的替换,以字符串中的方法,使用 replace (译:瑞破类似) 进行替换

# 原始字符串:{"mobilephone": "${not_existed_tel}", "pwd":"123456"}
# json 字符串
src_str = '{"mobilephone": "${not_existed_tel}", "pwd":"123456"}'
# 替换 json 字符串中 ${not_existed_tel} 为 18845820369
print(src_str.replace("${not_existed_tel}", "18845820369"))

# 结果:{"mobilephone": "18845820369", "pwd":"123456"}

二、使用 re 模块进行替换

  re 正则表达式,是一个查找、搜索、替换文本的一种格式语言

搜索方法一,re 模块中的 match(译:马驰)方法,match方法是从头开始匹配

import re


# 1. 创建原始字符串(待替换的字符串)
src_str4 = '{"mobilephone": "${not_existed_tel}", "pwd":"123456"}'

# 2. 定义模式字符串去进行匹配
# 模式字符串 == 模子,以这个模板去原始字符串中进行比对

# 方法一,re 正则中的 match(译:马驰)方法,match方法是从头开始匹配
# 从 { 开始,匹配不上, 那么就回复none
res = re.match("{not_existed_tel}", src_str4)
print(res)  # 结果:none

# 从 { 开始,能匹配上, 会返回match对象,加r不需要转义
res = re.match(r'{"mobilephone"', src_str4)
print(res)      # 返回match对象结果:<re.match object; span=(0, 14), match='{"mobilephone"'>

# 获取匹配的结果内容 group (译:歌如破)
a = res.group()
print(a)        # 结果:{"mobilephone"

搜索方法二:search   (译:涩吃)方法,只匹配一次

import re


# 1. 创建原始字符串(待替换的字符串)
src_str1 = '{"mobilephone": "${not_existed_tel}", "pwd":"123456"}'

# 2. 定义模式字符串去进行匹配
# 模式字符串 == 模子,以这个模板去原始字符串中进行比对:"${not_existed_tel}"

# 方法二:search (译:涩吃)方法,只匹配一次
# 如果能匹配上会返回match对象,匹配不上会返回none
# 美元符号需要转义加 r,特殊字符都需要转义
res1 = re.search(r"\${not_existed_tel}", src_str1)
print(res1)     # match对象结果:<re.match object; span=(17, 35), match='${not_existed_tel}'>

# 获取匹配到的结果,group(译:格如破)方法
b = res1.group()
print(b)    # 结果:${not_existed_tel}

搜索方法三:findall  (译:范德奥)方法,会匹配很多次

src_str1 = '{"mobilephone": "${not_existed_tel}", "pwd":"123456"}'
res2 = re.findall(r'o', src_str1)
print(res2)     # 直接返回列表结果:['o', 'o', 'o']

替换方法: sub  (译:萨博)方法

import re


# 1. 创建原始字符串(待替换的字符串)
src_str2 = '{"mobilephone": "${not_existed_tel}", "pwd":"123456"}'

# 第一个参数为模式字符串:${not_existed_tel}
# 第二个参数为新的字符串:18845820369
# 第三个参数为原始的字符串
# sub 方法如果能匹配上, 那么会返回替换之后的字符串
# sub 方法如果匹配不上, 那么直接返回原始的字符串

# 使用 search 方法先搜索,搜索到再用 sub 方法进行替换;count=0 默认匹配一次
if re.search(r"\${not_existed_tel}", src_str2):     # 先搜索
    res2 = re.sub(r"\${not_existed_tel}", "18845820369", src_str2)  # 在替换
    print(res2)
else:
    print("无法匹配原始字符串")
# 替换后结果:{"mobilephone": "18845820369", "pwd":"123456"}

# 使用正则匹配:\w+ 单词字符[a-za-z0-9_],+ 多次匹配;通过正则在原始字符串中匹配
if re.search(r"\${\w+}", src_str2):     # 先搜索
    res2 = re.sub(r"\${not_existed_tel}", "18845820369", src_str2)  # 在替换
    print(res2)
else:
    print("无法匹配原始字符串")
# 替换后结果:{"mobilephone": "18845820369", "pwd":"123456"}

 

  re 模块基本参数化封装

 

import re

from scripts.handle_mysql import handlemysql        # 数据库封装
from scripts.handle_config import handleconfig      # 配置文件封装
from scripts.constants import configs_user_accouts_dir      # 要读取的配置文件路径


class context:
    """
    处理上下文参数化
    """
    not_existed_tel_pattern = r'\${not_existed_tel}'
    invest_user_tel_pattern = r'\${invest_user_tel}'  # 配置${invest_user_tel} 的正则表达式
    invest_user_pwd_pattern = r'\${invest_user_pwd}'  # 配置${invest_user_pwd} 的正则表达式

    handle_config = handleconfig(filename=configs_user_accouts_dir)     # 配置文件

    @classmethod
    def not_existed_tel_replace(cls, data):
        """
        参数化未注册手机号(调用随机生成手机号方法)----常用替换方法以下面的方式
        :param data: 发起请求时传入的data
        :return: 参数化后data 或 匹配不上不需要参数化的原始data
        """
        if re.search(cls.not_existed_tel_pattern, data):          # 使用search方法在用例中匹配,匹配不上不需要替换,返回原始字符串
            do_mysql = handlemysql()    # 创建会话
            data = re.sub(cls.not_existed_tel_pattern,            # 使用sub方法替换
                          do_mysql.create_not_existed_mobile(),   # 随机生成一个在数据库不存在的手机号
                          data)
            do_mysql.close()            # 关闭会话
        return data

    @classmethod
    def invest_user_tel_replace(cls, data):
        """
        参数化已注册的手机号(配置文件中获取已注册手机号码)
        :param data: 发起请求时传入的data
        :return: 参数化后data 或 匹配不上不需要参数化的原始data
        """
        if re.search(cls.invest_user_tel_pattern, data):            # 使用search方法在用例中匹配,匹配不上不需要替换,返回原始字符串
            invest_user_tel = cls.handle_config.get_value("invest_user", "mobilephone")     # 配置文件中获取手机号码
            data = re.sub(cls.invest_user_tel_pattern, invest_user_tel, data)               # 使用sub方法替换
        return data

    @classmethod
    def invest_user_pwd_replace(cls, data):
        """
        参数化已注册的密码(配置文件中账号的密码)
        :param data: 发起请求时传入的data
        :return: 参数化后data 或 匹配不上不需要参数化的原始data
        """
        if re.search(cls.invest_user_pwd_pattern, data):            # 使用search方法在用例中匹配,匹配不上不需要替换,返回原始字符串
            invest_user_pwd = cls.handle_config.get_value("invest_user", "pwd")     # 配置文件中获取密码
            data = re.sub(cls.invest_user_pwd_pattern, invest_user_pwd, data)       # 使用sub方法替换
        return data

    @classmethod
    def register_parameterization(cls, data):
        """
        对某个接口批量参数化
        :param data: 发起请求时传入的data
        :return:  参数化后data 或 匹配不上不需要参数化的原始data
        """
        data = cls.not_existed_tel_replace(data)  # 参数化未注册的手机号
        data = cls.invest_user_tel_replace(data)  # 参数化已注册的手机号
        data = cls.invest_user_pwd_replace(data)  # 再参数化已注册的用户密码
        return data


if __name__ == '__main__':
    one_str = '{"mobilephone": "${not_existed_tel}", "pwd":"${invest_user_pwd}", "regname": "keyou"}'
    two_str = '{"mobilephone": "${not_existed_tel}", "pwd": ""}'
    three_str = '{"mobilephone": "", "pwd": "123456"}'          # 不需要参数化,返回原始data
    six_str = '{"mobilephone": "${invest_user_tel}", "pwd": "123456", "regname": "keyou"}'

    # 参数化
    print(context.register_parameterization(one_str))
    print(context.register_parameterization(two_str))
    print(context.register_parameterization(three_str))
    print(context.register_parameterization(six_str))

    # 结果
    # {"mobilephone": "13218495762", "pwd":"123456", "regname": "keyou"}
    # {"mobilephone": "13278293460", "pwd": ""}
    # {"mobilephone": "", "pwd": "123456"}
    # {"mobilephone": "13226834715", "pwd": "123456", "regname": "keyou"}

 

 

*******请大家尊重原创,如要转载,请注明出处:转载自:   谢谢!!*******