Unittest+HTMLTestRunner不能生成報告解決方法和報告安裝使用


-Unittest+HTMLTestRunner不能生成報告解決方法

 

1、問題現象

    在使用HTMLTestRunner生成測試報告時,出現程序運行不報錯,但不能生成報告的情況。

    剛開始找了很久沒發現問題,后來加上打印信息,發現根本沒執行生成報告這部分代碼。最后網上找到原因:pycharm 在運行測試用例的時候 默認是以unittest 框架來運行的,所以不能生成測試報告。

需要設置成不要用unittest框架運行:

HTMLTestRunner.pyw文件經過修改的,需要下載:鏈接:https://pan.baidu.com/s/1BtF4Xus3kecI8qfTAy4z7w
提取碼:2gtj

復制代碼
 
         
#unittest測試框架
#pytest比unittest全面一點
#導包
import requests#導入接口用的包
import unittest
from pyJIAO.APP.APP.aa.baogao4 import Test01

from test1.PO3.zhiliao.HTMLTestRunnerNew import HTMLTestRunner
import time

class Test(unittest.TestCase):
# 用於測試用例執行前的初始化工作
def setUp(self):
print("test start")

def test_bbb(self):
print("test bbb")

# 用於測試用例執行之后的善后工作
def tearDown(self):
print("test end")

if __name__ == '__main__':
# 實例化測試套件
suite = unittest.TestSuite()
# 加載測試用例
suite.addTest(Test("test_bbb"))
#獲取當前時間
now = time.strftime("%Y-%m-%d %H_%M_%S")
# 定義報告存放路徑
filename = now + 'result.html'
#報告存放在當前目錄
fp = open(filename, 'wb')
# 定義測試報告
runner = HTMLTestRunner(stream=fp, title='測試報告', description='用例執行情況:')
runner.run(suite)
fp.close() # 關閉報告文件
 
復制代碼

 

2、如何判斷是否以unittest框架運行?

  如下,只要運行環境是unittest in ...的,就都是以unittest框架運行的

 

3、解決方案

  只要不以unittest框架方式運行就可解決不能生成報告的問題。可自己新增一個不以unittest框架方式運行的環境。

 

 

 

1)如下點擊edit configurations-->選中python-->點擊+

2)點擊python

3)填寫環境名稱和腳本路徑

 

 

 

 

4)選擇步驟三種新增的環境,運行腳本,即可生成報告

 

 

 5、用HTMLTestRunner必須下載好文件放到python的lid目錄也行,直接放到更目錄也行,最終是要導入的:

 

 

 

二、還有一種比HTMLTestRunner跟加好用的,BeautifulReport

安裝下載:一個報告:pip install BeautifulReport

執行代碼如下:

import unittest
from BeautifulReport import BeautifulReport

if __name__ == '__main__':
test_suite = unittest.defaultTestLoader.discover('E:\\pythonJIAO\\test1\\jiekou\\scripts', pattern='jieko*.py')
result = BeautifulReport(test_suite)
result.report(filename='測試報告', description='測試deafult報告', report_dir='E:\\pythonJIAO\\test1\\PO3\\scripts', theme='theme_default')

"""


BeautifulReport.report
report (
filename -> 測試報告名稱, 如果不指定默認文件名為report.html
description -> 測試報告用例名稱展示
report_dir='.' -> 報告文件寫入路徑
theme='theme_default' -> 報告主題樣式 theme_default theme_cyan theme_candy theme_memories
)

BeautifulReport.add_test_img

"""
生成的報告是不是比較好看:

 

三、用pytest生成報告最方便,報告如下,具體看另外文章
pytest生成報告的插件:    pip install pytest-html
安裝pytest和allure:    pip install pytest   和  pip install allure-pytest
查看:pip list

生成報告命令:pytest --html=report/report.html

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM