The start code is as follows, and the resulting report content is empty:
#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 ("Add API Interface succeeded")
Else
Print ("Add API interface failed!") ")
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 test Report ',
Description=u ' use case execution: ')
#执行测试用例
Testsuite = UnitTest. TestSuite ()
#添加测试用例到测试集中
Testsuite.addtest (Test_api ("Test_aad_api"))
Runner.run (Testsuite)
Best of all, think for a long time, and finally found that each report file generated can not be deleted, and then add a,fp.close (), in the implementation, the report has content, hope to be able to help people like me, the complete code is as follows:
#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 ("Add API Interface succeeded")
Else
Print ("Add API interface failed!") ")
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 test Report ',
Description=u ' use case execution: ')
#执行测试用例
Testsuite = UnitTest. TestSuite ()
#添加测试用例到测试集中
Testsuite.addtest (Test_api ("Test_aad_api"))
Runner.run (Testsuite)
Fp.close ()
So the report will have a content.
Call Htmltestrunner Production report content is empty solution