python+unittest+excel接口自动化_Python+unittest+requests+excel实现接口自动化测试框架

该博客内容涉及自动化接口测试的实现,使用了unittest、parameterized、HTMLTestRunner等库,从Excel读取测试用例数据,进行API调用,对比检查点数据,并将测试结果写回Excel。同时,它还生成了详细的HTML测试报告。
摘要由CSDN通过智能技术生成

from common.operate_excel import *

importunittestfrom parameterized importparameterizedfrom common.send_request importRunMethodimportjsonfrom common.logger importMyLoggingimportjsonpathfrom common.is_instance importIsInstancefrom HTMLTestRunner importHTMLTestRunnerimportosimporttime

lib_path= os.path.abspath(os.path.join(os.path.dirname(__file__), "../data"))

file_path= lib_path + "/" + "接口自动化测试.xlsx" #excel的地址

sheet_name = "测试用例"log=MyLogging().loggerdefgetExcelData():

list=ExcelData(file_path, sheet_name).readExcel()returnlistclassTestCase(unittest.TestCase):

@parameterized.expand(getExcelData())deftest_api(self, rowNumber, caseRowNumber, testCaseName, priority, apiName, url, method, parmsType, data,

checkPoint, isRun, result):if isRun == "Y" or isRun == "y":

log.info("【开始执行测试用例:{}】".format(testCaseName))

headers= {"Content-Type": "application/json"}

data= json.loads(data) #字典对象转换为json字符串

c = checkPoint.split(",")

log.info("用例设置检查点:%s" %c)print("用例设置检查点:%s" %c)

log.info("请求url:%s" %url)

log.info("请求参数:%s" %data)

r=RunMethod()

res=r.run_method(method, url, data, headers)

log.info("返回结果:%s" %res)

flag=Nonefor i inrange(0, len(c)):

checkPoint_dict={}

checkPoint_dict[c[i].split('=')[0]] = c[i].split('=')[1]#jsonpath方式获取检查点对应的返回数据

list = jsonpath.jsonpath(res, c[i].split('=')[0])

value=list[0]

check= checkPoint_dict[c[i].split('=')[0]]

log.info("检查点数据{}:{},返回数据:{}".format(i + 1, check, value))print("检查点数据{}:{},返回数据:{}".format(i + 1, check, value))#判断检查点数据是否与返回的数据一致

flag =IsInstance().get_instance(value, check)ifflag:

log.info("【测试结果:通过】")

ExcelData(file_path, sheet_name).write(rowNumber+ 1, 12, "Pass")else:

log.info("【测试结果:失败】")

ExcelData(file_path, sheet_name).write(rowNumber+ 1, 12, "Fail")#断言

self.assertTrue(flag, msg="检查点数据与实际返回数据不一致")else:

unittest.skip("不执行")if __name__ == '__main__':#unittest.main()

#Alt+Shift+f10 执行生成报告

#报告样式1

suite =unittest.TestSuite()

suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestCase))

now= time.strftime('%Y-%m-%d %H_%M_%S')

report_path= r"D:\PycharmProjects\AutoTest\result\report.html"with open(report_path,"wb") as f:

runner= HTMLTestRunner(stream=f, title="Esearch接口测试报告", description="测试用例执行情况", verbosity=2)

runner.run(suite)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值