python学习笔记(接口自动化框架 V1.0)

时间:2021-02-26 18:43:53

之前是利用python自带的unittest测试框架

这次自己设计一个 之后再一点点往里面加功能

(ps:当然这个框架真的是很简单。。很简单。。。很简单。。。)

excel文件格式:

python学习笔记(接口自动化框架 V1.0)

 #!/usr/bin/env python
# -*- coding: utf_8 -*- import xlrd
import json class CreateExcel:
def __init__(self):
pass @classmethod
def open_excel(cls):
path = "testcase.xls"
workbook = xlrd.open_workbook(path)
table = workbook.sheets()[0]
return table
# 获取sheet @classmethod
def get_nrows(cls, table):
nrows = table.nrows
return nrows
# 获取行号 @classmethod
def get_name(cls, table, nrows):
testname = []
for i in range(1, nrows):
testname.append(table.cell(i, 0).value)
return testname
# 获取用例name @classmethod
def get_data(cls, table, nrows):
testdata = []
for i in range(1, nrows):
data = json.loads(table.cell(i, 1).value)
testdata.append(data)
return testdata
# 获取data接口参数 @classmethod
def get_url(cls, table, nrows):
testurl = []
for i in range(1, nrows):
testurl.append(table.cell(i, 2).value)
return testurl
# 获取接口测试url @classmethod
def get_method(cls, table, nrows):
testmethod = []
for i in range(1, nrows):
testmethod.append(table.cell(i, 3).value)
return testmethod
# 获取接口测试method @classmethod
def get_pattern(cls, table, nrows):
testpattern = []
for i in range(1, nrows):
testpattern.append(table.cell(i, 4).value)
return testpattern
# 获取接口期望响应结果 @classmethod
def get_report(cls, table, nrows):
testreport = []
for i in range(1, nrows):
testreport.append(table.cell(i, 5).value)
return testreport
# 获取用例期望的运行结果 if __name__ == "__main__":
CreateExcel()

上面代码是处理excel文档的

下面代码是测试平台

 #!/usr/bin/env python
# -*- coding: utf_8 -*- import requests
import re
from createexcel import CreateExcel class CreateTest:
def __init__(self):
pass @classmethod
def test_api(cls, method, url, data):
global results
if method == "post":
results = requests.post(url, data)
if method == "get":
results = requests.get(url, data)
return results @classmethod
def test_on(cls):
print "用例执行开始" @classmethod
def test_close(cls):
print "用例执行结束" @classmethod
def test_result(cls, ra, rb):
if ra == rb:
print "测试结果: 测试通过"
else:
print "测试结果: 测试失败" @classmethod
def test_http(cls, code):
if code == 200:
print "测试请求: 请求通过" @classmethod
def test_main(cls):
global report
table = CreateExcel.open_excel()
nrows = CreateExcel.get_nrows(table)
for i in range(0, nrows - 1):
testname = CreateExcel.get_name(table, nrows)[i]
testdata = CreateExcel.get_data(table, nrows)[i]
testurl = CreateExcel.get_url(table, nrows)[i]
testmethod = CreateExcel.get_method(table, nrows)[i]
testpattern = CreateExcel.get_pattern(table, nrows)[i]
testreport = CreateExcel.get_report(table, nrows)[i]
CreateTest.test_on()
print "测试用例:", testname
try:
testresults = CreateTest.test_api(testmethod, testurl, testdata)
CreateTest.test_http(testresults.status_code)
pattern = re.compile(testpattern)
match = pattern.search(testresults.url)
if match.group() == testpattern:
report = "pass"
CreateTest.test_result(testreport, report)
except AttributeError:
report = "no"
CreateTest.test_result(testreport, report)
except Exception.__base__:
print "测试请求: 请求失败"
report = None
CreateTest.test_result(testreport, report)
CreateTest.test_close() if __name__ == '__main__':
CreateTest()