1 import jsonpath 2 import os 3 import xlrd 4 5 from config.setting import log 6 7 8 def get_value(dic, key): 9 ''' 10 这个函数是从一个字典里面,根据key获取vlaue 11 :param dic:传一个字典 12 :param key:传一个 13 :return:如果有,返回key取到value,如果key没有,返回空字符串 14 ''' 15 result = jsonpath.jsonpath(dic, '$..%s' % key) 16 if result: 17 return result[0] 18 return '' 19 20 21 class GetTestData: 22 23 @staticmethod 24 def data_for_txt(file_name): 25 log.debug('开始读取参数化文件%s' % file_name) 26 if os.path.exists(file_name): 27 with open(file_name, encoding='utf-8') as fr: 28 data = [] 29 for line in fr: 30 if line.strip(): 31 line_data = line.strip().split(',') 32 data.append(line_data) 33 return data 34 log.error('%s参数化文件不存在' % file_name) 35 raise Exception('%s参数化文件不存在' % file_name) 36 37 @staticmethod 38 def data_for_excel(file_name, sheet_name=None): 39 log.debug('开始读取参数化文件%s' % file_name) 40 if os.path.exists(file_name): 41 data = [] 42 book = xlrd.open_workbook(file_name) 43 if sheet_name: 44 sheet = book.sheet_by_name(sheet_name) 45 else: 46 sheet = book.sheet_by_index(0) 47 for row_num in range(1, sheet.nrows): 48 row_data = sheet.row_values(row_num) 49 data.append(row_data) 50 return data 51 log.error('%s参数化文件不存在' % file_name) 52 raise Exception('%s参数化文件不存在' % file_name) 53 54 @staticmethod 55 def data_for_mysql(sql): 56 pass 57 58 59 class Singleton: 60 instance = None 61 62 def __new__(cls, *args, **kwargs): 63 if cls.instance: 64 return cls.instance 65 cls.instance = super().__new__(cls) # 66 return cls.instance