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

  自动化测试用例,如果一百个接口要在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}", "")) # 结果:{"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}", "", 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}", "", 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"}

*******请大家尊重原创,如要转载,请注明出处:转载自:https://www.cnblogs.com/shouhu/   谢谢!!******* 

使用 Python 中 re 模块对测试用例参数化,进行搜索 search、替换 sub的更多相关文章

  1. Python中optionParser模块的使用方法[转]

    本文以实例形式较为详尽的讲述了Python中optionParser模块的使用方法,对于深入学习Python有很好的借鉴价值.分享给大家供大家参考之用.具体分析如下: 一般来说,Python中有两个内 ...

  2. python中threading模块详解(一)

    python中threading模块详解(一) 来源 http://blog.chinaunix.net/uid-27571599-id-3484048.html threading提供了一个比thr ...

  3. 【转】关于python中re模块split方法的使用

    注:最近在研究文本处理,需要用到正则切割文本,所以收索到了这篇文章,很有用,谢谢原作者. 原址:http://blog.sciencenet.cn/blog-314114-775285.html 关于 ...

  4. Python中的模块介绍和使用

    在Python中有一个概念叫做模块(module),这个和C语言中的头文件以及Java中的包很类似,比如在Python中要调用sqrt函数,必须用import关键字引入math这个模块,下面就来了解一 ...

  5. python中导入模块的本质, 无法导入手写模块的解决办法

    最近身边一些朋友发生在项目当中编写自己模块,导入的时候无法导入的问题. 下面我来分享一下关于python中导入模块的一些基本知识. 1 导入模块时寻找路径 在每一个运行的python程序当中,都维护了 ...

  6. Python中time模块详解

    Python中time模块详解 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time模块. ...

  7. Python中collections模块

    目录 Python中collections模块 Counter defaultdict OrderedDict namedtuple deque ChainMap Python中collections ...

  8. Python中pathlib模块

    Python中pathlib模块 Path.cwd():返回当前目录的路径 Path.home():返回当前用户的家目录 Path.stat():返回此路径信息 Path.touch():创建文件 P ...

  9. Python 中包/模块的 `import` 操作

    版权声明:博客为作者原创,允许转载,但必须注明原文地址: https://www.cnblogs.com/byronxie/p/10745292.html 用实例来说明 import 的作用吧. 创建 ...

随机推荐

  1. web移动开发小贴士

    1.判断手机类型 var u = navigator.userAgent; || u.indexOf(; //android var isiOS = !!u.match(/\(i[^;]+;( U;) ...

  2. 模板—tarjan求割边

    int dfn[MAXN],low[MAXN],cnt; void tarjan(int x,int edg) { low[x]=dfn[x]=++cnt; for(int i=f(x);i;i=n( ...

  3. css white-space属性

    css white-space属性 规定段落中的文本不进行换行

  4. 实现菜单底部线条沿着 X 轴的值缩放转换scaleX

    效果: 代码: a{padding: 10px 10px; position: relative;} a:before{content: ''; width: 100%; height: 3px; b ...

  5. saltStack 配置管理(也就是替换文件)

    目录  /srv/salt/base下面新建一个文件dns.sls /opt/resolv.conf_bak:     #这个是文件替换的位置,也就说替换到远程文件的/opt/resolv.conf_ ...

  6. iptables禁止代理端口

    #iptables -A INPUT -p tcp --dport 3128 -j REJECT

  7. tensorflow入门——5tensorflow安装

    你将把你学到的神经网络的知识,借助 TensorFlow ,一个 Google 开源的深度学习框架,应用在真实的数据集中. 你将使用 TensorFlow 来辨别 notMNIST 数据集.它是一个由 ...

  8. Python:pip 和pip3的区别

    前言 装完python3后发现库里面既有pip也有pip3,不知道它们的区别,因此特意去了解了一下. 解释 先搜索了一下看到了如下的解释, 安装了python3之后,库里面既会有pip3也会有pip ...

  9. 什么是HOOK技术

    https://zhidao.baidu.com/question/50557962.html HOOK技术是Windows消息处理机制的一个平台,应用程序可以在上面设置子程序以监视指定窗口的某种消息 ...

  10. OP_REQUIRES failed at conv_ops.cc:386 : Resource exhausted: OOM when allocating tensor with shape..

    tensorflow-gpu验证准确率是报错如上: 解决办法: 1. 加入os.environ['CUDA_VISIBLE_DEVICES']='2' 强制使用CPU验证-----慢 2.'batch ...