python selenium-webdriver 生成測試報告 (十四)


測試最后的一個重要的過程就是生成一份完整的測試報告,生成測試報告的主要是通過python的一個第三方模塊HTMLTestRunner.py生成,但是生成的測試報告不是特別的美觀,而且沒有辦法統計測試結果分類,同時也沒有辦法把測試結果的圖片保存下來。通過github 查找到一個改版后的HTMLTestRunner,但是發現美觀是美觀些,但是有些小問題,而且也不能把我的測試結果截圖顯示,所以自己又在其基礎上增加了圖片、測試結果的餅圖分布、對測試結果進行錯誤、失敗、通過進行分類。

生成的報告

 下面看下如何生成報告,接下來我們為寫一個測試演示的代碼生成一份報告,生成報告的時候主要利用unittest和HTMLTestReport構建。

# -*- coding: utf-8 -*-
from selenium import webdriver
import unittest
import os,sys,time
import HTMLTestReport


class Baidu(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Chrome()  
        self.driver.implicitly_wait(30)
        self.driver.maximize_window()
        self.base_url = "https://www.baidu.com"
        self.driver.get(self.base_url)

    def test_case1(self):
        '''設計測試失敗case'''
        print ("========【case_0001】打開百度搜索 =============")
        current_time = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
        # 必須打印圖片路徑HTMLTestRunner才能捕獲並且生成路徑,\image\**\\**.png 是獲取路徑的條件,必須這樣的目錄
        pic_path = '.\\result\\image\\' + '2017-07-17\\' + current_time +'.png'  #設置存儲圖片路徑,測試結果圖片可以按照每天進行區分
        print (pic_path)  #打印圖片路徑
        time.sleep(2)
        self.driver.save_screenshot(pic_path)  #截圖,獲取測試結果
        self.assertEqual('百度',self.driver.title)  #斷言判斷測試是否成功,判斷標題是否為百度(設計失敗的case)

    def test_case2(self):
        '''設計測試過程中報錯的case'''
        print ("========【case_0002】搜索selenium =============")
        self.driver.find_element_by_id("kw").clear()
        self.driver.find_element_by_id("kw").send_keys(u"selenium")
        self.driver.find_element_by_id('su').click()
        time.sleep(2)
        current_time = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
        pic_path = '.\\result\\image\\'+ '2017-07-17\\' + current_time + '.png'
        print (pic_path)
        self.driver.save_screenshot(pic_path)  #截圖測試結果
        self.assertIn1('selenium',self.driver.title)   #斷言書寫錯誤,導致case出錯

    def test_case3(self):
        '''設計測試成功的case'''
        print ("========【case_0003】 搜索夢雨情殤博客=============")
        self.driver.find_element_by_id("kw").clear()
        self.driver.find_element_by_id("kw").send_keys(u"夢雨情殤")
        self.driver.find_element_by_id('su').click()
        time.sleep(2)
        current_time = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
        pic_path = '.\\result\\image\\'+ '2017-07-17\\' + current_time + '.png'
        print (pic_path)
        self.driver.save_screenshot(pic_path)
        self.assertIn('夢雨情殤',self.driver.title)

    def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    '''生成測試報告'''
    current_time = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
    testunit = unittest.TestSuite()   #構建測試套件
    testunit.addTest(Baidu("test_case1"))
    testunit.addTest(Baidu("test_case2"))
    testunit.addTest(Baidu("test_case3"))
    report_path = ".\\result\\SoftTestReport_" + current_time  + '.html'  #生成測試報告的路徑
    fp = open(report_path, "wb")
    runner = HTMLTestReport.HTMLTestRunner(stream=fp, title=u"自動化測試報告",description='自動化測試演示報告',tester='夢雨')
    runner.run(testunit)
    fp.close()

過程很簡單,過程需要理解整個過程,HTMLTestReport代碼已經共享,請下載整體工程。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM