移動端自動化測試報告的生成之pytest

2021-09-29 09:06:29 字數 3139 閱讀 2553

首先我們需要在python裡面安裝

執行完之後我們pip list

這時我們會看到有pytest,說明已經安裝成功

我們需要建立二個包和乙個pytest.ini配置檔案

乙個用來存放我們生成的測試報告,乙個裡面我們寫測試用例

pytest.ini裡面配置檔案:

各個命令列引數 addopts = -s —reruns n —html=路徑/xx.html # 命令之間空格分隔

2.搜尋路徑:testpaths = 指令碼存放的路徑

3.搜尋檔名:python_files = test_*.py

4.搜尋測試類名:python_classes = test_*

5.搜尋方法名:python_functions = test_*

下面看一下**,隨便寫了個通訊錄的

from selenium.webdriver.support.wait import webdriverwait

def setup(self):

desired_caps = {}

desired_caps['platformname'] = 'android'

desired_caps['platformversion'] = '5.1'

desired_caps['devicename'] = '192.168.56.101:5554'

desired_caps['noreset'] = 'true'

desired_caps['unicodekeyboard'] = true

desired_caps['resetkeyboard'] = false

self.driver = webdriver.remote('', desired_caps)

def teardown(self):

pass

def waitxpath(self,xpath):

return webdriverwait(self.driver,5,0.5).until(lambda x:x.find_element_by_xpath(xpath))

def waitid(self,id):

return webdriverwait(self.driver,5,0.5).until(lambda x:x.find_element_by_id(id))

def test_001(self):

self.waitxpath("//*[contains(@text,'通訊錄')]").click()

self.waitid("com.android.contacts:id/floating_action_button").click()

self.waitxpath("//*[contains(@text,'姓名')]").send_keys("秀兒")

self.waitxpath("//*[contains(@text,'**')]").send_keys("123456")

這時我們不能像平常那樣單擊右鍵執行

我們需要

會出來這個路徑

我們需要輸入  使用:跟在命令列裡,pytest 測試檔案 —html=路徑/xx.html

執行完成後

會看到

我建立的是html資料夾下面會生成乙個html檔案

我們會用預設瀏覽器開啟

會生成這樣一種測試報告,整體流程就這樣

前面我們所說的配置pytest.ini檔案

如果配置了可以直接輸入pytest執行就可以生成測試報告

pytest:

1.安裝 pip3 install pytest

2.初始化和結束函式:

1.setup teardown : 在乙個類內部每個測試方法的開始和結束執行一次

2.setup_class,teardown_class:在乙個類內部只執行一次,不關心有多少測試方法

3.pytest外掛程式

1.pytest-html: 生成測試報告

安裝:pip3 install pytest-html

使用:跟在命令列裡,pytest 測試檔案 —html=路徑/xx.html

2.pytest-ordering: 控制測試函式執行順序

使用:@pytest.mark.run(order=x)

x:1.全為正數 或者 全為負數 值越小 優先順序越高,意味著最先執行

2.正數和負數同時存在,正數優先順序高

3.值為負數時,優先順序低於沒被標記的測試方法

4.值為正數時,優先順序高於沒被標記的測試方法

3.pytest-rerunfailures : 失敗測試函式重試機制

使用:在命令列引數中配置:—reruns n 

n:重試次數

4.配置檔案:

1.命令列引數 addopts = -s —reruns n —html=路徑/xx.html # 命令之間空格分隔

2.搜尋路徑:testpaths = 指令碼存放的路徑

3.搜尋檔名:python_files = test_*.py

4.搜尋測試類名:python_classes = test_*

5.搜尋方法名:python_functions = test_*

基本流程大概就這樣,後續會加上allure框架生成測試報告

自動化測試報告的生成

匯入htmltestrunner from htmltestrunner import htmltestrunner import unittest 用於識別測試用例 import time 用於生成測試報告名稱的字尾 識別得到要執行的測試用例 case path 測試用例檔案所在的父目錄 test...

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

生成html測試報告 from htmltestreport import htmltestrunner import os,sys import unittest import time dirname 當前目錄位置,filename 該py名稱 dirname,filename os.path....

介面自動化之測試報告

寫好指令碼後,需要生成乙個測試報告 目錄1 安裝htmltestrunner 1 安裝htmltestrunner 第二步 匯入試試,沒報錯就沒問題 2 應用 在run all.py檔案中,第一步 我們可以呼叫discover方法,來找到所有的用例 如圖 discover方法,需要傳入3個引數,第乙...