標籤:python3 time main gpo site eal __name__ efault block
前言
selenium多線程跑用例,這個前面一篇已經解決了,如何產生一個測試報告這個是痛點,剛好在github上有個大神分享了BeautifulReport,完美的結合起來,就能產生報告了。
環境必備:
- python3.6 : BeautifulReport不支援2.7
- tomorrow : pip install tomorrow安裝
- BeautifulReport : github下載後放到/Lib/site-packages/目錄下
BeautifulReport
1.BeautifulReport:BeautifulReport
2.下載方法:
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-8import unittestfrom selenium import webdriverimport timeclass 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-8import unittestfrom BeautifulReport import BeautifulReportimport osfrom 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
selenium+python自動化91-unittest多線程產生報告(BeautifulReport)