使用excel管理用例

①、读取excel的用例数据,每一类参数保存在list中返回;
②、那么接下来使用unitest编写用例时,可以去调用这个函数。使用里面的数据;

个人认为好处是,大部分人还是习惯excel的编写用例习惯,将实际用例与代码分离,有助于分工和维护

一、在excel中写好用例后,在python(unitest)中调用
 #写文件
import xlwt;
#读文件
import xlrd;
#若要对excel文件进行修改必须!要引入这个工具模块,其中含有复制和修改功能---------注意引入方式
import xlutils;
from xltuils.copy import copy; import unittest;
import requests;
#使用写文件模式打开文件
open_workbook=xlrd.open_workbook(r'D:\Users\4399-3046\Desktop\test.xls');
#最终返回的是文件类型
print(open_workbook);
#打印该文件下,对应数据表的行数
#print(open_workbook.sheet_by_name('mytest').nrows); #打印该文件下,对应数据表的列数
#print(open_workbook.sheet_by_name('mytest').ncols);
#for item_rows in range(1,open_workbook.sheet_by_name('mytest').nrows):
#for item_cols in range(1,open_workbook.sheet_by_name('mytest').ncols):
#print(open_workbook.sheet_by_name('mytest').cell(item_rows,item_cols)); #循环打印出所有数据,并分别保存在list中
#编号
listkey=[];
#测试地址
listurl=[];
#提交方式
listtype=[];
#参数1
listcanshu1=[];
#参数2
listcanshu2=[];
#预期结果
listyuqi=[];
#实际结果
listresult=[];
#在unitest框架中,通过调用参数,执行用例,将实际结果比对预期结果是否一致,若一致,则通过,否则用例失败,并将实际结果写入excel中(写入代码后文将写到)
for item_row in range(1,open_workbook.sheet_by_name('mytest').nrows):
#获取用例编号
listkey.append(open_workbook.sheet_by_name('mytest').cell(item_row,0).value);
# 获取参数1
listcanshu1.append(open_workbook.sheet_by_name('mytest').cell(item_row, 1).value);
# 获取参数2
listcanshu2.append(open_workbook.sheet_by_name('mytest').cell(item_row, 2).calue);
# 获取预期结果
listyuqi.append(open_workbook.sheet_by_name('mytest').cell(item_row, 3).value);
# 获取实际结果
listresult.append(open_workbook.sheet_by_name('mytest').cell(item_row, 4).value);------------->注意:必须要有【.value】,因为返回的是dict字典,而实际我们只需要其中的具体值 print(listkey);
print(listcanshu1);
print(listcanshu2);
print(listyuqi);
print(listresult);
运行结果:

 2、新建文件,并在新文件中写入数据

  open_workbook=xlwt.Workbook();---定义一个工作簿

 new_sheet=open_workbook.add_sheet('lianxi',cell_overwrite_ok = True);----在工作簿中新增一个工作表,自定义一个工作表名称
注意1:若当前单元格已经有值,无法重复写入覆盖原有,会提示异常,需要在新建表单sheet时,就要指定,可以重复写入【cell_overwrite_ok = True】,默认是false
 new_sheet.write(0,5,'lianxi_test');-----在工作表中写入数据(注意:写入数据只在工作表sheet中生效)---
注意2:如果当前目录下已存在同名的工作簿名称,则会报错
 open_workbook.save(r'D:\Users\Desktop\test01.xls');----保存工作簿(需要指定地址和名称)
3、在已有文件下excel下,写入数据
1#思路:打开文件----复制文件----在文件中写入数据----删除原文件----保存新文件 

#打开当前已经存在的工作簿
open_workbook2=xlrd.open_workbook(r'D:\Users\Desktop\test.xls');
#复制当前工作簿
open_sheet2=copy(open_workbook2);
#打开置顶索引的工作表
open_ws=open_sheet2.get_sheet(3);
#在指定的单元格写入数据
open_ws.write(1,4,'TEST');
#移除原有的文件
os.remove(r'D:\Users\4399-3046\Desktop\test.xls')
#保存新文件
open_sheet2.save(r'D:\Users\4399-3046\Desktop\test.xls'); 运行结果:


