• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

pytest(11): 全局变量使用:fixtures和cache

武飞扬头像
python开发笔记
帮助1

需求场景:

测试用例间,测试文件间需要共享一些变量,前面用例生成数据供后面用例使用:例如登录后的token;或自定义测试框架中需要用到的一些全局变量

解决:

1.利用fixture特性解决

利用pytest的fixture来解决,例如定义一个session级别的fixture,返回一些数据,在多个用例文件中引用fixture即可。注:对于可变数据类型,在用例中改变其值是即可全局生效

定义:

  1.  
    @pytest.fixture(scope='session')
  2.  
    def run_variables(cmdopt):
  3.  
    """
  4.  
    初始化脚本运行全局参数
  5.  
    """
  6.  
    cmdopts = cmdopt
  7.  
    plugin = cmdopts['cmdplugin']
  8.  
    if str(plugin) == str(None):
  9.  
    client_module = None
  10.  
    handler_module = None
  11.  
    plugin_config = None
  12.  
    else:
  13.  
    client_file = "compare.plugin.{}.src.client".format(plugin)
  14.  
    client_module = importlib.import_module(client_file)
  15.  
    handler_file = "compare.plugin.{}.src.handler".format(plugin)
  16.  
    handler_module = importlib.import_module(handler_file)
  17.  
    ypath = os.path.join(COMPARE_PATH, 'plugin/{}/conf/conf.yml'.format(plugin))
  18.  
    plugin_config = YamlHelper(ypath).data
  19.  
    all_variables = {}
  20.  
    all_variables['cmdopts'] = cmdopts #命令参数字典
  21.  
    all_variables['client_module'] = client_module #客户端模块
  22.  
    all_variables['handler_module'] = handler_module #客户端操作模块
  23.  
    all_variables['plugin_config'] = plugin_config #插件配置
  24.  
    all_variables['current_result'] = {} #当前执行用例行结果
  25.  
    all_variables['run_statistics'] = {'pass': 0, 'fail': 0, 'norun': 0} #当前执行用例数据统计
  26.  
    return all_variables
学新通

使用 

  1.  
    @pytest.mark.parametrize("test_case_id, cust_id,business_type,exec_flag,desc,data", datas)
  2.  
    def test_access_01(client, run_variables, access_variables, access_output_cr, access_output_func,
  3.  
    test_case_id, cust_id, business_type, exec_flag, desc, data):
  4.  
     
  5.  
    handler_module = run_variables['handler_module']
  6.  
    print('=====>data.data_dict:',data.data_dict)
  7.  
    row_result = init_result(access_output_cr.columns, data.data_dict)
  8.  
    run_variables['current_result'] = row_result
  9.  
    if exec_flag:
  10.  
    row_result['result'] = '不执行'
  11.  
    row_result['rerun'] = exec_flag
  12.  
    return

2.cash方法解决

cache 是一个可以在测试会话之间保持状态的缓存对象。

  1.  
    @pytest.fixture
  2.  
    def cache(request):
  3.  
    """
  4.  
    Return a cache object that can persist state between testing sessions.
  5.  
    cache.get(key, default)
  6.  
    cache.set(key, value)
  7.  
    Keys must be a ``/`` separated value, where the first part is usually the
  8.  
    name of your plugin or application to avoid clashes with other cache users.
  9.  
    Values can be any object handled by the json stdlib module.
  10.  
    """
  11.  
    return request.config.cache

cache是Cache类的一个实例对象

  • mkdir 创建一个文件夹

  • set(key: str, value: object)  设置一个cache值

  • get(key: str, default)   得到key对应的值

例1:当前置操作生成一个id值,在用例中获取这个id

  1.  
    import pytest
  2.  
     
  3.  
    @pytest.fixture()
  4.  
    def create_token(cache):
  5.  
    """取值生成一个id"""
  6.  
    token = 'Dfgczsxghbvljzxghlvzhdvhlxckvuhbxchb'
  7.  
    cache.set("token ", token )
  8.  
    yield token
  9.  
     
  10.  
    def test_1(cache, create_token):
  11.  
    # 方式1:cache获取
  12.  
    token= cache.get("id", None)
  13.  
    print("获取到的id: {}".format(token))
  14.  
     
  15.  
     
  16.  
    # 方式2:直接通过create_token获取返回值
  17.  
    print("create_id fixture return: {}".format(create_token))
学新通

例2:执行用例后生成一个user_data,后置操作需要清理数据

  1.  
    import pytest
  2.  
     
  3.  
    @pytest.fixture()
  4.  
    def delete_user_data(cache):
  5.  
    """后置处理"""
  6.  
    yield
  7.  
    # 先获取用例执行后得到的user_data
  8.  
    user_data= cache.set("user_data", None)
  9.  
    print("后置处理得到值: {}".format(user_data))
  10.  
     
  11.  
    def test_2(cache, delete_user_data):
  12.  
    # 执行用例后生成user_data
  13.  
    user_data= "token5d4vz5vz"
  14.  
    cache.set("user_data", user_data)

.pytest_cache 缓存文件

在pycharm中右键执行,不会生成.pytest_cache 缓存文件。
使用 pytest 命令行执行,会在项目目录生成.pytest_cache 缓存文件

学新通

  1.  
    @pytest.fixture
  2.  
    def cache(request):
  3.  
    return request.config.cache
  1.  
    @pytest.fixture(scope='function')
  2.  
    @pytest.mark.parametrize('data1', datas)
  3.  
    def eee(data1,cache):
  4.  
    cache.set('user_data', 'izusdygfuoysagdyufgau')

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfikifk
系列文章
更多 icon
同类精品
更多 icon
继续加载