目錄
1、動態生成標題
1.1、示例一:引數化無標題
1.2、示例二:引數化有標題
1.3、示例三:引數化使用ids
1.4、示例四:引數化動態生成標題
1.5、示例五:引數化動態生成標題優化
2、動態生成功能
2.1、示例一:allure.dynamic.title()
2.2、示例二:allure.dynamic.description()
2.3、示例三:結合@pytest.mark.parametrize()
2.4、示例四:全部方法示例
3、報告添加用例失敗截圖
1、動態生成標題
默認 allure 報告上的測驗用例標題不設定就是用例名稱,其可讀性不高;當結合 @pytest.mark.parametrize 引數化完成資料驅動時,如標題寫死,其可讀性也不高,
那如果希望標題可以動態的生成,采取的方案是:
引數化 @pytest.mark.parametrize + @allure.title()
1.1、示例一:引數化無標題
1、創建test_allure_title_parametrize.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
import pytest
import allure
@pytest.fixture()
def login(request):
"""登錄"""
param = request.param
print(f"用戶名:{param['username']},密碼:{param['password']}")
# 回傳
return {"code": 0, "msg": "登陸成功"}
datas = [
{"username": "name1", "password": "pwd1"},
{"username": "name2", "password": "pwd2"},
{"username": "name3", "password": "pwd3"}
]
@allure.story('登錄功能')
@pytest.mark.parametrize('login', datas, indirect=True)
def test_login(login):
"""
登錄測驗用例
"""
assert login['code'] == 0
2、輸入命令運行:
pytest test_allure_title_parametrize.py --alluredir=./allure
allure serve allure
如圖所示:用例標題就是函式名+引數化的資料

1.2、示例二:引數化有標題
1、創建test_allure_title_parametrize2.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
import pytest
import allure
@pytest.fixture()
def login(request):
"""登錄"""
param = request.param
print(f"用戶名:{param['username']},密碼:{param['password']}")
# 回傳
return {"code": 0, "msg": "登陸成功"}
datas = [
{"username": "name1", "password": "pwd1"},
{"username": "name2", "password": "pwd2"},
{"username": "name3", "password": "pwd3"}
]
@allure.story('登錄功能')
@allure.title('登錄測驗用例')
@pytest.mark.parametrize('login', datas, indirect=True)
def test_login(login):
"""
登錄測驗用例
"""
assert login['code'] == 0
2、輸入命令運行:
pytest test_allure_title_parametrize2.py --alluredir=./allure
allure serve allure
如圖所示:引數化的三條測驗用例都使用同一個title

1.3、示例三:引數化使用ids
1、創建test_allure_title_parametrize3.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
import pytest
import allure
@pytest.fixture()
def login(request):
"""登錄"""
param = request.param
print(f"用戶名:{param['username']},密碼:{param['password']}")
# 回傳
return {"code": 0, "msg": "登陸成功"}
datas = [
{"username": "name1", "password": "pwd1"},
{"username": "name2", "password": "pwd2"},
{"username": "name3", "password": "pwd3"}
]
ids = [
"name1,pwd1",
"name2,pwd2",
"name3,pwd3"
]
@allure.story('登錄功能')
@pytest.mark.parametrize('login', datas, ids=ids, indirect=True)
def test_login(login):
"""
登錄測驗用例
"""
assert login['code'] == 0
2、輸入命令運行:
pytest test_allure_title_parametrize3.py --alluredir=./allure
allure serve allure
如圖所示:用例標題就是函式名+ids

1.4、示例四:引數化動態生成標題
1、創建test_allure_title_parametrize4.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
import pytest
import allure
@pytest.fixture()
def login(request):
"""登錄"""
param = request.param
print(f"用戶名:{param['username']},密碼:{param['password']}")
# 回傳
return {"code": 0, "msg": "登陸成功"}
data1 = [
{"username": "name1", "password": "pwd1"},
{"username": "name2", "password": "pwd2"},
{"username": "name3", "password": "pwd3"}
]
data2 = [
("admin1", "123456"),
("admin2", "123456"),
("admin3", "123456")
]
@allure.story('字典引數化')
@allure.title('登錄測驗用例1-{dict}')
@pytest.mark.parametrize('dict', data1)
def test_login1(dict):
"""
登錄測驗用例1
"""
print(dict['username'], dict['password'])
@allure.story('傳值進fixture')
@allure.title('登錄測驗用例2{login}')
@pytest.mark.parametrize('login', data1, indirect=True)
def test_login2(login):
"""
登錄測驗用例2
"""
assert login['code'] == 0
@allure.story('分別傳值')
@allure.title('登錄測驗用例3-用戶名:{username}-密碼:{password}')
@pytest.mark.parametrize('username, password', data2)
def test_login3(username, password):
"""
登錄測驗用例3
"""
print(username, password)
2、輸入命令運行:
pytest test_allure_title_parametrize4.py --alluredir=./allure
allure serve allure
如圖所示:三種方式傳入引數

1.5、示例五:引數化動態生成標題優化
1、創建test_allure_title_parametrize5.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
import pytest
import allure
data = [
("admin1", "123456", "admin1 登錄成功"),
("admin2", "123456", "admin2 登錄失敗"),
("admin3", "123456", "admin3 登錄成功")
]
@allure.story('分別傳值')
@allure.title('登錄測驗用例-{title}')
@pytest.mark.parametrize('username, password, title', data)
def test_login(username, password, title):
"""
登錄測驗用例
"""
print(username, password)
2、輸入命令運行:
pytest test_allure_title_parametrize5.py --alluredir=./allure
allure serve allure
如圖所示:測驗用例標題可讀性比較好,易于維護

2、動態生成功能
@allure.title() 和 @allure.description() 都是裝飾器,給測驗用例提供標題和描述的,其實 allure 提供了在測驗用例執行程序中動態指定標題和描述等標簽的方法,如:allure.dynamic.title()、allure.dynamic.description()
allure.dynamic 提供的方法:

2.1、示例一:allure.dynamic.title()
用例標題
1、創建test_allure_dynamic.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
import allure
@allure.title("裝飾器標題")
def test_case():
print("AllTests軟體測驗")
allure.dynamic.title("動態標題")
2、輸入命令運行:
pytest test_allure_dynamic.py --alluredir=./allure
allure serve allure
如圖所示:

2.2、示例二:allure.dynamic.description()
用例描述
1、創建test_allure_dynamic2.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
import allure
@allure.title("裝飾器標題")
def test_case():
"""
動態設定描述
"""
print("AllTests軟體測驗")
allure.dynamic.description("動態描述")
allure.dynamic.title("動態標題")
2、輸入命令運行:
pytest test_allure_dynamic2.py --alluredir=./allure
allure serve allure
如圖所示:

2.3、示例三:結合@pytest.mark.parametrize()
1、創建test_allure_dynamic3.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
import pytest
import allure
data = [
("admin1", "123456", "admin1 登錄成功"),
("admin2", "123456", "admin2 登錄失敗"),
("admin3", "123456", "admin3 登錄成功")
]
@pytest.mark.parametrize('username, password, title', data)
def test_case(username, password, title):
"""
測驗用例
"""
print(username, password)
allure.dynamic.title(title)
2、輸入命令運行:
pytest test_allure_dynamic3.py --alluredir=./allure
allure serve allure
如圖所示:

2.4、示例四:全部方法示例
1、創建test_allure_dynamic4.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
import allure
def test_case1():
"""
測驗用例1
"""
allure.dynamic.title("動態title")
allure.dynamic.description_html("動態description_html")
allure.dynamic.severity("blocker")
allure.dynamic.feature("動態feature")
allure.dynamic.story("動態story")
allure.dynamic.tag("動態tag")
allure.dynamic.link("https://www.baidu.com/?wd=1", "動態link")
allure.dynamic.issue("https://www.baidu.com/?wd=2", "動態issue")
allure.dynamic.testcase("https://www.baidu.com/?wd=3", "動態testcase")
def test_case2():
"""
測驗用例2
"""
allure.dynamic.description("動態description")
2、輸入命令運行:
pytest test_allure_dynamic4.py --alluredir=./allure
allure serve allure
如圖所示:

測驗用例1

測驗用例2

3、報告添加用例失敗截圖
在進行 UI 自動化的時候,執行測驗用例失敗時,想把用例失敗的截圖展現在 allure 報告里面,
可以使用 pytest 的鉤子函式 pytest_runtest_makereport,用來獲取用例的執行結果,當用例失敗則進行截圖操作,之后添加截圖到allure報告里,可以使用 allure.attach 方法,
1、創建conftest.py檔案
使用鉤子函式pytest_runtest_makereport,并判斷用例失敗時截圖操作,
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
from selenium import webdriver
import pytest
import allure
import os
my_driver = None
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
"""鉤子函式:獲取每個用例的狀態"""
# 獲取鉤子方法的呼叫結果
my_results = yield
rep = my_results.get_result()
# 獲取用例call,執行結果是失敗的,不包含 setup/teardown
if rep.when == "call" and rep.failed:
mode = "a" if os.path.exists("failures") else "w"
with open("failures", mode) as f:
# let's also access a fixture for the fun of it
if "tmpdir" in item.fixturenames:
extra = " (%s)" % item.funcargs["tmpdir"]
else:
extra = ""
f.write(rep.nodeid + extra + "\n")
# 添加allure報告截圖
if hasattr(my_driver, "get_screenshot_as_png"):
with allure.step("添加失敗截圖"):
allure.attach(my_driver.get_screenshot_as_png(), "失敗截圖", allure.attachment_type.PNG)
@pytest.fixture(scope='session')
def browser():
global my_driver
if my_driver is None:
my_driver = webdriver.Chrome()
yield my_driver
print("退出登陸")
my_driver.quit()
2、創建test_allure_screenshot.py檔案,為測驗用例,
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
from selenium import webdriver
import pytest
import allure
def test_case(browser):
with allure.step("打開首頁"):
browser.get("https://www.cnblogs.com/alltests/")
# 斷言-標題
assert browser.title == "AllTests軟體測驗"
3、輸入命令運行:
pytest test_allure_screenshot.py --alluredir=./allure
allure serve allure
如圖所示:
用例失敗時截圖,并將截圖展現在allure報告里,

截圖檔案在報告目錄里,


轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/347096.html
標籤:其他
上一篇:C#的基礎—C#中的方法(3)
