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
     


  • 相关阅读:
    (转)【web前端培训之前后端的配合(中)】继续昨日的故事
    ural(Timus) 1136. Parliament
    scau Josephus Problem
    ACMICPC Live Archive 6204 Poker End Games
    uva 10391 Compound Words
    ACMICPC Live Archive 3222 Joke with Turtles
    uva 10132 File Fragmentation
    uva 270 Lining Up
    【转】各种字符串哈希函数比较
    uva 10905 Children's Game
  • 原文地址:https://www.cnblogs.com/Mezhou/p/9882592.html
Copyright © 2011-2022 走看看