python3+selenium+BeautifulReport生成自動化測試報告

自動化測試,最重要的還是測試報告,下面就教大家使用BeautifulReport生成自動化測試報告
GitHub:https://github.com/TesterlifeRaymond/BeautifulReport

第一步:安裝git

1、下載地址:https://git-scm.com/downloads
2、安裝:按照默認安裝就完事了
3、環境配置:配置(Git安裝目錄)/Git/cmd完整路徑到環境變量path下

配置環境變量

安裝成功

第二步:安裝BeautifulReport

1、cmd下進入到指定目錄:(python3安裝目錄)\Lib\site-packages


指定目錄

進入指定目錄

2、復制BeautifulReport到指定目錄

>>>git clone https://github.com/TesterlifeRaymond/BeautifulReport
復制BeautifulReport

復制完成
第三步:使用BeautifulReport生成報告
大概的一個目錄

1、測試用例py:

# -*- coding: utf-8 -*-
import os
import time
import unittest
from selenium import webdriver
from dateutil.parser import parse
from BeautifulReport import BeautifulReport

class Test(unittest.TestCase):
    # 定義一個保存截圖函數
    def save_img(self, img_name):       
        self.browser.get_screenshot_as_file('{}/{}.png'.format(os.path.abspath("E:/test/auto_test_local/Auto_Test/img"), img_name))
    # 啟動函數,每個用例測試前,都會執行該函數
    def setUp(self):
        self.browser = webdriver.Chrome()
        self.browser.set_window_size(1920, 1080)
        self.starttime = parse(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
        print("開始測試時間:", self.starttime)
        self.browser.get("https://www.baidu.com/")
        time.sleep(3)
    
    # 結束函數,每個用例測試結束后,都會執行該函數
    def tearDown(self):
        time.sleep(3)
        self.browser.quit()
        self.endtime = parse(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
        print("測試結束時間:", self.endtime)
        totaltime = (self.endtime - self.starttime).total_seconds()
        print("總時長:", totaltime, "秒")
 
    # 測試用例1:必須以test_開頭
    @BeautifulReport.add_test_img('打開登錄頁面', '輸入賬號密碼', '登錄')
    def test_01(self):
        u"""登錄"""
        self.browser.find_element_by_xpath("http://*[@id=\"u1\"]/a[7]").click()
        # 需要進行截圖的時候,直接調用截圖函數就ok,下同
        self.save_img('打開登錄頁面')
        self.browser.find_element_by_xpath("http://*[@id=\"TANGRAM__PSP_10__footerULoginBtn\"]").click()
        # self.browser.find_element_by_id("TANGRAM__PSP_10__footerULoginBtn").click()
        self.browser.find_element_by_id("TANGRAM__PSP_10__userName").send_keys("userName")
        time.sleep(1)
        self.browser.find_element_by_id("TANGRAM__PSP_10__password").send_keys("password")
        time.sleep(1)
        self.save_img('輸入賬號密碼')
        self.browser.find_element_by_id("TANGRAM__PSP_10__submit").click()
        time.sleep(1)
        self.save_img('登錄')

    # 測試用例2:也是必須以test_開頭
    @BeautifulReport.add_test_img('測試用例2')
    def test_02(self):
        u"""測試用例2"""
        self.save_img('測試用例2')
        time.sleep(1)

if __name__ == '__main__':
    unittest.main()

2、整合測試用例py

# -*- coding: utf-8 -*-
import unittest
from BeautifulReport import BeautifulReport

# 用例存放位置
test_case_path="E:/test/auto_test_local/Auto_Test/Test_Case"
# 測試報告存放位置
log_path='E:/test/auto_test_local/Auto_Test/Test_Result/Test_Report'
# 測試報告名稱
filename='測試報告-百度'
#用例名稱
description='百度登錄'
# 需要執行哪些用例,如果目錄下的全部,可以改為"*.py",如果是部分帶test后綴的,可以改為"*test.py"
pattern="login_test.py"

if __name__ == '__main__':
    test_suite = unittest .defaultTestLoader.discover(test_case_path, pattern=pattern)
    result = BeautifulReport(test_suite)
    result.report(filename=filename,description=description,log_path=log_path)

3、執行測試:每次執行測試,只需要執行整合測試用例py就可以了

測試完成

4、測試報告展示:

測試報告
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容