python3实现读取Excel进行接口自动化测试

2021/9/14 1:05:04

本文主要是介绍python3实现读取Excel进行接口自动化测试,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

# -*- coding:utf-8 -*-
# auther = 林深见鹿
# excel_controller.py


import xlrd
import xlwt
from CoreMail.Tools import excel_data


'''
xlrd.open_workbook(file_path),打开Excel
openexcel.sheet_by_index(0);索引到sheet页
sheet1.nrows:行数
sheet1.ncols:列数
遍历行数/列数,读取Excel单元格中数据
'''
def OpenExcel():
    openexcel = xlrd.open_workbook(r"C:\\Users\\xckj-XXX\\PycharmProjects\\ZDHYW\\CoreMail\\Tools\\api_all.xls")
    sheet1 = openexcel.sheet_by_index(0)
    print("总行数:",sheet1.nrows)
    print("总列数:",sheet1.ncols)
    excel_list = []
    for i in range(sheet1.nrows):
        if i == 0:
            continue
        data = excel_data.Data()
        data.url = sheet1.row_values(i)[1]
        data.method = sheet1.row_values(i)[2]
        data.params = sheet1.row_values(i)[3]
        data.case_number = sheet1.row_values(i)[0]


        # print(data.url)
        # print(data.method)
        # print(data.params)
        # print(data.api_name)
        # print(data.case_number)
        excel_list.append(data)
    # print(excel_list)
    return excel_list

OpenExcel()

'''
table.write方法无法关联,直接使用即可
writeexcel.add_sheet("sheet1"):创建一个Excel表格,名称为:sheet1
table.write(0,0,"ID"):第一行第一列写入数据
writeexcel.save(file_name_path)保存文件
'''

def WriteExcel():
    writeexcel = xlwt.Workbook(encoding="utf-8")
    table = writeexcel.add_sheet("sheet1")
    table.write(0,0,"ID")
    writeexcel.save(r"C:\\Users\\xckj-XXX\\PycharmProjects\\ZDHYW\\CoreMail\\Tools\\test.xls")
# WriteExcel()
# -*- coding:utf-8 -*-
# auther = 林深见鹿
# date = 20210816

'''
readme:
allow_redirects = False禁止重定向,添加在request后参数内
get请求用params传参
post请求:数据类型form,用data传参
post请求:数据类型json,用json传参
timeout:模拟请求超时时间
通过读取CoreMail项目下Tools中的excel(接口测试用例),执行接口测试
接口参数要用dict类型传参
url,method要用str类型传参
通过读取method,判断请求方式,针对get和post请求方式分别使用不同数据类型传参
'''




import requests
from CoreMail.Tools import excel_controller
import json
import logging


'''
logging相关配置
Log_Format: 配置日志输出格式
Date_Format: 配置输出时间格式
logging.basicConfig: 添加logging相关配置:
level:默认展示最低输出等级;
filename:log文件路径(注释掉即输出在控制台;
filemode:log文件打开方式
'''

Log_Format = "%(asctime)s %(name)s %(levelname)s %(message)s "
Date_Format = '%Y-%m-%d  %H:%M:%S %a'
logging.basicConfig(level=logging.DEBUG,
                    format=Log_Format,
                    datefmt=Date_Format,
                    filename=r"C:\Users\xckj-XXX\PycharmProjects\ZDHYW\CoreMail\Logs\logs.log",
                    filemode='w')
datalist = excel_controller.OpenExcel()

def all_api():

    for i in datalist:
        # print("url:",i.url)
        # print("method:",i.method)
        # print("params:",i.params)
        # print("api_name:",i.api_name)
        # print("case_number:",case_number)
        _url = i.url
        param = i.params
        data = json.loads(param,strict = False)
        api_name = i.api_name
        case_number = i.case_number

        print(i.method)
        method = i.method
        try:
            if method == 'post':
                # print(type(data))
                # print(type(method))
                r = requests.request(method=method,url=_url,json=data)
                print("测试用例编号:",case_number)
                print("API:",_url)
                print('状态码:',r.status_code)
                print("返回报文:",r.text,end='\n')
                result = r.json()
                if result["resultFlag"]== 'SUCCESS':
                    print("测试用例%s执行成功"%(case_number))
                else:
                    print("测试用例%s执行失败"%(case_number))

            elif method == 'get':
                r = requests.request(method=method,url=_url,params=data)
                print("测试用例编号:",case_number)
                print("API:",_url)
                print('状态码:',r.status_code)
                print("返回报文:",r.text,end='\n')
                result = r.json()
                if result["resultFlag"]== 'SUCCESS':
                    print("测试用例%s执行成功"%(case_number))
                else:
                    print("测试用例%s执行失败"%(case_number))

            else:
                print("暂不支持%s类型的请求方式:"%(method))
                print("测试用例编号:",case_number)
        except Exception as error:
            print("!!!---------------error---------------!!!:",error)
            continue
all_api()

 

 
# -*- coding:utf-8 -*-
# auther = 林深见鹿
# date = 20210816
# excel_data.py


class Data:
    url = ''
    # print(url)
    method =''
    # print(method)
    params = {}
    # print(params)
    api_name = ''
    # print(api_name)
    case_number = ''
    # print(case_number)



 

 all_api.xls



这篇关于python3实现读取Excel进行接口自动化测试的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程