接口自动化(二)--操作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