4、扩展--结合python-unitest,通常我们需要将执行结果自动写入表格中
5、拓展--将写文件和读文件,分别封装为公共方法调用(已完成)
 #by zhonghuihong
import xlwt;
import xlrd;
from xlutils.copy import copy;
import os;
#此接口主要用于excel数据的读取和写入 def get_excel_data(paths,sheetName):
open_workbook=xlrd.open_workbook(paths);
open_sheet=open_workbook.sheet_by_name(sheetName);
rows=open_sheet.nrows;
listkey = [];
listcanshu1 = [];
listcanshu2 = [];
listyuqi = [];
listresult = [];
for item_row in range(1, rows):
listkey.append(open_sheet.cell(item_row, 0).value);
listcanshu1.append(open_sheet.cell(item_row, 1).value);
listcanshu2.append(open_sheet.cell(item_row, 2).value);
listyuqi.append(open_sheet.cell(item_row, 3).value);
listresult.append(open_sheet.cell(item_row, 4).value);
return listkey,listcanshu1,listcanshu2,listyuqi,listresult; def write_excel_data(paths,sheet_id,rows_id,cols_id,values):
open_workbook2 = xlrd.open_workbook(paths);
open_sheet2 = copy(open_workbook2);
open_ws = open_sheet2.get_sheet(sheet_id);
open_ws.write(rows_id,cols_id,values);
os.remove(paths);
open_sheet2.save(paths);
return 'success';




遇到的问题
1、读取的整数变成浮点类型,解决办法

2、脚本中引入copy方法,但提示TypeError: 'module' object is not callable的解决办法:将引入语句写成from xlutils.copy import copy;

3、指定sheet时(ow.get_sheet[0];),提示TypeError: 'method' object is not subscriptable

产生原因:subscriptable的意思是可有下标,错误的原因就是:对象不具有下标,即把不具有下标操作的对象用成了对象[i],比如int对象变量[i]就会报错。仔细检查错误行。

代码最终优化:

*增加try--exception异常捕获

*增加写入输入不能为空

 #写数据(文件薄,表格id,行id,列id,要写的内容)
def write_excel_data(paths,sheet_id,rows_id,cols_id,values):
if(len(values)==0 ):
return '写入数据不能为空';
else: try:
#如果写入位置已经有值, 会直接使用新内容覆盖
open_workbook2 = xlrd.open_workbook(paths);
open_sheet2 = copy(open_workbook2);
open_ws = open_sheet2.get_sheet(sheet_id);
open_ws.write(rows_id, cols_id, values);
os.remove(paths);
open_sheet2.save(paths); return 'success';
except Exception as e:
return e; #读取excel_data(文件薄地址,表格名)
#通过循环读出指定表格中的所有数据,以列表格式输出
def get_excel_data(paths,sheetName):
open_workbook = xlrd.open_workbook(paths);
open_sheet = open_workbook.sheet_by_name(sheetName); if open_sheet.nrows <= 1:
return '文件总行数小于1,请确认'; else:
res = []
j = 1
for i in list(range(open_sheet.nrows-1)):
s = {};
#行编号
s['rowNum'] = i+1;
values = open_sheet.row_values(j)
for x in list(range(open_sheet.ncols)):
keys=open_sheet.row_values(0);
s[keys[x]] = values[x];
res.append(s);
j += 1;
return res;
												

