接口自動化-allure的使用


首先,最好的參考應該是官方文檔:http://allure.qatools.ru/

1. report界面:總覽頁面、缺陷頁面、xunit單元測試頁面、行為驅動開發頁面、視圖頁面;

2. 安裝配置:
windows: 解壓到項目文件夾下,運行allure.bat添加環境變量,cmd: allure --version檢查是否添加環境變量成功。

3. allure的工作原理。
3.1 解壓縮allure,添加環境變量,安裝allure-pytest程序包,運行test*.py(pytest自動運行里面test開頭的方法),同時生成與Allure 2兼容的報告數據,xml格式。
3.2 allure serve xml報告文件夾路徑;生成html格式的allure報告,以在瀏覽器查看。


4.環境。
4.1
environment.properties文件,放置在xml報告目錄,allure生成的html報告總覽上會顯示測試環境內容:
Browser=Chrome
Browser.Version=63.0
Stand=Production
4.2
categories.json創建自定義缺陷分類,在生成報告之前將文件添加到目錄中
[
{
"name": "Ignored tests",
"matchedStatuses": ["skipped"]
},
{
"name": "Infrastructure problems",
"matchedStatuses": ["broken", "failed"],
"messageRegex": ".*bye-bye.*"
},
{
"name": "Outdated tests",
"matchedStatuses": ["broken"],
"traceRegex": ".*FileNotFoundException.*"
},
{
"name": "Product defects",
"matchedStatuses": ["failed"]
},
{
"name": "Test defects",
"matchedStatuses": ["broken"]
}
]
其中的測試狀態列表。可選默認值為:["failed", "broken", "passed", "skipped", "unknown"];

5. allure的配置使用(python中)

5.0 安裝:
pip install allure-pytest
這將安裝allure-pytest和allure-python-commons程序包,以生成與Allure 2兼容的報告數據;
5.1 指定測試報告的生成路徑:(命令行不能運行時pytest加環境變量)
pytest --alluredir=/tmp/my_allure_results
5.2 生成html報告:
allure serve /tmp/my_allure_results
5.3 上面的在python導入pytest環境的情況下,也可以如下寫:


if __name__ == "__main__":

# pytest.main(['test_1.py'])

# 生成測試報告---json格式
pytest.main(['--alluredir', 'D:/se_frame/Reports/allure_data', 'test_1.py'])
# allure轉換成---html並打開測試報告
os.system('cd D:/se_frame/Reports/allure_data')
# 清理上次的報告並生成新的報告
os.system('allure generate D:/se_frame/Reports/allure_data -o D:/se_frame/Reports/html --clean')
os.system('allure serve D:/se_frame/Reports/allure_data')
————————————————
版權聲明:本文為CSDN博主「老_焦」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_45451320/article/details/113916870


#

6. allure常用裝飾器

@allure.feature #主題名,大的功能點,比如用在一個類上面
@allure.story # 用故事標題,小的用例標題,用於行為驅動開發頁面查看
@allure.description(str)# 詳細的測試用例描述;python中也可以用多行注釋代替;
@allure.title # 用例標題,設置顯示的方法名;在單元測試頁面查看;
@allure.severity(allure.severity_level.BLOCKER) # 設置用例優先級;
@allure.link # 設置可以點擊查詢訪問的鏈接;

7. 參考他人實例:
7.1
@allure.feature('風險分析')
class Testriskesti_V2():

@allure.story("風險分析--無風險-行為驅動頁面標題")
@allure.title("風險分析--無風險-單元測試頁面標題:")
@pytest.mark.nomal
@allure.severity(allure.severity_level.CRITICAL) # 嚴重
def test_01(self):
'''
用例標題: 風險分析--無風險
前置條件: ***
斷言:
1 / responseCode = 200
2 / 業務返回code = 0
'''
。。。
7.2

