HTMLTestRunner 是 Python 標准庫的 unittest 模塊的一個擴展,它可以生成 HTML的 測試報告。
一、下載HTMLTestRunnerNew.py文件:
下載鏈接:https://pan.baidu.com/s/1wqZIWN08h_cvvh0qorQI1g
二、安裝:
將下載的文件保存到Python安裝目錄Lib下:
如:C:\Miniconda3\Lib
引入的方式: from HTMLTestRunnerNew import HTMLTestRunner

三、單元測試源碼如下:
import unittest
import requests
from HTMLTestRunnerNew import HTMLTestRunner
import time
import os
class MyTest(unittest.TestCase):
def setUp(self):
def test_001(self):
self.assertEqual(2,1+1)
# """使用錯誤的請求方法"""
# def test_01(self):
# res = requests.get(self.url)
# json_data = res.json()
# # print(json_data)
# # print(res)
# status =res.status_code
# self.assertEqual(status,200)
# self.assertEqual(json_data["code"],10700003)
# self.assertEqual(json_data["message"],"請求類型錯誤!")
# def test_02(self):
# res = requests.post(self.url)
# json_data = res.json()
# status = res.status_code
# self.assertEqual(json_data["code"],10001)
# self.assertIn("參數驗證錯誤",json_data["message"])
# print(json_data)
if __name__ == '__main__': s = unittest.TestSuite() #實例化 s.addTests(unittest.TestLoader().loadTestsFromTestCase(MyTest)) #加載用例 now = time.strftime('%Y-%m%d %H%M%S') print(now) # filename = open(os.getcwd() + '/testResult_report' + now + '.html','wb') # runner = HTMLTestRunner(stream = filename,title = '單元測試報告',description = '單元測試報告',tester = '正在學習的測試人員' ) # runner.run(s) # 定義報告存放路徑 filename = 'F:/ ' + now + 'result.html' fp=open(filename,'wb') #定義測試報告 runner=HTMLTestRunner(stream=fp,title='測試報告',description='單元測試報告:',tester = '正在學習的測試人員') runner.run(s) fp.close() # 關閉報告文件
四 執行代碼
找到代碼的路徑

在代碼路徑上 cmd

輸入 python **.py 去路徑查找生成的報告

