接口自动化(二)--操作Excel获取需要数据

这一部分的内容记述一下对Excel表格的操作,本实战中的测试用例是由Excel来管理的,因此操作Excel是重要的一部分。

再次贴出这张图,所有的测试用例都在这个sheet内,请求数据真实存放在json文件内,表格中的请求数据列放置的是json文件内字典的key值。

操作Excel主要就是取出Excel中的case(即数据),然后执行,重点是怎样取出数据,因为有些数据是不需要的,所以不能用遍历Excel的方法来取数据。

思路一:把列数作为常量,行数作为变量来取想要的数据

思路二:把Excel中不需要的列数据剪切掉,留下需要的数据,然后遍历Excel

这里使用的思路一

下面是excelColNum.py的源码(请根据需要灵活使用,模块作用参考第一篇文章):

 1 class ExcelColNum():
 2     case_id = 0
 3     model = 1
 4     case_name = 2
 5     url = 4
 6     header = 5
 7     run = 6
 8     requestMethord = 7
 9     caseDepend = 8
10     depReKey = 9
11     depKey = 10
12     requestData = 11
13     respectResult = 12
14     returnData = 13
15     realResult = 14
16
17     #获取用例ID
18     def get_caseID(self):
19         return ExcelColNum.case_id
20
21     #获取请求的url
22     def get_url(self):
23         return ExcelColNum.url
24
25     #获取是否运行
26     def get_run(self):
27         return ExcelColNum.run
28
29     #获取请求方式
30     def get_methord(self):
31         return ExcelColNum.requestMethord
32
33     #获取请求数据
34     def get_requestData(self):
35         return ExcelColNum.requestData
36
37     #或取是否携带header
38     def get_haeder(self):
39         return ExcelColNum.header
40
41     #获取被依赖的caseID
42     def getDepCaseID(self):
43         return ExcelColNum.caseDepend
44
45     #获取被依赖的用例的返回数据的key值
46     def getDepReKey(self):
47         return ExcelColNum.depReKey
48
49     #获取依赖的需要传入值的key值
50     def getDepKey(self):
51         return ExcelColNum.depKey
52
53     #获取预期结果
54     def getRespectResult(self):
55         return ExcelColNum.respectResult
56
57     #获取返回数据
58     def getReturnResult(self):
59         return ExcelColNum.returnData
60
61     #实际结果
62     def getRealResult(self):
63         return ExcelColNum.realResult

然后是operateExcel.py的源码:

 1 import xlrd
 2 from xlutils.copy import copy
 3
 4 class operateExcel():
 5     def __init__(self,excelPath=None,sheetName=None):
 6         #容错处理
 7         if excelPath:
 8             self.excelPath = excelPath
 9             self.sheetName = sheetName
10         else:
11             self.excelPath = "******"                #Excel文件的路径
12             self.sheetName = "******"                #sheet的名字
13         self.data = self.openExcel()
14     #打开Excel表格
15     def openExcel(self):
16         file = xlrd.open_workbook(self.excelPath)
17         sheet = file.sheet_by_name(self.sheetName)
18         return sheet
19
20     #获取sheet的行数
21     def getSheetRow(self):
22         rows = self.data.nrows
23         return rows
24
25     #获取sheet的列数
26     def getSheetCol(self):
27         cols = self.data.ncols
28         return cols
29
30     #获取某一个单元格的内容
31     def getCellContent(self,rowNum,colNum):
32         content = self.data.cell_value(rowNum,colNum)
33         return content
34
35     #向excel里追加写入数据
36     def writeValue(self,rowNum,colNum,value):
37         file = xlrd.open_workbook(self.excelPath)
38         workbook = copy(file)
39         sheetData = workbook.get_sheet(0)
40         sheetData.write(rowNum,colNum,value)
41         workbook.save(self.excelPath)
42
43     #获取Excel中某一列的内容
44     def getColData(self,colNum=None):
45         if colNum != None:
46             colData = self.data.col_values(colNum)
47         else:
48             colData = self.data.col_values(0)
49         return colData
50
51     #根据依赖的caseID找到对应的行号
52     def depRowNum(self,caseID):
53         rowNum = 0
54         colDatas = self.getColData()
55         for colData in colDatas:
56             if caseID in colData:
57                 return rowNum
58             rowNum += 1
59
60     # 根据行号获取对应行的整行内容
61     def getRowData(self, rowNum):
62         rowData = self.data.row_values(rowNum)
63         return rowData
64
65     #根据依赖的caseID,获取依赖case的行号,根据行号取出对应行的内容
66     def dependCaseData(self,caseID):
67         rowNum = self.depRowNum(caseID)
68         rowData = self.getRowData(rowNum)
69         return rowData

下面是get_Case的源码:

  1 from data import operateExcel
  2 from data import excelColNum
  3 from data import operateJson
  4 import json
  5
  6 class getCaseData():
  7     def __init__(self):
  8         self.operateExcel = operateExcel.operateExcel()
  9         self.excelColNum = excelColNum.ExcelColNum()
 10         self.operateJson = operateJson.operateJson()
 11
 12     # 获取测试用例的行数,即case的个数,即表格的行数
 13     def getExcelRow(self):
 14         excelRows = self.operateExcel.getSheetRow()
 15         return excelRows
 16
 17     #获取是否执行
 18     def getRun_or_not(self,rowNum):
 19         flag = None
 20         colNum = self.excelColNum.get_run()
 21         runOrNot = self.operateExcel.getCellContent(rowNum,colNum)
 22         if runOrNot == "yes":
 23             flag = True
 24         else:
 25             flag = False
 26         return flag
 27
 28     #获取是否携带header,header可以放在配置文件内
 29     def getHeader(self,rowNum):
 30         colNum = self.excelColNum.get_haeder()
 31         headers = self.operateExcel.getCellContent(rowNum,colNum)
 32         headers = json.loads(headers)
 33         return headers
 34
 35     #获取请求方式
 36     def getRequestMethord(self,rowNum):
 37         colNum = self.excelColNum.get_methord()
 38         requestmethord = self.operateExcel.getCellContent(rowNum,colNum)
 39         return requestmethord
 40
 41     #获取请求的url
 42     def getUrl(self,rowNum):
 43         colNum = self.excelColNum.get_url()
 44         url = self.operateExcel.getCellContent(rowNum,colNum)
 45         return url
 46
 47     #获取请求数据
 48     def getRequestData(self,rowNum):
 49         colNum = self.excelColNum.get_requestData()
 50         reqData = self.operateExcel.getCellContent(rowNum,colNum)
 51         if reqData == "":
 52             return None
 53         else:
 54             return reqData
 55
 56     #获取json文件内的请求数据
 57     def getJsonData(self,rowNum):
 58         data = self.operateJson.getJsonData(self.getRequestData(rowNum))
 59         data = json.dumps(data)
 60         return data
 61
 62     #获取预期结果
 63     def getRespectResult(self,rowNum):
 64         colNum = self.excelColNum.getRespectResult()
 65         resResult = self.operateExcel.getCellContent(rowNum,colNum)
 66         if resResult == "":
 67             return None
 68         return resResult
 69
 70     #写入返回数据
 71     def wriReturn(self,rowNum,value):
 72         colNum = self.excelColNum.getReturnResult()
 73         self.operateExcel.writeValue(rowNum,colNum,value)
 74
 75     #写入实际结果(pass、fail)
 76     def wriRealResult(self,rowNum,value):
 77         colNum = self.excelColNum.getRealResult()
 78         self.operateExcel.writeValue(rowNum,colNum,value)
 79
 80     # 判断是否有case依赖
 81     def judgeCaseDep(self, rowNum):
 82         colNum = self.excelColNum.getDepCaseID()
 83         caseID = self.operateExcel.getCellContent(rowNum, colNum)
 84         if caseID != "":
 85             return caseID
 86         else:
 87             return None
 88
 89     #获取Excel中被依赖返回数据中的值
 90     def getDepReKey(self,rowNum):
 91         colNum = self.excelColNum.getDepReKey()
 92         reKey = self.operateExcel.getCellContent(rowNum,colNum)
 93         if reKey == "":
 94             return None
 95         else:
 96             return reKey
 97
 98     #获取Excel中需要传入的依赖的数据
 99     def getDependKey(self,rowNum):
