Pytest框架集成Allure訂製測試報告詳解(一)
- 2020 年 2 月 19 日
- 筆記
Allure簡介
Allure是一款非常輕量級並且非常靈活的開源測試報告生成框架。它支援絕大多數測試框架, 例如TestNG、Pytest、JUint等。它簡單易用,易於集成。下面就Pytest如何與Allure集成做詳細介紹。
Pytest框架集成Allure
Pytest是Python的單元測試框架,非常方便和易用。強烈推薦對於用Python進行測試工作的小夥伴使用這個測試框架,相比與Python自帶的UnitTest好用太多太多。今天我們主要是介紹如何將測試報告生成工具Allure集成到Pytest中。目前現在已經有allure2了,我們要使用的就是這個allure2
之前我們寫了Pytest結合Allure生成測試報告的環境搭建: 【Pytest篇】Allure生成漂亮的HTML圖形化測試報告(一))
今天我們來寫一下測試報告的訂製詳解:
一、Features、Story訂製詳解
@allure.feature # 用於定義被測試的功能,被測產品的需求點,模組
@allure.story # 用於定義被測功能的用戶場景,即子功能點,用例
import pytest,os
import allure
class Test(object):
@allure.feature('登錄功能')
@allure.story('登錄成功')
def test_login(self):
assert 1 == 1
def test2(self):
assert 1==1
if __name__=="__main__":
#生成測試報告json
pytest.main(["-s", "-q", '--alluredir', 'report/result', 'test01.py'])
#將測試報告轉為html格式
split='allure '+'generate '+'./report/result '+'-o '+'./report/html '+'--clean'
os.system('cd C:/Users/wangli/PycharmProjects/Test/test/report')
os.system(split)
print(split)
"C:Program FilesPython35python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
..
2 passed in 0.06s
Report successfully generated to .reporthtml
allure generate ./report/result -o ./report/html --clean
Process finished with exit code 0