python---读取/写入excel用例数据的更多相关文章

  1. 第十课: - 读取/写入Excel/Json格式数据

    第 10 课 从DataFrame到Excel 从Excel到DataFrame 从DataFrame到JSON 从JSON到DataFrame In [1]: import pandas as pd ...

  2. Python 读取写入配置文件 —— ConfigParser

    Python 读取写入配置文件 —— ConfigParser Python 读取写入配置文件很方便,可使用内置的 configparser 模块:可查看源码,如博主本机地址: “C:/python2 ...

  3. JXL读取写入excel表格数据

    问题描述: 使用java的jxl包创建.写入excel表格数据 问题解决: (1)说明 (2)写入execel数据 注: 以上是写入数据需要调用的函数接口 注: 具体接口调用过程,如上所示 (3)读取 ...

  4. 【Python】如何处理Excel中的数据

    我们平时在做自动化测试的时候,可能会涉及到从表格中去读取或者存储数据,我们除了可以使用openpyxl来操作excel,当然也可以利用pandas来完成,这篇随笔只是我在学习过程中的简单记录,其他的功 ...

  5. 使用C#实现读取/写入Excel表

    C#实现写入Excel表 using System; using System.Reflection; using System.IO; using Microsoft.Office.Interop. ...

  6. python pandas写入excel文件

    pandas读取.写入csv数据非常方便,但是有时希望通过excel画个简单的图表看一下数据质量.变化趋势并保存,这时候csv格式的数据就略显不便,因此尝试直接将数据写入excel文件. pandas ...

  7. POI读取/写入Excel文件

    import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io ...

  8. JXL读取,写入Excel

    JXL读取,写入Excel2003 相关阅读:poi 读写excel2003:http://www.cnblogs.com/gavinYang/p/3576739.htmlpoi 读写excel200 ...

  9. python(写入excel操作-xlwt模块)

    一.安装xlwt模块 pip install xlwt 二.excel写入操作 这种方式只能新增或者覆盖文件写入 import xlwt # 创建一个workbook 设置编码 workbook = ...

随机推荐

  1. FastCGI sent in stderr: "PHP Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '&lt;' not found in

    2018-4-16 17:59:03 星期一 1. 发送带有xml参数的请求时如果是用php curl, 需要将数组形式的post data 用 http_build_query() 转换 2. 接收 ...

  2. docker里面运行jenkins详解

    需求:将jenkins运行在docker中 思路:1.安装docker,并启动docker 服务            2.下载jenkins的docker镜像,然后运行. 前提知识:1.dockde ...

  3. oracle 11.2.0.4 rac 打补丁

    本次安装pus环境是11.2.0.4 rac,打的patch为11.2.0.4.180717 (Includes Database PSU),gi补丁和数据库补丁一起打 安装最新opatch版本 un ...

  4. [PHP]curl上传多文件

    码一下curl上传多文件的行 5.5之前版本的写法 $file = array( 'pic[0]'=>"@E:\\wwwroot\\10003\\temp_56.ini;type=te ...

  5. 练就Java24章真经—你所不知道的工厂方法

    前言 最近一直在Java方向奋斗<终于,我还是下决心学Java后台了>,今天抽空开始学习Java的设计模式了.计划有时间就去学习,你这么有时间,还不来一起上车吗? 之所以要学习Java模式 ...

  6. ios  调整 label 的字体行间距

     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 200) ...

  7. 用echarts写的轨迹图demo

    轨迹图预览: [下载地址]:https://github.com/zhangzn3/trail-graph.git

  8. CentOS 7 部署 Spring Boot

    Spring Boot 内嵌了tomcat .我们可以将Boot打成 jar 包丢到服务器上运行才行. Spring Boot已经帮我们打理好了这一切,如果项目是继承自 spring-boot-sta ...

  9. Layers Of Caffe

    本文试图描述构建一个网络结构的layers,可以用prototxt脚本直接写,也可以用python接口实现. 最简单的神经网络包含但不限于以下四部分: 数据层(Data): Data.ImageDat ...

  10. ERROR 1044 (42000): Access denied for user 'root'@'%' to database 'mysql'

    原因:修改数据库账号时删除了默认的localhost root,  新建了% root 但没有赋予全部权限; 解决方法: 1.关闭数据库# mysqld stop 2.在my.cnf里加入skip-g ...