調用HTMLTestRunner生產的報告內容為空白解決辦法

來源:互聯網
上載者:User

標籤:setup   err   accept   alert   param   ror   測試   ati   http   

開始代碼如下,產生報告內容為空白:

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

這樣報告就有內容了

調用HTMLTestRunner生產的報告內容為空白解決辦法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.