二、title用例標題和description用例描述訂製詳解
@allure.title(用例的標題)
@allure.description(用例的描述)
或用例描述也可寫成這樣
""" 這裡是登錄成功測試用例 """import pytest,osimport allureclass Test(object): @allure.feature('登錄功能') @allure.story('登錄成功') @allure.title('用例的標題')#用例的標題 @allure.severity('blocker') @allure.issue('https://www.baidu.com/')#添加許可權對應鏈接 @allure.testcase('https://www.baidu.com/')#添加用例對應鏈接 def test_login(self): """ 這裡是登錄成功測試用例 :return: """ assert 1 == 1 @allure.severity('critical') def test_01(self): assert 1==1 @allure.severity('normal') def test_02(self): assert 1==1 @allure.severity('minor') def test_03(self): assert 1==1 @allure.severity('trivial') def test_04(self): assert 1==1 if __name__=="__main__": #生成測試報告json pytest.main(["-s", "-q", '--alluredir', 'report/result', 'test01.py']) #將測試報告轉為html格式 split='allure '+'generate '+'./report/result '+'-o '+'./report/html '+'--clean' os.system('cd C:/Users/wangli/PycharmProjects/Test/test/report') os.system(split) print(split) "C:Program FilesPython35python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py.....5 passed in 0.09sReport successfully generated to .reporthtmlallure generate ./report/result -o ./report/html --cleanProcess finished with exit code 0 三、Severity訂製標記用例級別詳解根據測試用例的重要性劃分測試用例等級,如果沒指定等級,默認為normal級別 Allure中對嚴重級別的定義: 1、 Blocker級別:中斷缺陷(客戶端程式無響應,無法執行下一步操作)@allure.severity('blocker') 2、 Critical級別:臨界缺陷( 功能點缺失) @allure.severity('critical') 3、 Normal級別:普通缺陷(數值計算錯誤) @allure.severity('normal') 4、 Minor級別:次要缺陷(介面錯誤與UI需求不符) @allure.severity('minor') 5、 Trivial級別:輕微缺陷(必輸項無提示,或者提示不規範) @allure.severity('trivial') import pytest,osimport allureclass Test(object): @allure.feature('登錄功能') @allure.story('登錄成功') @allure.severity('blocker') def test_login(self): """ 這裡是登錄成功測試用例 :return: """ assert 1 == 1 @allure.severity('critical') def test_01(self): assert 1==1 @allure.severity('normal') def test_02(self): assert 1==1 @allure.severity('minor') def test_03(self): assert 1==1 @allure.severity('trivial') def test_04(self): assert 1==1 if __name__=="__main__": #生成測試報告json pytest.main(["-s", "-q", '--alluredir', 'report/result', 'test01.py']) #將測試報告轉為html格式 split='allure '+'generate '+'./report/result '+'-o '+'./report/html '+'--clean' os.system('cd C:/Users/wangli/PycharmProjects/Test/test/report') os.system(split) print(split) "C:Program FilesPython35python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py.....5 passed in 0.10sReport successfully generated to .reporthtmlallure generate ./report/result -o ./report/html --clean Process finished with exit code 0四、Step和attach訂製詳解allure.step("調用登錄"): # 將一個測試用例分成幾個步驟,將步驟列印到測試報告中allure.attach('帳號', '18221124104') # attach可以列印一些附加資訊import pytest,osimport allure @allure.feature('購物車功能') # feature定義功能class Test(object): @allure.story('加入購物車') # story定義用戶場景 def test_add_shopping_trolley(self): login('橙子', '登錄密碼') # 調用「步驟函數」 with allure.step("瀏覽商品"): # 將一個測試用例分成幾個步驟,將步驟列印到測試報告中,步驟2 allure.attach('商品1', 'NIKE球鞋') # attach可以列印一些附加資訊 allure.attach('商品2', '大眾速騰') with allure.step("點擊商品"): # 將一個測試用例分成幾個步驟,將步驟列印到測試報告中,步驟3 pass with allure.step("校驗結果"): allure.attach('期望結果', '添加購物車成功') allure.attach('實際結果', '添加購物車失敗') assert 'success' == 'failed' @allure.story('修改購物車') def test_edit_shopping_trolley(self): pass @pytest.mark.skipif(reason='本次不執行') @allure.story('刪除購物車') def test_delete_shopping_trolley(self): pass @allure.step('帳號登錄') # 還可以將一個函數作為一個步驟,調用此函數時,報告中輸出一個步驟,步驟名字通常是函數名,我把這樣的函數叫「步驟函數」def login(user, pwd): print(user, pwd)if __name__=="__main__": #生成測試報告json pytest.main(["-s", "-q", '--alluredir', 'report/result', 'test01.py']) #將測試報告轉為html格式 split='allure '+'generate '+'./report/result '+'-o '+'./report/html '+'--clean' os.system('cd C:/Users/wangli/PycharmProjects/Test/test/report') os.system(split) print(split) "C:Program FilesPython35python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py橙子 登錄密碼F.s================================== FAILURES ===================================_______________________ Test.test_add_shopping_trolley ________________________ self = <test.test01.Test object at 0x0000024F3425C898> @allure.story('加入購物車') # story定義用戶場景 def test_add_shopping_trolley(self): login('橙子', '登錄密碼') # 調用「步驟函數」 with allure.step("瀏覽商品"): # 將一個測試用例分成幾個步驟,將步驟列印到測試報告中,步驟2 allure.attach('商品1', 'NIKE球鞋') # attach可以列印一些附加資訊 allure.attach('商品2', '大眾速騰') with allure.step("點擊商品"): # 將一個測試用例分成幾個步驟,將步驟列印到測試報告中,步驟3 pass with allure.step("校驗結果"): allure.attach('期望結果', '添加購物車成功') allure.attach('實際結果', '添加購物車失敗')> assert 'success' == 'failed'E AssertionError test01.py:51: AssertionError1 failed, 1 passed, 1 skipped in 0.18sReport successfully generated to .reporthtmlallure generate ./report/result -o ./report/html --clean Process finished with exit code 0五、Issue缺陷鏈接和TestCase用例鏈接訂製詳解@allure.issue() 缺陷 對應缺陷管理系統裡面的鏈接,在測試報告中可以點擊跳轉的 @allure.testcase() 測試用例的鏈接地址 對應功能測試用例系統裡面的case鏈接,在測試報告中可以點擊跳轉的 import pytest,osimport allureclass Test(object): @allure.feature('登錄功能') @allure.story('登錄成功') @allure.severity('blocker') @allure.issue('https://www.baidu.com/')#添加缺陷對應鏈接 @allure.testcase('https://www.baidu.com/')#添加用例對應鏈接 def test_login(self): """ 這裡是登錄成功測試用例 :return: """ assert 1 == 1 @allure.severity('critical') def test_01(self): assert 1==1 @allure.severity('normal') def test_02(self): assert 1==1 @allure.severity('minor') def test_03(self): assert 1==1 @allure.severity('trivial') def test_04(self): assert 1==1 if __name__=="__main__": #生成測試報告json pytest.main(["-s", "-q", '--alluredir', 'report/result', 'test01.py']) #將測試報告轉為html格式 split='allure '+'generate '+'./report/result '+'-o '+'./report/html '+'--clean' os.system('cd C:/Users/wangli/PycharmProjects/Test/test/report') os.system(split) print(split) "C:Program FilesPython35python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py.....5 passed in 0.05sReport successfully generated to .reporthtmlallure generate ./report/result -o ./report/html --clean Process finished with exit code 0五、link鏈接訂製詳解@allure.link(『https://www.baidu.com/』)六、attachment附件制定@allure.attachment()
