python3+HTMLTestRunner


下載修改過的HTMTestRunner.py 放置到python安裝目錄下的lib目錄下:

測試報告例子截圖:

編寫測試代碼:

import os
import sys
import unittest
import time
from selenium import webdriver
from HTMLTestRunner import HTMLTestRunner


sys.path.append('E:/webdriver/Page/test_mathfun.py')
ABSPATH = os.path.abspath(os.path.realpath(os.path.dirname(__file__)))


class MyTest(unittest.TestCase):
driver = webdriver.Chrome()

def setUp(self):
pass

def test_case1(self):
"""打開百度網頁"""
self.driver.get("https://www.baidu.com/")
assert "百度一下,你就知道" in self.driver.title

def test_case2(self):
"""搜索功能"""
self.driver.get("https://www.baidu.com/")
self.driver.find_element_by_id("kw").send_keys("taozhen")
self.driver.find_element_by_id("su").click()

def test_quit(self):
"""關閉瀏覽器"""
self.driver.quit()

def tearDown(self):
# self.driver.quit()
# self.driver.refresh() # 將退出瀏覽器的操作變成刷新瀏覽器,用於不同用例之間的接洽操作
pass


if __name__ == '__main__':
# 構造測試集
suite = unittest.TestSuite() # 構造測試集
suite.addTest(MyTest("test_case1")) # 加入測試用例
suite.addTest(MyTest("test_case2"))
suite.addTest(MyTest("test_quit"))

# 執行測試
date = time.strftime("%Y%m%d") # 定義date為日期,time為時間
time = time.strftime("%Y%m%d_%H%M%S")
path = "./report/ui/"
# 判斷是否定義的路徑目錄存在,不能存在則創建
if not os.path.exists(path):
os.makedirs(path)
else:
pass
report_path = path+time+"UIreport.html" # 將運行結果保存到report,名字為定義的路徑和文件名,運行腳本

report_title = u"測試報告"
desc = u'功能自動化測試報告詳情:'

with open(report_path, 'wb') as report:
runner = HTMLTestRunner(stream=report, title=report_title, description=desc)
runner.run(suite)
# 關閉report,腳本結束
report.close()

3 使用pycharm能夠正常運行測試用例,但是不能創建與寫入測試報告。

問題原因:發現是編輯器的原因,編輯器為了方便用戶執行測試,都有一項功能,可以用編輯器來調用 、

unittest 或者 nose 來執行測試用例,這種情況下,執行的只是用例或者套件,而不是整個文件,寫在main

里的代碼是不會被執行的!!

自然無法生成測試報告。

解決辦法:運行文件的時候不要選擇以 unitest 的方式運行,要選擇運行文件的方式。

 

 


免責聲明!

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



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