pytest(11): 全局变量使用:fixtures和cache
需求场景:
测试用例间,测试文件间需要共享一些变量,前面用例生成数据供后面用例使用:例如登录后的token;或自定义测试框架中需要用到的一些全局变量
解决:
1.利用fixture特性解决
利用pytest的fixture来解决,例如定义一个session级别的fixture,返回一些数据,在多个用例文件中引用fixture即可。注:对于可变数据类型,在用例中改变其值是即可全局生效
定义:
-
-
def run_variables(cmdopt):
-
"""
-
初始化脚本运行全局参数
-
"""
-
cmdopts = cmdopt
-
plugin = cmdopts['cmdplugin']
-
if str(plugin) == str(None):
-
client_module = None
-
handler_module = None
-
plugin_config = None
-
else:
-
client_file = "compare.plugin.{}.src.client".format(plugin)
-
client_module = importlib.import_module(client_file)
-
handler_file = "compare.plugin.{}.src.handler".format(plugin)
-
handler_module = importlib.import_module(handler_file)
-
ypath = os.path.join(COMPARE_PATH, 'plugin/{}/conf/conf.yml'.format(plugin))
-
plugin_config = YamlHelper(ypath).data
-
all_variables = {}
-
all_variables['cmdopts'] = cmdopts #命令参数字典
-
all_variables['client_module'] = client_module #客户端模块
-
all_variables['handler_module'] = handler_module #客户端操作模块
-
all_variables['plugin_config'] = plugin_config #插件配置
-
all_variables['current_result'] = {} #当前执行用例行结果
-
all_variables['run_statistics'] = {'pass': 0, 'fail': 0, 'norun': 0} #当前执行用例数据统计
-
return all_variables
使用
-
-
def test_access_01(client, run_variables, access_variables, access_output_cr, access_output_func,
-
test_case_id, cust_id, business_type, exec_flag, desc, data):
-
-
handler_module = run_variables['handler_module']
-
print('=====>data.data_dict:',data.data_dict)
-
row_result = init_result(access_output_cr.columns, data.data_dict)
-
run_variables['current_result'] = row_result
-
if exec_flag:
-
row_result['result'] = '不执行'
-
row_result['rerun'] = exec_flag
-
return
2.cash方法解决
cache 是一个可以在测试会话之间保持状态的缓存对象。
-
-
def cache(request):
-
"""
-
Return a cache object that can persist state between testing sessions.
-
cache.get(key, default)
-
cache.set(key, value)
-
Keys must be a ``/`` separated value, where the first part is usually the
-
name of your plugin or application to avoid clashes with other cache users.
-
Values can be any object handled by the json stdlib module.
-
"""
-
return request.config.cache
cache是Cache类的一个实例对象
-
mkdir 创建一个文件夹
-
set(key: str, value: object) 设置一个cache值
-
get(key: str, default) 得到key对应的值
例1:当前置操作生成一个id值,在用例中获取这个id
-
import pytest
-
-
-
def create_token(cache):
-
"""取值生成一个id"""
-
token = 'Dfgczsxghbvljzxghlvzhdvhlxckvuhbxchb'
-
cache.set("token ", token )
-
yield token
-
-
def test_1(cache, create_token):
-
# 方式1:cache获取
-
token= cache.get("id", None)
-
print("获取到的id: {}".format(token))
-
-
-
# 方式2:直接通过create_token获取返回值
-
print("create_id fixture return: {}".format(create_token))
例2:执行用例后生成一个user_data,后置操作需要清理数据
-
import pytest
-
-
-
def delete_user_data(cache):
-
"""后置处理"""
-
yield
-
# 先获取用例执行后得到的user_data
-
user_data= cache.set("user_data", None)
-
print("后置处理得到值: {}".format(user_data))
-
-
def test_2(cache, delete_user_data):
-
# 执行用例后生成user_data
-
user_data= "token5d4vz5vz"
-
cache.set("user_data", user_data)
.pytest_cache 缓存文件
在pycharm中右键执行,不会生成.pytest_cache 缓存文件。
使用 pytest 命令行执行,会在项目目录生成.pytest_cache 缓存文件
-
-
def cache(request):
-
return request.config.cache
-
-
-
def eee(data1,cache):
-
cache.set('user_data', 'izusdygfuoysagdyufgau')
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfikifk
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01