Selenium自動化測試 生成測試報告

2021-09-14 08:31:52 字數 1897 閱讀 3712

生成html測試報告

from htmltestreport import htmltestrunner

import os,sys

import unittest

import time

#dirname=當前目錄位置,filename=該py名稱

dirname,filename=os.path.

split

(os.path.

abspath

(sys.ar**[0]

)) #通過defaulttestloader來載入當前目錄下所有名稱為test開頭的py

discover = unittest.defaulttestloader.

discover

(dirname, pattern=

'test*.py'

, top_level_dir=none)

# 獲取系統當前時間

now = time.

strftime

('%y-%m-%d-%h_%m_%s'

, time.

localtime

(time.

time()

))day = time.

strftime

('%y-%m-%d'

, time.

localtime

(time.

time()

))# 定義個報告存放路徑,支援相對路徑

tdresult =

report_path

+"\\"

+ day

if os.path.

exists

(tdresult)

: # 檢驗資料夾路徑是否已經存在

filename = tdresult +

"\\"

+ now +

"_result.html"

fp =

open

(filename,

'wb'

) # 定義測試報告

runner =

htmltestrunner

(stream=fp,

title=

'測試報告'

, description=

'執**況:'

) # 執行測試用例

runner.

run(discover)

fp.close

() # 關閉報告檔案

else

: os.

mkdir

(tdresult) # 建立測試報告資料夾

filename = tdresult +

"\\"

+ now +

"_result.html"

fp =

open

(filename,

'wb'

) # 定義測試報告

runner =

htmltestrunner

(stream=fp,

title=

'selenium測試報告'

, description=

'執**況:'

) # 執行測試用例

runner.

run(discover)

fp.close

() # 關閉報告檔案

本文章使用的生成報告樣板是優化過的測試報告

selenium 自動化測試。

第一步安裝 谷歌瀏覽器。第三步編寫測試測試指令碼 第四步 安裝selemium coding utf 8 引入webdriver和unittest所需要的包 from selenium import webdriver from selenium.webdriver.common.by import...

Python自動化測試 Selenium

第乙個selenium就。先show from selenium import webdriver driver webdriver.firefox driver.get assert oxcoder decode utf 8 in driver.title print driver.title d...

自動化測試selenium(四)

三 用例的執行順序 四 unittest斷言 unittest 各元件的關係為 test fixture 初始化和清理測試環境,比如建立臨時的資料庫,檔案和目錄等,其中 setup 和 setdown 是最常用的方法 test case 單元測試用例,testcase 是編寫單元測試用例最常用的類 ...