刚接触python接口自动化,目前只是摸索,记录,代码还未写完,后期会持续更新
用例用excel编写,分3个sheet页:
读取用例:
import xlrd
class read_exl(object):
"""docstring for read_exl"""
def __init__(self):
super(read_exl, self).__init__()
self.filename = r'D:\auto_test\testcase_excel\cases_v8.xlsx'
self.date = xlrd.open_workbook(self.filename)
def exl_read_url(self):
table = self.date.sheet_by_name('接口信息')
return table.cell_value(1, 3)
def exl_read_date(self):
table = self.date.sheet_by_name('变量')
dict01 = {table.cell_value(1, 0): int(table.cell_value(1, 1)),
table.cell_value(2, 0): int(table.cell_value(2, 1))}
return dict01
def exl_read_case(self):
a = self.exl_read_date()
list01 = []
table = self.date.sheet_by_name('用例')
row = table.nrows
# print(row)
for i in range(1, row):
m = table.cell_value(i, 2)
for k, v in a.items():
m = m.replace(k, str(v))
# print(m)
# print('---'*10)
if table.cell_value(i, 1) == 1:
list01.append(m)
i += 1
else:
break;
return list01
testcase:
import unittest, requests, json
from public.public import read_exl
class register(unittest.TestCase):
def setUp(self):
self.url = url
def test_register(self):
self.date = date
re = requests.post(self.url, self.date)
self.result = re.json()
self.assertEquals(self.result['status'], 0)
self.assertEquals(self.result['code'], "20103")
r = read_exl()
url = r.exl_read_url()
date = json.loads(r.exl_read_case()[0])
if __name__ == '__main__':
unittest.main()
结果: