开始代码如下,生成报告内容为空:
#coding=utf-8
import unittest,time,re
import requests
import json
import HTMLTestRunner
class test_api(unittest.TestCase):
def setUp(self):
self.url = "http://10.10.10.92:5050/api/demo/add.do"
self.params1 = {‘id‘:‘10011‘}
self.headers = {‘content-type‘:‘application/json‘}
self.verificationErrors = []
self.accept_next_alert = True
def test_aad_api(self):
req = requests.post(self.url,data=json.dumps(self.params1),headers=self.headers)
status = req.status_code
print(req.json())
if status == 200:
print("添加api接口成功")
else:
print("添加API接口失败!")
def tearDown(self):
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
#取前面时间
now = time.strftime("%Y-%m-%d-%H_%M_%S",time.localtime(time.time()))
filename = ‘D:\\python-function\\api\\report\\‘+now+‘result.html‘
fp = open(filename, ‘wb‘)
#定义测试报告
runner =HTMLTestRunner.HTMLTestRunner(
stream=fp,
title=u‘API测试报告‘,
description=u‘用例执行情况:‘)
#执行测试用例
testsuite = unittest.TestSuite()
#添加测试用例到测试集中
testsuite.addTest(test_api("test_aad_api"))
runner.run(testsuite)
百思不得其解,想了好久,最后是发现每次生成的报告文件都删除不了,然后在后面加了一个,fp.close(),在执行,报告有内容了,希望能够帮助到跟我一样的人,完整代码如下:
#coding=utf-8
import unittest,time,re
import requests
import json
import HTMLTestRunner
class test_api(unittest.TestCase):
def setUp(self):
self.url = "http://10.10.10.92:5050/api/demo/add.do"
self.params1 = {‘id‘:‘10011‘}
self.headers = {‘content-type‘:‘application/json‘}
self.verificationErrors = []
self.accept_next_alert = True
def test_aad_api(self):
req = requests.post(self.url,data=json.dumps(self.params1),headers=self.headers)
status = req.status_code
print(req.json())
if status == 200:
print("添加api接口成功")
else:
print("添加API接口失败!")
def tearDown(self):
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
#取前面时间
now = time.strftime("%Y-%m-%d-%H_%M_%S",time.localtime(time.time()))
filename = ‘D:\\python-function\\api\\report\\‘+now+‘result.html‘
fp = open(filename, ‘wb‘)
#定义测试报告
runner =HTMLTestRunner.HTMLTestRunner(
stream=fp,
title=u‘API测试报告‘,
description=u‘用例执行情况:‘)
#执行测试用例
testsuite = unittest.TestSuite()
#添加测试用例到测试集中
testsuite.addTest(test_api("test_aad_api"))
runner.run(testsuite)
fp.close()
这样报告就有内容了