一、Allure介紹
Allure是一個輕量級,靈活的,支持多語言的測試報告工具;多平台的,奢華的report框架;
可以為dev/qa提供詳盡的測試報告、測試步驟、log;
也可以為管理層提供high level 統計報告;
java語言開發的,支持pytest、javascript,php,ruby等
可以集成jenkins
二、Allure安裝(windows/mac通用安裝方法):
https://github.com/allure-framework/allure2/releases 下載allure2.7.zip包
解壓-->進入bin目錄-->運行allure.bat
把bin目錄加入path環境變量
1、windows下安裝 Allure工具:
(1) 安裝JDK1.8+ ,配置環境變量
(https://jingyan.baidu.com/article/db55b609fa946e4ba20a2f56.html)
(2) 安裝Allure
解壓到后,進入bin目錄,運行allure.bat
添加allure到環境變量PATH
(https://www.cnblogs.com/haohuixiang/p/12627551.html)
2、Mac可以使用brew安裝:
brew install allure
文檔:http://docs.qameta.io/allure/#
三、pytest-allure插件
安裝allure-pytest插件:pip install allure-pytest
四、allure報告生成
在測試執行期間收集結果
pytest[測試文件] -s -q --alluredir=./result/ (--alluredir這個選項,用於指定存儲測試結果的路勁)
如: pytest test_alluredemo1.py --alluredir=./result/5
查看測試報告:
方法一:測試完成后查看實際結果,在線看報告,會直接打開默認瀏覽器展示當前報告
allure serve ./result/ (注意這里的serve書寫)
如: allure serve ./result/5
方法二:從結果生成報告,這是一個啟動tomcat服務,需要兩個步驟:生成報告,打開報告
生成報告:
allure generate ./result/ -o ./report/ --clean(注意路勁--clean)
如:allure generate ./result/5 -o ./report/5 --clean
打開報告:
allure open -h 127.0.0.1 -p 8883 ./report/
如:allure open -h 127.0.0.1 -p 8883 ./report/5
五、allure特性分析
場景:
希望在報告中看到測試功能,子功能或場景,測試步驟,包括測試附加信息
解決:
@Feature 、 @story 、 @step 、@attach
步驟:
import allure
功能上加@allure.feature("功能名稱")
子功能上加@allure.story("子功能名稱")
步驟上加@allure.step("步驟細節")
@allure.attach("具體文本信息"),需要附加的信息,可以是數據、文本、圖片、視頻、網頁
如果只測試登錄功能運行是時候可以加限制過濾:
pytest 文件名 --allure-features 購物車功能 --allure-stories 加入購物車 (注意這里--allure_features中間是下划線)
六、按feature、story運行
1、@allure.feature 與 @allure.store的關系
feature相當於一個功能,一個大的模塊,將case分類到某個feature中,報告中behaviors中顯示,相當於testsuite
story相當於對應這個功能或模塊下的不同場景,分支功能,屬於feature之下的結構,報告在features中顯示,相當於testcase
feature與story類似於父子關系
@allure.feature("測試登錄模塊")
class TestLogin:
@allure.story("測試成功的登錄場景")
def test_001(self): ....
@allure.story("測試失敗的登錄場景")
def test_002(self): ...
2、allure特性-step
測試過程中每個步驟,一般放在具體的邏輯方法中
可以放在關鍵步驟中,在報告中顯示
在app,web自動測試當中,建議每切換到一個新的頁面當做一個step
用法:
@allure.step() 只能以裝飾器的形式放在類或者方法上面
with allure.step():可以放在測試用例方法里面,但測試步驟的代碼需要被該語句包含
def test_003(self):
with allure.step("點擊我的自選tab"): ....
with allure.step("點擊股票選項"):
time.sleep(random.randint(1,3))
3、前端自動化測試
場景:
前端自動化測試經常需要添加圖片或html,在適當的地方,適當的時機截圖
解決:
@allure.attach顯示許多不同類型的提供的附件,可以補充測試,步驟或測試結果
步驟:
在測試報告里附加網頁:
allure.attach(body(內容),name,attachment_type,extension):
allure.attach('<head></head><body>首頁</body>','這是錯誤頁的結果信息',allure.attachment_type.HTML)
在測試報告里附加圖片:
allure.attach.file(source,name,attachment_type,extension):
allure.attach.file("./result/b.png",attachment_type=allure.attachment_type.PNG)
七、allure+pytest+selenium 實戰演示