摘要:
python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库。
主要内容:
先上代码
automation_interface.py :
# -*- coding:utf-8 -*-
import xlrd
import xlwt
import automation_method
# excel 操作
class AutoTest:
def __init__(self):
pass
# xlrd读取数据
def read_text(self):
data_xls = []
xls = xlrd.open_workbook(u"接口自动化测试用例.xlsx")
table = xls.sheet_by_name(u"接口测试报告")
num_row = table.nrows
for i in range(num_row):
id = table.row(i)[0].value
title = table.row(i)[1].value
body = table.row(i)[2].value
parameter = table.row(i)[3].value
expect_result = table.row(i)[4].value
actual_result = table.row(i)[5].value
test_result = table.row(i)[6].value
data = (id, title, body, parameter, expect_result, actual_result, test_result)
data_xls.append(data)
return data_xls
# xlwt写入数据
def write_text(self):
wbk = xlwt.Workbook(encoding='utf-8')
sheet = wbk.add_sheet(u'接口测试报告')
read_xls = self.read_text()
for i in range(len(read_xls)):
for m in range(len(read_xls[i])):
if m == 5 and i > 3:
sheet.write(i, m, str(automation_method.test_interface(read_xls[1][1],
read_xls[i][2],
read_xls[i][3],
read_xls[1][3])))
else:
sheet.write(i, m, read_xls[i][m])
wbk.save(u"接口自动化测试用例.xlsx")
if __name__ == '__main__':
a = AutoTest()
a.write_text()
automation_method.py :
# -*- coding:utf-8 -*-
from binascii import a2b_hex
from Crypto.Cipher import AES
from Crypto import Random
import requests
import os
# AES加密
def aes(timestamp):
BS = 16
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
key_str = "D8EF40495D6C6CD171BFA4403516B035"
plain = timestamp
iv = Random.new().read(AES.block_size)
obj = AES.new(a2b_hex(key_str),AES.MODE_ECB,iv)
plain = pad(plain)
result = obj.encrypt(plain).encode("hex").upper()
return result
# APP接口请求
def test_interface(head_app, body, payload, parameter_app):
url = head_app + str(body) + "?" + str(payload) + parameter_app
headers = {'content-type': 'application/json'}
try:
r = requests.post(url, headers=headers)
return r.json()
except:
return {"status": url}
# kill WPS
def kill_wps():
cmd = 'taskkill /F /IM wpscloudsvr.exe'
os.system(cmd)
总结:
做了一年的接口测试以及接口自动化测试,想把这方面的工作简单化,让不懂代码的测试人员也可以进行该方面的工作,写一个基于excel接口测试自动化工具,最后以失败告终,主要原因还是因为接口请求参数不确定性,以及参数是其他接口返回值,对于这个难点,后期下定决心了再进行下去。
参考文献:
Python中使用xlrd、xlwt操作excel表格详解
本人利用Bootstrap + EasyUI + Django开发网站:http://www.xuyangting.com/ 欢迎来访
阳台测试: 239547991(群号)