pytest資料引數化 yaml的基本使用 Allure測試框架
pytest資料引數化
引數化使用
@pytest.mark.parametrize(argnames,argvalues)
argnames:要引數化的變數,string(逗號隔開),list,tuple
argvalues:引數化的值,list,list[tuple]
@pytest.mark.parametrize('a,b',[(1,2),(3,4)])
def test_demo1(a,b):
print(a,b)
# 使用tuple
@pytest.mark.parametrize(("a","b") ,[(1,2),(3,4)])
def test_demo2(a,b):
print(a,b)
# 使用list
@pytest.mark.parametrize(["a","b"],[(1,2),(3,4)])
def test_demo3(a,b):
print(a,b)
yaml資料引數化
先安裝yaml: pip install pyyaml
匯入:import yaml
格式:
實現list
-10
-20
-30
-40
-45
實現字典dict
key:value :中間加有空格
id: 1
name: "XIIX"
age: 18
yaml巢狀
-
id: 1
name: "XIIX"
age: 18
-
id: 2
name: "XIIX"
age: 18
互動yaml檔案
方法:
yaml.safe_load(open(“yaml檔案地址”))
獲取yaml引數化:
import yaml
@pytest.mark.parametrize("data",yaml.safe_load(open("./test_data.yaml")))
def test_param(data):
print(data[ "id"],data["name"],data["age"])
注意⚠️ :對於yaml.safe_load(open(“yaml檔案地址”)讀取的檔案如果是字典,只會讀取key值。
要想獲取對應的value值,建議把yaml檔案的資料寫成列表巢狀字典的格式。
例如:
-
id: 1
name: "XIIX"
age: 18
-
id: 2
name: "HaHa"
age: 19
Allure測試框架
1、allure介紹
2、allure安裝
3、pytest-allure外掛
4、Allure報告的生成
5、allure特性分析
6、按feature,story執行
7、allure + pytest + selenium實戰
allure介紹
一個輕量級,靈活,支援多語言的測試報告工具
allure安裝
windows和mac通用安裝方法
下載allure2.7.zip包 地址:https://github.com/allure-framework/allure2/releases
解壓 -> 進入bin目錄 -> 執行allure.bat
把bin目錄加入PATH環境變數
Mac可以使用brew
安裝:brew install allure
安裝brew:https://blog.csdn.net/yuanshangshenghuo/article/details/106599836
安裝時踩的坑: Error: Can’t create vendor-install-ruby lock in /usr/local/var/homebrew/locks! Fix permissions by running: sudo chown -R $(whoami) /usr/local/var/homebrew Error: Failed to install Homebrew Portable Ruby (and your system version is too old)! /
解決:https://blog.csdn.net/u012135425/article/details/89095183
官網地址:https://allure.qatools.ru/
文件地址:https://docs.qameta.io/allure/#
生成測試報告:
1、先安裝:pip install allure-pytest
2、pytest 用例檔案 --alluredir=生成報告的路徑 ( 執行測試用例,收集測試資料)
3、檢視報告
方式一:allure serve 生成報告的路徑 (生成測試用例 (報告會直接開啟))
方式二: 1、allure generate 報告地址 -o 報告地址 --clean (覆蓋路徑加–clean)
2、開啟報告: allure open -h 127.0.0.1 -p 8888 報告地址
Allure常用的特性
場景:
希望在報告中看到測試功能,子功能或者場景,測試步驟,包括測試附加資訊。
解決:
@Feature @story @step @attach
步驟:
1、import allure
2、功能上加@allure.feature(‘功能名稱’)
3、子功能上加@allure.story(‘子功能名稱’)
4、步驟上加@allure.step(‘步驟細節’)
5、@allure.attach(‘具體文字資訊’),需要附加資訊,可以是資料,文字,圖片,視訊,網頁
6、如果只測試登陸功能執行的時候可以加限制過濾:
pytest 檔名 --allure-features ‘購物車功能’ --allure-stories ‘加入購物車’
pytest 檔名 --allure-features ‘購物車功能’
pytest 檔名 --allure-stories ‘加入購物車’
⚠️ : 注意這裡–allure-features中間是中劃線,用例備註和前面有空格)
import allure
import pytest
@allure.feature("登陸模組")
class Test_login():
@allure.story("登陸成功")
def test_demo1(self):
print("demo1---登陸成功")
@allure.story("登陸失敗")
def test_demo2(self):
print("demo2 登陸失敗")
assert 1>3
@allure.story("輸入資訊")
def test_case(self):
with allure.step("輸入使用者名稱"):
print("輸入使用者名稱")
with allure.step("輸入密碼"):
print("輸入密碼")
with allure.step("點選登陸"):
print("點選登陸")
@pytest.mark.xfail
@allure.story("資料引數化")
@pytest.mark.parametrize('a,b', [(1, 2), (3, 4)])
def test_demo1(self,a, b):
print(a, b)
if __name__ == '__main__':
pytest.main()
issue和testcase
場景:關聯測試用例或者關聯BUG
1、關聯測試用例
@allure.issue(‘http://www.baidu.com’,name=“issue連結”) issue連線前面有個甲殼蟲
@allure.testcase(‘http://www.baidu.com’,name=“testcase連結”)
import allure
import pytest
@allure.issue('http://www.baidu.com',name="issue連結")
def test_demo():
print("測試issue")
@allure.testcase('http://www.baidu.com',name="testcase連結")
def test_demo1():
print("測試issue")
if __name__ == '__main__':
pytest.main()
2、關聯BUG,執行的時候前面要加個引數
–allure-link-pattern=issue:https://github.com/DeltaFarce?tab=stars (bug地址)
執行的時候一定要加上這個地址pytest test_issue_testcase.py --allure-link-patterissue:https://github.com/DeltaFarce?tab=stars{} --alluredir=./data/5
import allure
import pytest
@allure.issue('http://www.baidu.com',name="issue連結")
def test_demo():
print("測試issue")
@allure.testcase('http://www.baidu.com',name="testcase連結")
def test_demo1():
print("測試testcase")
# --allure-link-pattern=issue:https://github.com/DeltaFarce?tab=stars{}
@allure.issue('111','這是issue')
def test_demo2():
print("測試issue加關聯bug地址")
assert 2>3
if __name__ == '__main__':
pytest.main()
按重要級別進行一定範圍的測試
場景
解決
步驟
執行:pytest test_leve.py --alluredir=./data/6 --allure-verities normal,blocker (注意:後面的驗證等級必須小寫)
import allure
import pytest
@allure.severity(allure.severity_level.TRIVIAL)
def test_demo1():
print("測試issue")
@allure.severity(allure.severity_level.NORMAL)
def test_demo2():
print("測試issue")
@allure.severity(allure.severity_level.BLOCKER)
def test_demo3():
print("測試issue")
@allure.severity(allure.severity_level.CRITICAL)
def test_demo4():
print("測試issue")
if __name__ == '__main__':
pytest.main()
在pycharm中如果配置嚴重程度直接執行用例
前端自動化測試 - 截圖
場景:
解決:
步驟:
import allure
def test_attach_text():
allure.attach("這是一個純文字",attachment_type=allure.attachment_type.TEXT)
def test_attach_html():
allure.attach('<h1>這是一個h1程式碼塊</h1>',attachment_type=allure.attachment_type.HTML)
def test_attch_img():
allure.attach.file('/Users/sunxinyang/PycharmProjects/OS_Modules/1.png',
name="風景如畫",attachment_type=allure.attachment_type.JPG)
注意⚠️ :對於圖片前面使用了file方法allure.attach.file()