调用HTMLTestRunner生产的报告内容为空解决办法

开始代码如下,生成报告内容为空:

#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()

这样报告就有内容了

转载于:https://www.cnblogs.com/wensiyang0916/p/6003391.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值