文章目錄
- 一、企業級的Allure報告的定制
- 左邊的定制:
- 右邊的定制:
- 1.用例的嚴重程度/優先級
- 2.用例描述
- 3.測試用例連接的定制
- 4.測試用例步驟的定制
- 5.附件的定制
- 二、企業中真實的定制有哪些?
- 三、allure報告如何在本地訪問
- 四、allure中的數據驅動裝飾器
- 第一種用法
- 第二種用法
- 第三種用法
- YAML的數據文件:
- YAML數據驅動實現:
一、企業級的Allure報告的定制
左邊的定制:
1.史詩(項目名稱):@allure.epic(“項目名稱:接口自動化測試”)
2.特性(模塊名稱):@allure.feature(“模塊名稱:用戶模塊”)
3.分組(接口名稱):@allure.story(“接口名稱:查詢用戶”)
4.測試用例標題:
- @allure.title(“測試用例標題:輸入正確的條件匹配成功”),適用于一個方法對
應一個用例。 - allure.dynamic.title(“測試用例標題:輸入正確的條件匹配成功”),適用于一個
方法對應多個用例。也就是有數據驅動的情況。
import allure
import pytest@allure.epic("項目名稱:接口自動化測試")
@allure.feature("模塊名稱:用戶模塊")
class User:@allure.story("接口名稱:查詢用戶")@allure.title("測試用例標題:輸入正確的條件匹配成功")def test_user(self):# allure.dynamic.title("測試用例標題:輸入正確的條件匹配成功")print("test_user" )assert 'abc' in 'abcd'
右邊的定制:
1.用例的嚴重程度/優先級
- blocker:中斷缺陷&致命bug:內存泄漏,用戶數據丟失,系統奔潰。
- critical:臨界缺陷&嚴重bug:功能未實現,功能錯誤,重復提交
- normal:一般缺陷&一般bug,條件查詢有誤,大數據了無響應等
- minor:次要缺陷:提示bug,顏色搭配不好,字體排列不整齊,錯別字。
- trivial:輕微缺陷:輕微bug,沒有使用專業術語,必填項無提示,建議。
@allure.severity(allure.severity_level.BLOCKER)
注意:
這個裝飾器可以修飾方法也可以修飾類。
2.用例描述
和用例標題一樣,有兩種寫法:@allure.description("")
allure.dynamic.description("")
import allure
import pytestclass TestUser:@allure.description("用戶測試用例描述")@pytest.mark.userdef test_get_userinfo(self):# allure.dynamic.description("用戶測試用例描述2")print("get_userinfo" )assert 'abc' in 'abcd'
3.測試用例連接的定制
- 接口地址:
- Bug地址:
- 測試用例的地址:
import allure
import pytestclass TestUser:@allure.link(name="接口地址", url="https://api.weixin.qq.com/cgi‐bin/token")@allure.issue(name="Bug連接", url="https://www.zentao.net/")@allure.testcase(name="測試用例地址", url="https://www.zentao.net/")def test_get_userinfo(self):print("get_userinfo" )assert 'abc' in 'abcd'
4.測試用例步驟的定制
有兩種寫法:
@allure.step("")
:不建議使用,不靈活,只能傳入一個title值,不能寫多個步驟with allure.step("")
:推薦使用,比較靈活
import allure
import pytestclass TestUser:# @allure.step("測試步驟") 不建議使用,不靈活,只能傳入一個title值,不能寫多個步驟@pytest.mark.userdef test_get_userinfo(self):# 增加測試步驟-建議使用for a in range(1, 5):with allure.step("測試用例步驟" + str(a) + ""):print("步驟" + str(a) + "執行的腳本")print("get_userinfo" )assert 'abc' in 'abcd'
5.附件的定制
body=附件內容, name=None文件名, attachment_type=None文件擴展名
- web自動化
# web自動化
with open(r"./screenshots/logo.png", mode="rb") as f:allure.attach(body=f.read(), name="用戶測試錯誤截圖",attachment_type=allure.attachment_type.PNG)
- 接口自動化
#接口自動化
allure.attach(body="https://api.weixin.qq.com/cgi‐bin/token", name="請求地址:",attachment_type=allure.attachment_type.TEXT)allure.attach(body="get", name="請求方式:", attachment_type=allure.attachment_type.TEXT)data = {"grant_type": "client_credential","appid": "wx6b11b3efd1cdc290","secret": "106a9c6157c4db5f6029918738f9529d"}allure.attach(body=json.dumps(data), name="請求數據:", attachment_type=allure.attachment_type.TEXT)rep = requests.get(url="https://api.weixin.qq.com/cgi‐bin/token", params=data)allure.attach(body=str(rep.status_code)+rep.text, name="響應數據:", attachment_type=allure.attachment_type.TEXT)
二、企業中真實的定制有哪些?
- 1.@allure.epic(“項目名稱”)
- 2.@allure.feature(“模塊名稱”)
- 3.@allure.story(“接口名稱”)
- 4.@allure.severity(allure.severity_level.BLOCKER) 嚴重程度
- 5.allure.dynamic.title(“用例名稱:測試用例名稱”)
- 6.allure.dynamic.description(“用例描述:測試用例描述”)
- 7.with allure.step(“測試步驟的名稱”)
- 8.allure.attach(body, name, attachment_type, extension) 測試用例附件
7與8一般會進行封裝,后期講解
三、allure報告如何在本地訪問
因為pycharm自帶容器:tomcat,Nginx,weblogic。有以下兩種方式實現本地訪問
- 1.在本地搭建本地服務器。
- 2.通過啟動服務打開allure報告。(簡單)
allure open [報告路徑]
四、allure中的數據驅動裝飾器
@pytest.mark.parametrize(參數名,數據(list,tuple,字典列表,字典元祖))
第一種用法
@allure.story("接口名稱:測試數據驅動")
@pytest.mark.parametrize("args_name",["無憂渡","藏海傳","折腰"])
@pytest.mark.user
def test_get_data(self,args_name):print(args_name)
第二種用法
@allure.story("接口名稱:測試數據驅動")
@pytest.mark.parametrize("order,name",[["01","《無憂渡》"],["04","《藏海傳》"],["03","《折腰》"]])
@pytest.mark.user
def test_get_data(self,order,name):print("序號:"+order+"劇名:"+name)
第三種用法
使用yaml
數據進行數據驅動
YAML有兩種數據:
-
開頭的代碼list- 鍵值對:key:value
YAML的數據文件:
-name: get correct user tokendescription: When trying to obtain a user token with a valid appid, correct secret, and correct grant_type, the request will succeed.request:url: https://api.weixin.qq.com/cgi-bin/tokenmethod: GETdata:appid: wx74a8627810cfa309secret: e40a02f9d79a8097df497e6aaf93ab81grant_type: client_credentialvalidate: None-name: don't get correct user tokendescription: When trying to obtain a user token with an empty appid, correct secret, and correct grant_type, an error occurs.request:url: https://api.weixin.qq.com/cgi-bin/tokenmethod: GETdata:appid:secret: e40a02f9d79a8097df497e6aaf93ab81grant_type: client_credentialvalidate: None-name: don't get correct user tokendescription: When trying to obtain a user token with an correct appid, error secret, and correct grant_type, an error occurs.request:url: https://api.weixin.qq.com/cgi-bin/tokenmethod: GETdata:appid: wx74a8627810cfa308secret: e40a02f9d79a8097df497e6aaf93ab81grant_type: client_credentialvalidate: None-name: don't get correct user tokendescription: When trying to obtain a user token with an correct appid, correct secret, and empty grant_type, an error occurs.request:url: https://api.weixin.qq.com/cgi-bin/tokenmethod: GETdata:appid: wx74a8627810cfa308secret: e40a02f9d79a8097df497e6aaf93ab81grant_type:validate: None
YAML數據驅動實現:
注:
記得安裝 PyYAML
庫
# -*- coding: utf-8 -*-
import json
import allure
import pytest
import requests
import yaml# 讀取 yaml 文件
def read_yaml(path):with open(path,mode="r",encoding="utf-8") as f:value = yaml.load(f,Loader=yaml.FullLoader)return value@allure.epic("項目名稱:接口自動化測試")
@allure.feature("模塊名稱:用戶模塊")
class TestUser:@allure.story("接口名稱:獲取用戶token")@allure.severity(allure.severity_level.BLOCKER)@pytest.mark.user@pytest.mark.parametrize("case_info",read_yaml("./testcases/user_manage/get_token.yml"))def test_get_user_token(self,case_info):print(case_info)allure.dynamic.title(case_info['name'])allure.dynamic.description(case_info['description'])allure.attach(body=case_info['request']['url'],name="請求地址:",attachment_type=allure.attachment_type.TEXT)allure.attach(body=case_info['request']["method"],name="請求方式:",attachment_type=allure.attachment_type.TEXT)data = case_info['request']["data"]allure.attach(body=json.dumps(data),name="請求數據:",attachment_type=allure.attachment_type.TEXT)rep = requests.get(url=case_info['request']['url'],params=data)allure.attach(body=str(rep.status_code) + rep.text,name="響應數據:",attachment_type=allure.attachment_type.TEXT)