一、allure的介紹
二、allure報告概覽
https://demo.qameta.io/allure/#
三、allure報告安裝
下圖為mac安裝后的截圖:
四、使用allure2生成更加精美的測試報告
安裝allure-pytest插件:
pip install allure-pytest (安裝allure生成測試報告)
運行:
在測試執行期間收集結果:
pytest --allure=指定路徑 (指定allure報告數據生成路徑)
查看測試報告:
方法1:
pytest serve 報告路徑 (生成html報告,會直接打開在線報告)
方法2:
allure generate ./result//5 -o ./report/5/ --clean(指定生成報告的路徑)
allure open -h 127.0.0.1 -p 8888 ./report/5 (啟動本地服務生成鏈接查看報告)
收集測試結果:
方法1生成測試報告:
方法2生成測試報告:
五、allure用例描述
使用方法 | 參數值 | 參數說明 |
---|---|---|
@allure.epic() | epic描述 | 敏捷里面的概念,定義史詩,往下是feature |
@allure.feature() | 模塊名稱 | 功能點的描述,往下是story |
@allure.story() | 用戶故事 | 用戶故事,往下是title |
@allure.title(用例的標題) | 用例的標題 | 重命名html報告名稱 |
@allure.step() | 操作步驟 | 測試用例的步驟 |
@allure.testcase() | 測試用例的鏈接地址 | 對應功能測試用例系統里面的case |
@allure.issue() | 缺陷 | 對應缺陷管理系統里面的鏈接 |
@allure.description() | 用例描述 | 測試用例的描述 |
@allure.severity() | 用例等級 | blocker,critical,normal,minor,trivial |
@allure.link() | 鏈接 | 定義一個鏈接,在測試報告展現 |
@allure.attachment() | 附件 | 報告添加附件 |
1)@allure.feature與@allure.story的關系
feature相當於一個功能,一個大的模塊,將case分類到某個feature中,報告在behaviour中顯示,相當於testsuite
story相當於這個功能或者模塊下的不同場景,分支功能,屬於feature之下的結構,報告在features中顯示,相當於testcase
feature與story類似於父與子關系
import pytest import allure @allure.feature("登錄模塊") class Test_login(): @allure.story("用戶名正確,登錄成功") def test_logina(self): print("這是登錄,用戶名正確,登錄成功") pass @allure.story("密碼正確,登錄成功") def test_loginb(self): print("這是登錄,密碼正確,登錄成功") pass
2)@allure.step()與with allure.step():的區別
測試過程中的每個步驟,一般放在具體邏輯方法中
可以放在關鍵步驟中,在報告中顯示
在app,web自動化測試中,建議每切換到一個新的頁面當做一個step
用法:
@allure.step()只能以裝飾器的形式放在類或者方法上面
with allure.step(): 可以放在測試用例方法里,但測試步驟代碼需要被該語句包含
import pytest import allure @allure.feature("登錄模塊") class Test_login(): @allure.step("這是測試步驟1") def test_logina(self): print("這是登錄,用戶名正確,登錄成功") with allure.step("這是新的步驟1"): print("step步驟1測試") with allure.step("這是新的步驟2"): print("step步驟2測試") pass if __name__ == '__main__': pytest.main("-v -s")
3)allure關聯issuce和testcase
關聯測試用例(可以直接給測試用例的地址鏈接)
關聯bug:執行的時候需要加參數 --allure-link-pattern=issue:http://www.mytesttracker.com/issue/{}
import pytest import allure @allure.feature("登錄模塊") class Test_login(): @allure.issue('140','test with issue link') def test_with_issue_link(self): print("測試關聯bug") pass @allure.testcase("https://www.baidu.com") def test_with_testcase_link(self): print("測試關聯testcase") pass if __name__ == '__main__': pytest.main("-v -s")
4)給測試用例划分優先級
場景:
通常測試有P0、冒煙測試、驗證上線測試。按重要性級別來分別執行,比如上線要把主流程和重要模塊都跑一遍。
解決:
a、通過附加pytest.mark標記
b、通過allure.feature,allure.story標記
c、也可以通過allure.severity來附加標記
級別:Trivial:不重要 Minor:不太重要 Normal:正常問題 Critical:嚴重 Blocker:阻塞
步驟:在方法、函數、類上面加上
@allure.severity(allure.severity_level.TRIVAL)
執行時
pytest -s -v 文件名 --allure-severities normal,critical
5)給allure報告添加內容(圖片、附件、文本、截圖、HTML等)
場景:
前端自動化測試經常需要附加圖片或html,在適當的地方,適當的時機截圖
解決:
@allure.attach顯示許多不同類型的提供的附件,可以補充測試、步驟或測試結果
步驟:
import pytest import allure @allure.feature("登錄模塊") class Test_login(): def test_attach_html(self): print("在測試報告里面附加網頁信息") allure.attach('<head></head><body>首頁</body>', '這是錯誤頁的結果信息', allure.attachment_type.HTML) pass def test_attach_text(self): print("在測試報告里面附加文本信息") allure.attach("這是文本信息","文本",allure.attachment_type.TEXT) pass def test_attach_file(self): print("在測試報告里面附加圖片") allure.attach.file("./result/test.png",attachment_type=allure.attachment_type.PNG) if __name__ == '__main__': pytest.main("-v -s")
在測試報告里加網頁
在測試報告里附加圖片
在測試報告里添加文本
六、實例
1、實戰1
import pytest import allure @allure.feature("登錄模塊") class Test_login(): @allure.story("用戶名正確,登錄成功") def test_logina(self): print("這是登錄,用戶名正確,登錄成功") pass @allure.story("密碼正確,登錄成功") def test_loginb(self): print("這是登錄,密碼正確,登錄成功") pass @allure.story("用戶名錯誤,登錄失敗") def test_loginc(self): print("這是登錄,用戶名錯誤,登錄失敗") pass @allure.story("密碼錯誤,登錄失敗") def test_logind(self): print("密碼錯誤,登錄失敗") pass if __name__ == '__main__': pytest.main("-v -s")
(1)指定運行的feture
pytest test.py -s -q --alluredir=./result/ --allure-features '登錄模塊'
(2)指定運行的story
pytest test.py -s -q --alluredir=./result/ --allure-stories '用戶名正確,登錄成功'
2、實戰2
import pytest import allure import time from selenium import webdriver Testcase_link1 = "https://www.baidu.com" @allure.testcase(Testcase_link1,"百度,你值得擁有") @allure.feature("百度搜索") @pytest.mark.parametrize("search_data",["奔馳","寶馬","保時捷"]) def test_search(search_data): with allure.step("打開百度網頁"): driver = webdriver.Chrome("/Users/grace/selenium/driver/88/chromedriver") driver.get("https://www.baidu.com") with allure.step(f"輸入搜索詞{Testcase_link1}"): driver.find_element_by_id("kw").send_keys(search_data) time.sleep(3) driver.find_element_by_id("su").click() time.sleep(3) with allure.step("保存圖片"): driver.save_screenshot("./result/b.png") allure.attach.file("./result/b.png",name="這是保存的圖片",attachment_type=allure.attachment_type.PNG) with allure.step("關閉瀏覽器"): driver.quit() if __name__ =='__main__': pytest.main("-v -s")
aallure報告結果: