日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Pytest框架集成Allure定制测试报告详解(一)

發布時間:2025/3/15 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Pytest框架集成Allure定制测试报告详解(一) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Allure簡介
Allure是一款非常輕量級并且非常靈活的開源測試報告生成框架。 它支持絕大多數測試框架, 例如TestNG、Pytest、JUint等。它簡單易用,易于集成。下面就Pytest如何與Allure集成做詳細介紹。

Pytest框架集成Allure
Pytest是Python的單元測試框架,非常方便和易用。強烈推薦對于用Python進行測試工作的小伙伴使用這個測試框架,相比與Python自帶的UnitTest好用太多太多。今天我們主要是介紹如何將測試報告生成工具Allure集成到Pytest中。目前現在已經有allure2了,我們要使用的就是這個allure2

一、Features、Story定制詳解

@allure.feature # 用于定義被測試的功能,被測產品的需求點,模塊
@allure.story # 用于定義被測功能的用戶場景,即子功能點,用例

import pytest,os import allure class Test(object):@allure.feature('登錄功能')@allure.story('登錄成功')def test_login(self):assert 1 == 1def test2(self):assert 1==1 if __name__=="__main__":#生成測試報告jsonpytest.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 Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py .. 2 passed in 0.06s Report successfully generated to .\report\html allure generate ./report/result -o ./report/html --cleanProcess finished with exit code 0

二、title用例標題和description用例描述定制詳解

@allure.title(用例的標題)

@allure.description(用例的描述)

或用例描述也可寫成這樣

""" 這里是登錄成功測試用例 """ import pytest,os import allure class 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==1if __name__=="__main__":#生成測試報告jsonpytest.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 Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py ..... 5 passed in 0.09s Report successfully generated to .\report\html allure 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,os import allure class 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==1if __name__=="__main__":#生成測試報告jsonpytest.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 Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py ..... 5 passed in 0.10s Report successfully generated to .\report\html allure generate ./report/result -o ./report/html --cleanProcess finished with exit code 0

四、Step和attach定制詳解

allure.step("調用登錄"): # 將一個測試用例分成幾個步驟,將步驟打印到測試報告中

allure.attach('賬號', '18221124104') # attach可以打印一些附加信息

import pytest,os import allure@allure.feature('購物車功能') # feature定義功能 class Test(object):@allure.story('加入購物車') # story定義用戶場景def test_add_shopping_trolley(self):login('橙子', '登錄密碼') # 調用“步驟函數”with allure.step("瀏覽商品"): # 將一個測試用例分成幾個步驟,將步驟打印到測試報告中,步驟2allure.attach('商品1', 'NIKE球鞋') # attach可以打印一些附加信息allure.attach('商品2', '大眾速騰')with allure.step("點擊商品"): # 將一個測試用例分成幾個步驟,將步驟打印到測試報告中,步驟3passwith 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__":#生成測試報告jsonpytest.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 Files\Python35\python.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("瀏覽商品"): # 將一個測試用例分成幾個步驟,將步驟打印到測試報告中,步驟2allure.attach('商品1', 'NIKE球鞋') # attach可以打印一些附加信息allure.attach('商品2', '大眾速騰')with allure.step("點擊商品"): # 將一個測試用例分成幾個步驟,將步驟打印到測試報告中,步驟3passwith allure.step("校驗結果"):allure.attach('期望結果', '添加購物車成功')allure.attach('實際結果', '添加購物車失敗') > assert 'success' == 'failed' E AssertionErrortest01.py:51: AssertionError 1 failed, 1 passed, 1 skipped in 0.18s Report successfully generated to .\report\html allure generate ./report/result -o ./report/html --cleanProcess finished with exit code 0

五、Issue缺陷鏈接和TestCase用例鏈接定制詳解

@allure.issue()? ?缺陷? ? ?對應缺陷管理系統里面的鏈接,在測試報告中可以點擊跳轉的

@allure.testcase()??測試用例的鏈接地址? ? 對應功能測試用例系統里面的case鏈接,在測試報告中可以點擊跳轉的

import pytest,os import allure class 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==1if __name__=="__main__":#生成測試報告jsonpytest.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 Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py ..... 5 passed in 0.05s Report successfully generated to .\report\html allure generate ./report/result -o ./report/html --cleanProcess finished with exit code 0

五、link鏈接定制詳解

@allure.link(‘https://www.baidu.com/’)

六、attachment附件制定

@allure.attachment()

?

?

總結

以上是生活随笔為你收集整理的Pytest框架集成Allure定制测试报告详解(一)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。