import pytest
import allure
# allure generate --clean report 生成測試報告
@allure.feature('購物車功能') # 用feature說明產品需求,可以理解為JIRA中的Epic
class TestShoppingTrolley(object):
@allure.story('加入購物車') # 用story說明用戶場景,可以理解為JIRA中的Story
def test_add_shopping_trolley(self):
login('劉春明', '密碼') # 步驟1,調用“step函數”
with allure.step("瀏覽商品"): # 步驟2,step的參數將會打印到測試報告中
allure.attach('筆記本', '商品1') # attach可以打印一些附加信息
allure.attach('手機', '商品2')
with allure.step("點擊商品"): # 步驟3
pass
with allure.step("校驗結果"): # 步驟4
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('用戶登錄') # 將函數作為一個步驟,調用此函數時,報告中輸出這個步驟,我把這樣的函數叫“step函數”
def login(user, pwd):
print(user, pwd)

————————————————
版權聲明:本文為CSDN博主「凱耐」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_36279318/article/details/105117073

7.3

import pytest
import allure
@allure.feature("這是登錄模塊測試用例")
class Test_login():
@allure.story("用戶名正確,登錄成功")
@allure.severity(allure.severity_level.BLOCKER) #阻塞
def test_logina(self):
allure.attach("這是一個純文本",name="文本信息",attachment_type=allure.attachment_type.TEXT) #添加文本
print("這是登錄,用戶名正確,登錄成功")
pass

@allure.story("密碼正確,登錄成功")
@allure.severity(allure.severity_level.CRITICAL) #嚴重
def test_loginb(self):
allure.attach("<body>這是一個網頁</body>",name="HTML測試模塊",attachment_type=allure.attachment_type.HTML) #添加網頁

print("這是登錄,密碼正確,登錄成功")
pass

@allure.story("用戶名錯誤,登錄失敗")
# --allure-link-pattern=issue:https://blog.csdn.net/weixin_44275820/article/details/105169871/issue/{}
@allure.issue("10086","這是一個bug,需要修復")
@allure.severity(allure.severity_level.NORMAL) #正常問題
def test_loginc(self):
allure.attach.file("./picture/微信頭像.jpg",name="這是一個圖片",attachment_type=allure.attachment_type.JPG) #添加圖片
print("這是登錄,用戶名錯誤,登錄失敗")
pass

@allure.story("密碼錯誤,登錄失敗")
@allure.link("https://blog.csdn.net/weixin_44275820/article/details/105169871",name="我的博客")
@allure.severity(allure.severity_level.MINOR) #不太重要
def test_logind(self):
with allure.step("點擊用戶名輸入框"):
print("輸入用戶名")
with allure.step("點擊輸入密碼輸入框"):
print("輸入密碼")
print("點擊登錄按鈕")
with allure.step("點擊登錄后登錄失敗"):
assert "1" == 1
print("這是登錄,密碼錯誤,登錄失敗")
pass

Testcase_link = "https://blog.csdn.net/weixin_44275820/article/details/105169871"
@allure.story("用戶不存在,登錄失敗")
@allure.testcase(Testcase_link,"我的博客管理平台")
@allure.severity(allure.severity_level.TRIVIAL) #不重要
def test_logine(self):
print("這是登錄,用戶不存在,請重新注冊")
pass

@allure.story("密碼已鎖定,登錄失敗")
def test_loginf(self):
print("這是登錄,密碼已鎖定,請重置密碼")
pass

@allure.story("密碼為空,登錄失敗")
def test_loging(self):
print("這是登錄,密碼為空,請輸入密碼")
pass

if __name__ =='__main__':
pytest.main("-v -s")
————————————————
版權聲明:本文為CSDN博主「Detail-L」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_44275820/article/details/105169996

7.4

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("C:\\Users\liwenliang\AppData\Local\Google\Chrome\Application\chrome.exe")
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")
————————————————
版權聲明:本文為CSDN博主「Detail-L」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_44275820/article/details/105169996

 

-----------------------------------------------------------------------------------------

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM