python + ddt + unittest + excel + request implement an interface automation

Interface automated testing process: Requirement Analysis - case design - script development - test execution - Analysis Results 
1. Obtaining interface document, the document acquisition request, the transmission protocol in accordance with the request parameters, the response parameters, by determining whether the test design with Example
2 script development: interface calls, the results of verification
3. perform test
4. Submit report message
5. results analysis

package read excel, a Dictionary
to xlrd Import 
from the conf Import *


class ExcelUtil ():

'' 'taken from the test excel, the output is [Column Name {1: 1 in the first column, column name 2: first column 2}, {1 column names: a second column, the column name 2: The second column 2} ...] '' '
    the __init __ DEF (Self, excelPath, sheetIndex = 0): 
self.data = xlrd.open_workbook (excelPath)
self.table = self.data.sheet_by_index (sheetIndex)
# Get the first row as the key value
self.keys = self.table. row_values (0)
# Get the number of rows
self.rowNum = self.table.nrows
# acquires the total number of columns
self.colNum = self.table.ncols

DEF dict_data (Self):
IF self.rowNum <=. 1:
Print ( "total number of rows less than. 1 ")
the else:
R & lt = []
J =. 1
for I in List (Range (-self.rowNum. 1)):
S = {}
# corresponding to the values taken from the second row of values
s [ 'rowNum'] = i + 2
values = self.table.row_values(j)
# print(values)
for x in list(range(self.colNum)):
s[self.keys[x]] = values[x]
r.append(s)
j += 1
return r

if __name__ == "__main__":
filepath = xlsPath+'/test.xlsx'
sheetIndex = 0
data = ExcelUtil(filepath, sheetIndex)
print(data.dict_data())
返回结果:[{'姓名': 1.0, '年龄': 18.0}, {'姓名': 1.0, '年龄': 20.0}]
使用ddt数据驱动读取Excel数据
#usr/bin/python
#encoding:utf-8
from ddt import ddt, data, unpack
import unittest
from debug import ExcelUtil
from conf import *
filepath = xlsPath + '/test.xlsx'
sheetIndex = 0
s = ExcelUtil(filepath, sheetIndex).dict_data()
@ddt
class DoubanTest(unittest.TestCase):
@classmethod
def setUp(self):
print("****Start Test*******")
@classmethod
def tearDown(self):
print("****End Test*******")
# @data([1, 2, 3, 6])
# @unpack
# def test_add(self,testdata1,testdata2,testdata3,exceptdata):
# sum = testdata1 + testdata2 + testdata3
# self.assertEqual(sum,exceptdata)
@data(*s)
def test_print(self,a):
print(a)

if __name__=='__main__':
unittest.main()
Return result:

The Start the Test ******* ****
{ 'rowNum': 2, 'Name': 1.0, 'Age': 18.0}
**** ******* the Test End
**** the Start ******* the Test
{ 'rowNum':. 3, 'name': 1.0, 'Age': 20.0}
**** ******* the Test End



Guess you like

Origin www.cnblogs.com/zyblb/p/10942106.html