自動(dòng)化測(cè)試,最重要的還是測(cè)試報(bào)告,下面就教大家使用BeautifulReport生成自動(dòng)化測(cè)試報(bào)告
GitHub:https://github.com/TesterlifeRaymond/BeautifulReport
第一步:安裝git
1、下載地址:https://git-scm.com/downloads
2、安裝:按照默認(rèn)安裝就完事了
3、環(huán)境配置:配置(Git安裝目錄)/Git/cmd完整路徑到環(huán)境變量path下

配置環(huán)境變量

安裝成功
第二步:安裝BeautifulReport
1、cmd下進(jìn)入到指定目錄:(python3安裝目錄)\Lib\site-packages

指定目錄

進(jìn)入指定目錄
2、復(fù)制BeautifulReport到指定目錄
>>>git clone https://github.com/TesterlifeRaymond/BeautifulReport

復(fù)制BeautifulReport

復(fù)制完成
第三步:使用BeautifulReport生成報(bào)告

大概的一個(gè)目錄
1、測(cè)試用例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):
# 定義一個(gè)保存截圖函數(shù)
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))
# 啟動(dòng)函數(shù),每個(gè)用例測(cè)試前,都會(huì)執(zhí)行該函數(shù)
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("開(kāi)始測(cè)試時(shí)間:", self.starttime)
self.browser.get("https://www.baidu.com/")
time.sleep(3)
# 結(jié)束函數(shù),每個(gè)用例測(cè)試結(jié)束后,都會(huì)執(zhí)行該函數(shù)
def tearDown(self):
time.sleep(3)
self.browser.quit()
self.endtime = parse(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
print("測(cè)試結(jié)束時(shí)間:", self.endtime)
totaltime = (self.endtime - self.starttime).total_seconds()
print("總時(shí)長(zhǎng):", totaltime, "秒")
# 測(cè)試用例1:必須以test_開(kāi)頭
@BeautifulReport.add_test_img('打開(kāi)登錄頁(yè)面', '輸入賬號(hào)密碼', '登錄')
def test_01(self):
u"""登錄"""
self.browser.find_element_by_xpath("http://*[@id=\"u1\"]/a[7]").click()
# 需要進(jìn)行截圖的時(shí)候,直接調(diào)用截圖函數(shù)就ok,下同
self.save_img('打開(kāi)登錄頁(yè)面')
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('輸入賬號(hào)密碼')
self.browser.find_element_by_id("TANGRAM__PSP_10__submit").click()
time.sleep(1)
self.save_img('登錄')
# 測(cè)試用例2:也是必須以test_開(kāi)頭
@BeautifulReport.add_test_img('測(cè)試用例2')
def test_02(self):
u"""測(cè)試用例2"""
self.save_img('測(cè)試用例2')
time.sleep(1)
if __name__ == '__main__':
unittest.main()
2、整合測(cè)試用例py
# -*- coding: utf-8 -*-
import unittest
from BeautifulReport import BeautifulReport
# 用例存放位置
test_case_path="E:/test/auto_test_local/Auto_Test/Test_Case"
# 測(cè)試報(bào)告存放位置
log_path='E:/test/auto_test_local/Auto_Test/Test_Result/Test_Report'
# 測(cè)試報(bào)告名稱
filename='測(cè)試報(bào)告-百度'
#用例名稱
description='百度登錄'
# 需要執(zhí)行哪些用例,如果目錄下的全部,可以改為"*.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、執(zhí)行測(cè)試:每次執(zhí)行測(cè)試,只需要執(zhí)行整合測(cè)試用例py就可以了

測(cè)試完成
4、測(cè)試報(bào)告展示:

測(cè)試報(bào)告
