zoukankan      html  css  js  c++  java
  • 第一个接口自动化框架atp

    接口自动化的测试框架。
    框架,一堆工具的集合。
    1.获取用例
    2.执行用例
    3.校验结果
    4.产生报告

    框架

    步骤:
    1.读excel获取到所有的case 用xlrd模块
    2.根据测试用例调用接口 requests
    3.校验结果 if
    4.结果写入excel xlutils
    5.生成报告,发送邮件 yagmail

    写代码:

    方法都放在工具类里,即tools.py
    获取用例
    读取excel中的所有用例的方法
    lib中的tools.py
     1 import xlrd
     2 from config.setting import log
     3 
     4 def readExcel(file_path):
     5     try:
     6         book  = xlrd.open_workbook(file_path)
     7     except Exception as e:
     8         log.error('打开用例出错,文件名是%s'%file_path)
     9         return []#返回空的list,没有数据
    10     else:
    11         all_case = [] #存放所有的用例
    12         sheet = book.sheet_by_index(0)#获取sheet页
    13         for i in range(1,sheet.nrows):
    14             #1代表从第二行开始,第一行是标题
    15             #sheet.nrows是指一共有多少行数据
    16             row_data = sheet.row_values(i)#获得一整行数据
    17             all_case.append(row_data[4:8])#所有用例
    18             # url, method, data, check = row_data[4],row_data[5],row_data[6],row_data[7]
    19             #url,method,data,check这四列
    20         return all_case
    21 res = readExcel(r'C:UsersMezhouPycharmProjectsuntitledday10atpcases测试用例.xlsx')
    22 print(res)

    结果:


    根据测试用例调用接口

    检验接口返回是否是json串,自己封装my_request.py

     1 import requests
     2 from config.setting import log
     3 def post(url,data,header=None,cookies=None,is_json=False):
     4     try:
     5         if is_json:
     6             res = requests.post(url,json=data,headers=header,cookies=cookies).text
     7         else:
     8             res =requests.post(url,data=data,headers=header,cookies=cookies).text
     9     except Exception  as e:
    10         log.error('接口请求出错,%s'%e)
    11         res = str(e)
    12     return res
    13 def get(url,data,header=None,cookies=None):
    14     try:
    15         res = requests.get(url,params=data,headers=header,cookies=cookies).text
    16     except Exception as e:
    17         log.error('接口请求出错,%s'%e)
    18         res = str(e)
    19     return res
    校验结果
     1 #返回结果进行替换,将冒号引号空格等替换成等号
     2     '''{
     3     "code": 100,
     4     "name: "zjr"}'''
     5  #替换成   code=100  name=zjr 这种格式
     6 #res是返回的结果,check是校验的数据
     7 def check_res(res:str,check:str):
     8     new_res = res.replace('": "', '=').replace('": ', '=')
     9     for c in check.split(','):
    10         if c not in new_res:
    11             return '失败'
    12         return '通过'

    将结果写入excel

    1 def write_res(file_name,res_list):#入参是文件名和返回结果
    2     book = xlrd.open_workbook(file_name)
    3     new_book = copy(book)#复制一个book
    4     sheet = new_book.get_sheet(0)
    5     for row,data in enumerate(res_list,1):#enumerate枚举,从第2行开始,第一行是标题
    6         res,status = data
    7         sheet.write(row,8,res)#写入返回结果和运行状态
    8         sheet.write(row,9,status)
    9     new_book.save(file_name)

     将表格中用户名密码转换成字典

    1 def str_to_dict(s:str,seq='&'):
    2     #username=niuhanyang&password=123456
    3     #['username=niuhanyang,password=123456']
    4     d = {}
    5     if s.strip():#非空即真,若为空直接返回一个空字典
    6         for tmp in s.split(seq):
    7             k,v = tmp.split('=')#username,niuhanyang
    8             d[k]=v
    9     return d

    运行用例

     1 def run_case(all_case):
     2     all_res=[]#存放所有用例结果
     3     for case in all_case:
     4         url,method,data,check = case
     5         data = str_to_dict(data)#把请求参数转成字典
     6         if method.upper()=='POST':
     7             res = my_request.post(url,data)
     8         else:
     9             res = my_request.get(url,data)
    10         status = check_res(res,check)
    11         all_res.append([res,status])
    12     return all_res
     


  • 相关阅读:
    测试开发进阶之路,2020 我们砥砺同行!
    【上海/北京/杭州】七牛云工程效率部直聘
    测试开发基础|一文搞定计算机网络(一)
    点点点工程师真的要被淘汰吗?
    电商性能测试实战 | JMeter 插件 Ultimate Thread Group 完成梯度递增场景的压测
    将H264与AAC打包Ipad可播放的TS流的总结
    Ubuntu阿里云搭建Mono.net环境
    利用正则表达式排除特定字符串
    你敢不敢不要让我这么忙
    离我而去的附录H
  • 原文地址:https://www.cnblogs.com/Mezhou/p/9882592.html
Copyright © 2011-2022 走看看