前言
selenium多線程跑用例,這個前面一篇已經解決了,如何生成一個測試報告這個是難點,剛好在github上有個大神分享了BeautifulReport,完美的結合起來,就能生成報告了。
環境必備:
- python3.6 : BeautifulReport不支持2.7
- tomorrow : pip install tomorrow安裝
- BeautifulReport : github下載后放到/Lib/site-packages/目錄下
BeautifulReport
1.BeautifulReport下載地址:BeautifulReport
2.下載方法:
- 方法一 會使用git的直接用git下載到本地
git clone https://github.com/TesterlifeRaymond/BeautifulReport
- 方法二 點Clone or Download按鈕,Download ZIP就能下載到本地了
3.下載完之后,把BeautifulReport整個包放到python的/Lib/site-packages/目錄下
使用方法
1.項目結構:
case test開頭的.py用例腳本
report 放生成的html報告
run_all.py 用於執行全部腳本
2.單個測試腳本test_a.py參考
# coding:utf-8
import unittest
from selenium import webdriver
import time
class Testaa(unittest.TestCase):
u'''測試用例a的集合'''
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Firefox()
def setUp(self):
self.driver.get("https://www.cnblogs.com/yoyoketang/")
def test_01(self):
u'''用例1:用例1的操作步驟'''
t = self.driver.title
print(t)
self.assertIn("悠悠", t)
def test_02(self):
u'''用例2:用例2的操作步驟'''
t = self.driver.title
print(t)
self.assertIn("悠悠", t)
def test_03(self):
u'''用例3:用例3的操作步驟'''
t = self.driver.title
print(t)
self.assertIn("悠悠", t)
@classmethod
def tearDownClass(cls):
cls.driver.quit()
if __name__ == "__main__":
unittest.main()
3.run_all代碼
# coding=utf-8
import unittest
from BeautifulReport import BeautifulReport
import os
from tomorrow import threads
# 獲取路徑
curpath = os.path.dirname(os.path.realpath(__file__))
casepath = os.path.join(curpath, "case")
if not os.path.exists(casepath):
print("測試用例需放到‘case’文件目錄下")
os.mkdir(casepath)
reportpath = os.path.join(curpath, "report")
if not os.path.exists(reportpath): os.mkdir(reportpath)
def add_case(case_path=casepath, rule="test*.py"):
'''加載所有的測試用例'''
discover = unittest.defaultTestLoader.discover(case_path,
pattern=rule,
top_level_dir=None)
return discover
@threads(3)
def run(test_suit):
result = BeautifulReport(test_suit)
result.report(filename='report.html', description='測試deafult報告', log_path='report')
if __name__ == "__main__":
# 用例集合
cases = add_case()
print(cases)
for i in cases:
print(i)
run(i)
4.報告效果圖
備注:BeautifulReport是某大神在github分享的框架,這里借花獻佛了,更多使用方法參考地址:https://github.com/TesterlifeRaymond/BeautifulReport