1、Environment
在Allure報(bào)告中添加環(huán)境信息,通過創(chuàng)建environment.properties或者environment.xml文件,并把文件存放到allure-results(這個(gè)目錄是生成最后的html報(bào)告之前,生成依賴文件的目錄)目錄下
# environment.properties
Browser=Chrome
Browser.Version=63.0
Stand=Production
ApiUrl=127.0.0.1/login
python.Version=3.6
# environment.xml
<environment>
<parameter>
<key>Browser</key>
<value>Chrome</value>
</parameter>
<parameter>
<key>Browser.Version</key>
<value>63.0</value>
</parameter>
<parameter>
<key>Stand</key>
<value>Production</value>
</parameter>
<parameter>
<key>ApiUrl</key>
<value>127.0.0.1/login</value>
</parameter>
<parameter>
<key>python.Version</key>
<value>3.6</value>
</parameter>
</environment>
這個(gè)文件需要在執(zhí)行pytest -s -q --alluredir [xml_path]
命令后將文件放在[xml_path]
路徑下,這樣在執(zhí)行allure generate [xml_parh] -o [report_path]
生成的html格式的report才會顯示相關(guān)的環(huán)境信息
2肩袍、Categories
測試報(bào)告默認(rèn)統(tǒng)計(jì)兩種類型的測試用例結(jié)果,失敗的用例和故障測試用例婚惫,我們可以自定義添加用例的統(tǒng)計(jì)類型氛赐,同樣需要在allure-results目錄下新建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"]
}
]
查看報(bào)告
3、@allure.step
pytest支持使用@allure.step
修飾某些測試用例中需要的函數(shù)先舷,使測試用例在allure報(bào)告中能夠更加詳細(xì)的顯示測試過程
@allure.step("輸入用戶名")
def input_username(user):
print("輸入用戶名")
return user
@allure.step("輸入密碼")
def input_password(pwd):
print("輸入密碼")
return pwd
@allure.feature("登錄模塊")
class TestLogin(object):
@allure.severity(allure.severity_level.BLOCKER)
@allure.story("測試登錄成功")
@allure.title("登錄成功場景-{data}")
@pytest.mark.parametrize("data", login_success_data, ids=ids_login_success_data)
def test_login_success(self, data):
"""測試登錄成功"""
user = input_username(data["user"])
pwd = input_password(data["pwd"])
result = login(user, pwd)
assert result == data["expected"]
@allure.severity(allure.severity_level.CRITICAL)
@allure.story("測試登錄失敗")
@pytest.mark.parametrize("data", login_fail_data, ids=ids_login_fail_data)
def test_login_fail(self, data):
"""測試用戶名或密碼錯(cuò)誤"""
user = input_username(data["user"])
pwd = input_password(data["pwd"])
result = login(user, pwd)
assert result == data["expected"]
查看報(bào)告
conftest.py
@allure.step修飾的測試步驟還支持在conftest.py
文件中定義艰管,作為fixture的步驟,現(xiàn)在我們在項(xiàng)目目錄下新建conftest.py
文件蒋川,寫入如下代碼
# conftest.py
@allure.step("打開瀏覽器")
def fixture_step():
pass
@pytest.fixture
def init_url():
fixture_step()
yield True
test_allure_feature.py
文件中添加如下用例
def test_init_url(self, init_url):
flag = init_url
assert flag == True
查看報(bào)告
4牲芋、allure.attach
使用allure.attach
可以給報(bào)告中添加文件,圖片,log缸浦,html代碼等等夕冲。 我們修改test_allure_feature.py
中如下用例, 并在用例所在目錄添加attach.png
圖片
def test_failed(self):
"""你也可以在這里添加用例的描述信息裂逐,但是會被allure.description覆蓋"""
try:
assert False
except AssertionError as e:
with open("attach.png", "rb") as f:
context = f.read()
allure.attach(context, "錯(cuò)誤圖片",
attachment_type=allure.attachment_type.PNG)
raise e
查看報(bào)告
5姑蓝、@allure.description
如果你想在報(bào)告中展示測試用例的描述信息搀罢,那么你可以使用@allure.description(string)
或者@allure.description_html(html代碼)
修飾你的測試用例
@allure.description("這是一個(gè)一直執(zhí)行失敗的測試用例")
def test_failed(self):
"""你也可以在這里添加用例的描述信息,但是會被allure.description覆蓋"""
try:
assert False
except AssertionError as e:
with open("attach.png", "rb") as f:
context = f.read()
allure.attach(context, "錯(cuò)誤圖片",
attachment_type=allure.attachment_type.PNG)
raise e
查看報(bào)告
6缅疟、@allure.title
使用allure.title(title)
可以重命名測試用例在allure報(bào)告中的名稱
@allure.title("登錄成功場景-{data}")
@pytest.mark.parametrize("data", login_success_data桃熄,ids=ids_login_success_data)
def test_login_success(self, data):
"""測試登錄成功"""
user = input_username(data["user"])
pwd = input_password(data["pwd"])
result = login(user, pwd)
assert result == data["expected"]
查看報(bào)告
7慢叨、@allure.link巴席、@allure.testcase匿情、@allure.issue
這三種特性都可以給測試用例添加一個(gè)鏈接
@allure.testcase("https://www.cnblogs.com/linuxchao/", "測試用例地址")
def test_init_url(self, init_url):
flag = init_url
assert flag == True
@allure.link("https://www.cnblogs.com/linuxchao/", name="bug鏈接")
@allure.description("這是一個(gè)一直執(zhí)行失敗的測試用例")
def test_failed(self):
"""你也可以在這里添加用例的描述信息,但是會被allure.description覆蓋"""
try:
assert False
except AssertionError as e:
with open("attach.png", "rb") as f:
context = f.read()
allure.attach(context, "錯(cuò)誤圖片", attachment_type=allure.attachment_type.PNG)
raise e
@allure.issue("https://www.cnblogs.com/linuxchao/", "錯(cuò)誤鏈接")
def test_broken(self):
"""broken"""
with open("broken.json", "r", encoding='utf8') as f:
f.read()
查看報(bào)告
8鸽照、@allure.feature螺捐、@allure.story
feature和story被稱為行為驅(qū)動(dòng)標(biāo)記颠悬,因?yàn)槭褂眠@個(gè)兩個(gè)標(biāo)記矮燎,通過報(bào)告可以更加清楚的掌握每個(gè)測試用例的功能和每個(gè)測試用例的測試場景
@allure.severity(allure.severity_level.BLOCKER)
@allure.feature("fixture場景")
class TestClass(object):
def test_with_scoped_finalizers(self,
function_scope_fixture_with_finalizer,
class_scope_fixture_with_finalizer,
module_scope_fixture_with_finalizer,
session_scope_fixture_with_finalizer):
step_inside_test_body()
@allure.feature("登錄模塊")
class TestLogin(object):
@allure.severity(allure.severity_level.BLOCKER)
@allure.story("測試登錄成功")
@allure.title("登錄成功場景-{data}")
@pytest.mark.parametrize("data", login_success_data, ids=ids_login_success_data)
def test_login_success(self, data):
"""測試登錄成功"""
user = input_username(data["user"])
pwd = input_password(data["pwd"])
result = login(user, pwd)
assert result == data["expected"]
@allure.severity(allure.severity_level.CRITICAL)
@allure.story("測試登錄失敗")
@pytest.mark.parametrize("data", login_fail_data, ids=ids_login_fail_data)
def test_login_fail(self, data):
"""測試用戶名或密碼錯(cuò)誤"""
user = input_username(data["user"])
pwd = input_password(data["pwd"])
result = login(user, pwd)
assert result == data["expected"]
@allure.severity(allure.severity_level.MINOR)
@allure.story("測試用戶名參數(shù)缺失")
@pytest.mark.parametrize("data", username_none, ids=ids_username_none)
def test_username_none(self, data):
"""測試缺省用戶名"""
pwd = input_password(data["pwd"])
result = login(password=pwd)
assert result == data["expected"]
@allure.severity(allure.severity_level.MINOR)
@allure.story("測試密碼參數(shù)缺失")
@pytest.mark.parametrize("data", password_none, ids=ids_password_none)
def test_password_none(self, data):
"""測試缺省密碼"""
user = input_username(data["user"])
result = login(username=user)
assert result == data["expected"]
@allure.severity(allure.severity_level.MINOR)
@allure.story("測試初始化地址")
@allure.testcase("https://www.cnblogs.com/linuxchao/", "測試用例地址")
def test_init_url(self, init_url):
flag = init_url
assert flag is True
@allure.severity(allure.severity_level.NORMAL)
@allure.story("測試失敗用例與用例中添加附件")
@allure.link("https://www.cnblogs.com/linuxchao/", name="bug鏈接")
@allure.description("這是一個(gè)一直執(zhí)行失敗的測試用例")
def test_failed(self):
"""你也可以在這里添加用例的描述信息,但是會被allure.description覆蓋"""
try:
assert False
except AssertionError as e:
with open(r"/Users/luozelin/Desktop/pytest/allurereport/imgs/attpng.png", "rb") as f:
context = f.read()
allure.attach(context, "錯(cuò)誤圖片", attachment_type=allure.attachment_type.PNG)
raise e
@allure.severity(allure.severity_level.TRIVIAL)
@allure.story("測試broken用例")
@allure.issue("https://www.cnblogs.com/linuxchao/", "錯(cuò)誤鏈接")
def test_broken(self):
"""broken"""
with open("broken.json", "r", encoding='utf8') as f:
f.read()
@allure.severity(allure.severity_level.TRIVIAL)
@allure.story("測試無條件跳過測試用例")
@pytest.mark.skip(reason="無條件跳過")
def test_skip(self):
"""skip"""
pass
查看報(bào)告
從上面可以看出
@allure.feature("xxxx")
在測試報(bào)告中顯示為用例屬于某一模塊
@allure.story("xxxx")
在測試報(bào)告中顯示為用例是什么測試場景
以上兩種標(biāo)記不僅僅能夠在測試報(bào)告中顯示赔癌,而且還可以使用命令執(zhí)行指定的測試模塊或者場景
9诞外、@allure.severity
此標(biāo)記用來標(biāo)識測試用例或者測試類的級別,分為blocker灾票,critical峡谊,normal,minor刊苍,trivial5個(gè)級別既们,下面?zhèn)儼褱y試用例按級別標(biāo)記,并查看一下測試報(bào)告
嚴(yán)重性級別來標(biāo)記case正什,這里等于給每個(gè)case定義一個(gè)嚴(yán)重級別 啥纸,在Graphs頁面查看分布情況
@allure.feature("登錄模塊")
class TestLogin(object):
@allure.severity(allure.severity_level.BLOCKER)
@allure.story("測試登錄成功")
@allure.title("登錄成功場景-{data}")
@pytest.mark.parametrize("data", login_success_data, ids=ids_login_success_data)
def test_login_success(self, data):
"""測試登錄成功"""
user = input_username(data["user"])
pwd = input_password(data["pwd"])
result = login(user, pwd)
assert result == data["expected"]
@allure.severity(allure.severity_level.CRITICAL)
@allure.story("測試登錄失敗")
@pytest.mark.parametrize("data", login_fail_data, ids=ids_login_fail_data)
def test_login_fail(self, data):
"""測試用戶名或密碼錯(cuò)誤"""
user = input_username(data["user"])
pwd = input_password(data["pwd"])
result = login(user, pwd)
assert result == data["expected"]
@allure.severity(allure.severity_level.MINOR)
@allure.story("測試用戶名參數(shù)缺失")
@pytest.mark.parametrize("data", username_none, ids=ids_username_none)
def test_username_none(self, data):
"""測試缺省用戶名"""
pwd = input_password(data["pwd"])
result = login(password=pwd)
assert result == data["expected"]
@allure.severity(allure.severity_level.MINOR)
@allure.story("測試密碼參數(shù)缺失")
@pytest.mark.parametrize("data", password_none, ids=ids_password_none)
def test_password_none(self, data):
"""測試缺省密碼"""
user = input_username(data["user"])
result = login(username=user)
assert result == data["expected"]
@allure.severity(allure.severity_level.MINOR)
@allure.story("測試初始化地址")
@allure.testcase("https://www.cnblogs.com/linuxchao/", "測試用例地址")
def test_init_url(self, init_url):
flag = init_url
assert flag is True
@allure.severity(allure.severity_level.NORMAL)
@allure.story("測試失敗用例與用例中添加附件")
@allure.link("https://www.cnblogs.com/linuxchao/", name="bug鏈接")
@allure.description("這是一個(gè)一直執(zhí)行失敗的測試用例")
def test_failed(self):
"""你也可以在這里添加用例的描述信息,但是會被allure.description覆蓋"""
try:
assert False
except AssertionError as e:
with open(r"/Users/luozelin/Desktop/pytest/allurereport/imgs/attpng.png", "rb") as f:
context = f.read()
allure.attach(context, "錯(cuò)誤圖片", attachment_type=allure.attachment_type.PNG)
raise e
@allure.severity(allure.severity_level.TRIVIAL)
@allure.story("測試broken用例")
@allure.issue("https://www.cnblogs.com/linuxchao/", "錯(cuò)誤鏈接")
def test_broken(self):
"""broken"""
with open("broken.json", "r", encoding='utf8') as f:
f.read()
@allure.severity(allure.severity_level.TRIVIAL)
@allure.story("測試無條件跳過測試用例")
@pytest.mark.skip(reason="無條件跳過")
def test_skip(self):
"""skip"""
pass
從上面來看婴氮,對應(yīng)的用例級別為:
@allure.severity(allure.severity_level.BLOCKER)
對應(yīng)的是中斷缺陷(客服端程序無響應(yīng)斯棒,無法執(zhí)行下一步驟)
@allure.severity(allure.severity_level.CRITICAL)
對應(yīng)的是臨界缺陷(功能點(diǎn)缺失)
@allure.severity(allure.severity_level.NORMAL)
對應(yīng)的是普通缺陷(數(shù)據(jù)計(jì)算錯(cuò)誤)
@allure.severity(allure.severity_level.MINOR)
對應(yīng)的是次要缺陷(界面錯(cuò)誤與ui需求不符)
@allure.severity(allure.severity_level.TRIVIAL)
對應(yīng)的是輕微缺陷(必須項(xiàng)無提示,或者提示不規(guī)范)
[轉(zhuǎn)載參考文章]:https://blog.csdn.net/BearStarX/article/details/101216016