100         colNum = self.excelColNum.getDepKey()
101         depkey = self.operateExcel.getCellContent(rowNum,colNum)
102         if depkey != "":
103             return depkey
104         else:
105             return None

  

原文地址:https://www.cnblogs.com/kakaln/p/8388332.html

时间: 2024-08-29 23:01:32

接口自动化(二)--操作Excel获取需要数据的相关文章

python基础学习6-mongodb、sys、接口开发、操作excel

1       mysql补充 cur = conn.cursor(cursor=pymysql.cursors.DictCursor)    #直接获取的数据转换为字典格式的 cur.description                   #直接获取的描述信息 fileds = [filed[0] for filed in cur.description]        #列表生成式,获取到第一行所有的字段 import pymysql,xlwtconn = pymysql.connect

MOOC(7)- case依赖、读取json配置文件进行多个接口请求-xlrd操作excel(11)

xlrd操作excel # -*- coding: utf-8 -*- # @Time : 2020/2/12 9:14 # @File : do_excel_xlrd_11.py # @Author: Hero Liu import xlrd # 打开工作簿 work_book = xlrd.open_workbook("../data/test_data.xlsx") # 通过指定索引获取sheet表,索引从0开始:也可以通过指定表名来获取 # table = work_book.

.Net实现微信公众平台开发接口(二) 之 “获取access_token”

access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token. 接口调用请求说明 http请求方式: GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET 参数说明 参数 是否必须 说明 grant_type 是 获取access_token填写client_credential appid 是 第三方用

利用POI操作Excel实现百万数据写入

POI 3.8以后提供了一个SXSSFWorkbook 类用于需要大量写入数据时使用,读取还是用XSSFWorkbook 1 public static void main(String[] args) { 2 // XSSFWorkbook 普通写入和读取 3 // SXSSFWorkbook 超大量数据写入 4 long time1 = System.currentTimeMillis(); 5 File file = new File("test.xlsx"); 6 if (fi

php编写app接口(二)-PHP生成XML数据

在Reponse.php文件中添加方法xml(): public static function xml(){ header('Content-type: text/xml'); $xml = "<?xml version='1.0' encoding = 'UTF-8'?>\n"; $xml .="<root>\n"; $xml .="<code>200</code>\n"; $xml .=&qu

Json文件 使用rapidjson获取Json数据

在游戏中使用Json来储存数据用作配置文件,既方便读取,有方便管理. 这里可以选择导出两种格式: 1.属性方式 [{"name":"cl","age":27,"sex":"M"}, {"name":"cbsss","age":25,"sex":"W"}, {"name":"gx&

十一、React 获取服务器数据: axios插件、 fetch-jsonp插件的使用

react获取服务器APi接口的数据: react中没有提供专门的请求数据的模块.但是我们可以使用任何第三方请求数据模块实现请求数据 一.axios 获取Api数据 使用文档:https://www.npmjs.com/package/axios git项目地址:https://github.com/axios/axios axios的作者觉得jsonp不太友好,推荐用CORS方式更为干净(后端运行跨域) npm官网:https://www.npmjs.com,在其搜索:axios即可看到详细说

[iOS微博项目 - 2.6] - 获取微博数据

github: https://github.com/hellovoidworld/HVWWeibo   A.新浪获取微博API 1.读取微博API 2.“statuses/home_timeline”接口 B.在app中获取微博数据 1.在“首页”控制器发送请求,获取json数据 1 /** 加载微博数据 */ 2 - (void) loadWeiboData { 3 // 创建AFNetworking的http操作中管理器 4 AFHTTPRequestOperationManager *m

Excel删除重复数据及用公式筛选重复项并标记颜色突出显示

当表格记录比较多时,常常会有重复数据,而重复记录往往只希望保存一条,因此需要把多余的删除:在 Excel 中,删除重复数据有两种方法,一种是用“删除重复数据”删除,另一种是用“高级筛选”删除:其中前者删除重复数据后会在表格末尾留下空行,而后者不会.如果只想查看重复数据而不必删除,可以给重复数据标记颜色突出显示,这样会一目了然.另外,还可以用公式统计每条重复数据有几条或筛选出重复数据.以下是Excel删除重复数据及用公式筛选重复项并标记颜色的具体操作方法,操作中所用版本为 Excel 2016.