1、全局变量 global
调用方法:

2、跨文件全局变量 glo_all
1)背景:如何在不同目录不同文件下共用一个变量来提高用例复用性?
2)源码:
# -*- coding: utf-8 -*-
# @Time : 2022/3/22 1:41
# @Author : yanfa
# @File : glo_all.py
# 跨文件全局变量用法
import logging
def _init():#初始化
global _global_dict
logging.info('初始化全局字典')
_global_dict = {}
def set_value(key,value):
""" 定义一个全局变量 """
try:
_global_dict[key] = value
logging.info(f'设置成功 {key}:{_global_dict[key]}')
# logging.info(f'当前字典为{str(_global_dict)}')
return True
except KeyError:
logging.info(f'设置失败 {key}:{_global_dict[key]}')
return False
def get_value(key,defValue=False):
try:
logging.info(f'读取成功 {key}:{_global_dict[key]}')
# logging.info(f'当前字典为{str(_global_dict)}')
return _global_dict[key]
except KeyError:
logging.error('读取' + key + '失败\r\n')
# logging.info(f'当前字典为{str(_global_dict)}')
return defValue
3)注意:需要在test_case目录下初始化文件init.py中新增glo_all._init() ,只能在方法内或者类里面指向,需要有main入口

4)调用方法:
设全局:
glo_all.set_value("glo_pic_title",title)
#支持直接传字符以外的如int类型
调全局:
glo_all.get_value("glo_pic_title")


网友评论