当前位置:网站首页>pytest+request+allure+excel接口自动化搭建 从0到1【二 读取Excel Case信息】
pytest+request+allure+excel接口自动化搭建 从0到1【二 读取Excel Case信息】
2022-07-15 09:07:00 【Song_Lun】
目前读取Excle数据有2种方式xrld和pandas。pandas读取数据默认过滤标题,代码也很简洁。最主要的是读取的速度比xrld要快。
一、使用xlrd读取数据
1.1判断文件格式是否是xlrd开头
依赖包:xlrd == 1.2.0
安装依赖包:pip3 install xlrd == 1.2.0
import xlrd
def read_exce(rule=".xlsx"): #判断文件是否以
try:
filename = "../casefile/case.xlsx" #文件地址
if filename.endswith(rule):
wb = xlrd.open_workbook(filename)
ws = wb.sheet_by_name('Esat') #sheet名称
except Exception as e:
errorMsg = str(e)
log.error("文件格式异常:%s" % errorMsg)
1.2读取数据返回List
all_cases = [] #定义一个list数组 用来储存返回数据
for r in range(1, ws.nrows):
case_data = {
}
for c in range(ws.ncols):
data_value = ws.cell_value(r, c)
case_data[ws.cell_value(0, c)] = data_value
# all_cases[ws.cell_value(r,0)]=case_data
# 将返回的dict装入list
print(type(all_cases))
all_cases.append(case_data)
return all_cases
附上完整代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/7/19 3:59 下午
# @Name : peilun
# @File : readexcel.py
# @Software: PyCharm
import os
import xlrd
from comment.log import *
''' 读取Excel 测试用例 并将用例dict组合成list '''
def read_exce(rule=".xlsx"):
try:
filename = "../casefile/case.xlsx"
if filename.endswith(rule):
wb = xlrd.open_workbook(filename)
ws = wb.sheet_by_name('Esat')
all_cases = []
for r in range(1, ws.nrows):
case_data = {
}
for c in range(ws.ncols):
data_value = ws.cell_value(r, c)
case_data[ws.cell_value(0, c)] = data_value
# all_cases[ws.cell_value(r,0)]=case_data
# 将返回的dict装入list
print(all_cases)
all_cases.append(case_data)
return all_cases
except Exception as e:
errorMsg = str(e)
log.error("文件格式异常:%s" % errorMsg)
case = read_exce()
print(type(case))

二、使用pandas读取数据
依赖包:pytest == 6.1.0
安装依赖包:pip3 install pytest == 6.1.0 \ pandas == 1.3.2
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/6/28 2:38 下午
# @Name : peilun
# @Software: PyCharm
import pandas as pd
import pytest
''' :pandas读取excel测试数据 '''
def read_exce(rule=".xlsx"):
try:
filename = "../casefile/case.xlsx"
if filename.endswith(rule):
data = pd.read_excel(filename,sheet_name='Esat') #文件地址和 sheet名称
datavalues = data.to_dict('records') # 将每行接口数据转换为字典 to_dict('records')格式固定
print(datavalues)
return datavalues
else:
pass
except Exception as e:
errorMsg = str(e)
print("文件格式异常:%s" % errorMsg)
read_exce()
read_excel方法说明
pd.read_excel(filename, sheet_name=0, header=0, skiprows=1, keep_default_na=False )

| filename | 文件地址 |
|---|---|
| sheet_name | 返回指定sheet名称 |
| header | 指定表头,也可用列表指定多行 |
| usecols | 读取指定的列数据 |
| skiprows | 跳过第2行,为了是过滤掉列表头 |
| keep_default_na | False 避免出现 空数据变成 None |
完整代码 包含(钉钉/飞书)推送/报警:去下载
结合 Docker+Jenkins自动化环境部署 即可完成完美的线上服务监控 (还在等什么?快去试试吧!!!)
小白即可搭建自动化环境
边栏推荐
- Week 4 – Linear Text Classification
- Application scheme for remote monitoring of Wutong Bolian photovoltaic power station
- How to realize the association between interfaces in JMeter?
- Proxmox ve 7.2 esxi ova import
- Express keyword search commodity API interface (item_search search search aliexpress commodity interface by keyword)
- Proxmox ve 7.2 importing virtual machines from CT templates
- 企业命名规范
- 内核的结构和设计
- R language basic function assign function
- Matlab微分方程的求解
猜你喜欢

C语言中的关键字struct、union、enum、typedef

Hcip seventh day notes

【无标题】

Solution of MATLAB differential equation

基于matab GUI的图形处理火焰检测系统

1688 API interface for all goods in the store (API interface for querying all goods in the whole store)

Proxmox ve 7.2 creating standard virtual machines

idea 配置npm自启动后跳转到0.0.0.0的问题解决

1688店铺所有商品API接口(整店所有商品查询API接口)

Target detection -- detailed explanation of Siou principle and code implementation of border regression loss function
随机推荐
基于ycbcr的图像火灾检测系统 基于matkab的GUI系统
微信公众号-服务器配置(token验证)
Flame detection system of graphic processing based on MATAB GUI
hyper子查询优雅实现join查询
淘宝app商品详情API接口(商品详情描述信息查询接口)
淘宝官方商品、交易、订单、物流接口列表(淘宝oAuth2.0接口)
【C语言入门】----指针(初阶)
【无标题】
C#中的Explicit和Implicit了解一下吧
Software testing interface testing practice wechat official account platform postman+newman+jenkins realizes automatic report generation and continuous integration
.NET Core 使用 ImageSharp 生成图片
【安全狗】微软7月多个漏洞更新解决
Express keyword search commodity API interface (item_search search search aliexpress commodity interface by keyword)
API interface for all products in pinduoduo store (whole store product list query interface)
《领域驱动设计之php实现》- 应用服务【转载】
Introduction and Simulation of string function
苏宁商品详情API接口(商品详情页数据接口)
Format transformation commonly used by grafana
Byte encountered a wave of resignation!
Proxmox ve 6 updated to 7