def my_requests(self, url, data, method): method = method.upper() #统一请求方法大写 data = self.data_to_dic(data) try: if method == 'GET': res = requests.get(url, params=data).text elif method == 'POST': res = requests.post(url, data).text else: atp_log.warning('请求方式暂不支持') res = '请求方式不支持' except Exception as e: atp_log.error('接口不通url %s\n 错误信息:%s' % e) res = '接口不通' return res
def gettoken(): filepath = r'C:\\Users\\cherrylixh\\PycharmProjects\\ATP\\conf\\2.yaml' f = open(filepath) res = yaml.load(f) req = res[0] host = req['host'] url = req['url'] data = req['data'] try: rew = requests.get(host + url, params=data) token = rew.json()['result']['token'] atp_log.info('gettoken:%s' % token) return token except Exception as e: atp_log.error('获取token失败')
def my_request(self, url, method, data): method = method.upper() # 将请求类型转换成大写 data = self.dataToDict(data) # 数据拆分为map try: if method == 'POST': res = requests.post(url, data).text # 发送POST请求并获取文本信息 elif method == 'GET': res = requests.get(url, params=data).text # 发送GET请求并获取文本信息 else: atp_log.warning('该请求方式暂不支持。。') res = '该请求方式暂不支持。。' except Exception as e: msg = '【%s】接口调用失败,%s' % (url, e) atp_log.error(msg) res = msg return res
def get_case(self, file_path): cases = [] #定义一个列表存放所有的cases if file_path.endswith('.xls') or file_path.endswith('.xlsx'): try: book = xlrd.open_workbook(file_path) sheet = book.sheet_by_index(0) for i in range(1, sheet.nrows): row_data = sheet.row_values(i) #获取的每一行数据存到列表row_data cases.append(row_data[4:9]) #追加到cases空列表 atp_log.info('共读取%s条用例' % (len(cases))) self.file_path = file_path #因为该函数已经传了参数路径,为方便write_excel引用,在此实例化 except Exception as e: atp_log.error('[%s]用例获取失败,错误信息:%s' % (file_path, e)) else: atp_log.error('用例文件不合法,%s' % file_path) return cases
def my_request(self, url, method, data): # 参数url,method,data method = method.upper() # 传入参数有可能会有大小写,统一转成大写字母 data = self.dataToDict(data) # 将获取的数据做处理,转成字典 try: if method == 'POST': # 判断method的类型 res = requests.post(url, data).text # 发送请求并转换成字典 elif method == 'GET': res = requests.get(url, params=data).text else: atp_log.warning('该请求方式暂不支持。。') # 调用日志文件 res = '该请求方式暂不支持。。' except Exception as e: msg = '【%s】接口调用失败,%s' % (url, e) atp_log.error(msg) res = msg return res # 返回请求返回的内容
def get_case(self, file_path): cases = [] # 存放所有的case if file_path.endswith('.xls') or file_path.endswith('.xlsx'): try: book = xlrd.open_workbook(file_path) # 读取指定excel,获取book对象 sheet = book.sheet_by_index(0) # 获取第一页 # 循环所有行,提取 4<= 列下标 <8 之间的列的数据 for i in range(1, sheet.nrows): row_data = sheet.row_values(i) # 获取当前行的所有数据 cases.append(row_data[4:8]) # 截取指定部分的数据存入到列表 atp_log.info('共读取%s条用例' % (len(cases))) self.file_path = file_path # 绑定文件地址到对象file_path属性上 except Exception as e: atp_log.error('【%s】用例获取失败,错误信息:%s' % (file_path, e)) else: atp_log.error('用例文件不合法的,%s' % file_path) return cases
def get_case(self, file_path): cases = [] # 存放所有的case if file_path.endswith('.xls') or file_path.endswith( '.xlsx'): # 判断文件是否为excel文件 try: # book = xlrd.open_workbook(file_path) # 打开excel sheet = book.sheet_by_index(0) # 获取sheet页 for i in range(1, sheet.nrows): # 循环每一行 row_data = sheet.row_values(i) # 获取每一行数据 cases.append(row_data[3:7]) # 将第5-8列的数据添加到case中 atp_log.info('共读取%s条用例' % (len(cases))) # 一共有多少条测试用例 self.file_path = file_path # 实例化file_path except Exception as e: atp_log.error('【%s】用例获取失败,错误信息:%s' % (file_path, e)) else: # 如果文件不是excel,提示 atp_log.error('用例文件不合法的,%s' % file_path) return cases # 返回case
def get_cases(self, filepath): cases = [] #保存用例 print('filepath%s' % filepath) if filepath.endswith('.xls') or filepath.endswith('xlsx'): try: book = xlrd.open_workbook(filepath) #打开文件 sheet = book.sheet_by_index(0) #获取第一个sheet表 nrows = sheet.nrows #获取表格行数 for i in range(1, nrows): #从第2行循环读取用例 row_data = sheet.row_values(i) #获取每行的数据,返回一个list cases.append( row_data[4:9]) #获取4-9列的数据放入cases:url,methos,data,check atp_log.info('共有%s条用例' % len(cases)) self.filepath = filepath print('cases:%s' % cases) except Exception as e: atp_log.error('文件打开失败:%s' % e) else: atp_log.error('用例格式不合法,需要xls或xksx:%s' % filepath) return cases
def requestTest(self, *args, **kwargs): detail = kwargs.get('detail', 'no detail') self._testMethodDoc = detail host = kwargs.get('host') url = kwargs.get('url') url = parse.urljoin(host, url) method = kwargs.get('method', 'get') data = kwargs.get('data', {}) header = kwargs.get('header', {}) cookie = kwargs.get('Cookie'.lower(), {}) #args是data中需要传入的参数key值,由于kwargs中已经更新了手动加入的参数,现在需要把这个参数放在data字典中,cookie是单独放在header中不能放在body中 if (len(args) > 0): for c in args: atp_log.info('c:%s' % c) if c.lower() == 'cookie': continue #将需要手动传入的数据放在data,key值是传入时的args,value值放在已更新的kwargs对应的key中 data[c] = kwargs[c] atp_log.info('data:%s' % data) check = kwargs.get('check') method = method.lower() try: #根据method判断需要执行哪个命令 if method == 'get': res = requests.get(url, params=data, cookies=cookie, headers=header) else: res = requests.post(url, data, cookies=cookie, headers=header) req = res.text atp_log.info('responde:%s' % req) return res except Exception as e: atp_log.error('requestTest: %s' % e) print('请求接口出错') req = e #断言,yaml中checklist是否包含在返回的结果中 for c in check: self.assertIn(c, req, msg='预期结果不符,预期结果%s,实际结果%s' % (c, req))
def my_request(self, headers, url, method, data): '' u'根据用例发请求' '' #print('method',method) #print('请求数据',data) data = self.dataToDict(data) #调用dataToDict,把data转成字典 print('res=', data) try: if method.upper() == 'POST': #headers = {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'} #res = requests.post(url, headers,data).text res = requests.post(url, headers=eval(headers), data=data.encode()).text #以session方式提交数据 elif method == 'GET': res = requests.get(url).text else: atp_log.warning('该请求方式暂不支持') res = '该请求方式暂不支持' except Exception as e: msg = '【%s】接口调用失败,%s' % (url, e) atp_log.error(msg) res = msg return res
def check(self, res, check): for c in check.split(','): if c not in res: atp_log.error('失败,预期结果:%s,实际结果:%s' % (c, res)) return 'fail' return 'secessced'