前言
selenium多線程跑用例,這個前面一篇已經解決了,如何生成一個測試報告這個是難點,恰好在github上有個大神分享了BeautifulReport,完美的結合起來,就能生成報告了。html
環境必備:python
python3.6 : BeautifulReport不支持2.7
tomorrow : pip install tomorrow安裝
BeautifulReport : github下載後放到/Lib/site-packages/目錄下
BeautifulReport
1.BeautifulReport下載地址:BeautifulReportgit
2.下載方法:github
方法一 會使用git的直接用git下載到本地
git clone https://github.com/TesterlifeRaymond/BeautifulReportweb
方法二 點Clone or Download按鈕,Download ZIP就能下載到本地了多線程
3.下載完以後,把BeautifulReport整個包放到python的/Lib/site-packages/目錄下框架
使用方法
1.項目結構:
case test開頭的.py用例腳本
report 放生成的html報告
run_all.py 用於執行所有腳本測試
2.單個測試腳本test_a.py參考ui
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代碼
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