前言:
最近在研究接口自動化的框架,好的測試報告在整個測試框架起到至關重要的部分。終于被我發現一個超好用的報告框架,不僅報告美觀,而且方便CI集成。
就是它,就是它:Allure Test Report!!!
先上一張報告效果圖:Allure Test Report.png
python版本及必要庫
pytest==3.6.0
pytest-allure-adaptor==1.7.10
allure-python-commons==2.7.0
2020-4-30更新
pytest==5.3.1
allure-pytest==2.8.6
allure-python-commons==2.8.6
??注:pytest-allure-adaptor已棄用,改為allure-pytest;
安裝allure-pytest時,需將pytest-allure-adaptor卸載
一、環境配置
安裝Python依賴庫:
pip3 install pytest
pip3 install allure-pytest
安裝 Command Tool:
brew tap qatools/formulas
brew install allure-commandline
官方參考文檔:https://docs.qameta.io/allure/#_pytest
二、生成html報告命令
1、pytest命令基礎上加--alluredir,生成xml報告。
pytest -s -q --alluredir [xml_report_path]
//[xml_report_path]根據自己需要定義文件夾,作者定義為:/report/xml
用例執行完成之后會在[xml_report_path]目錄下生成了一堆xml的report文件,當然這不是我們最終想要的美觀報告。2、需要使用 Command Tool 來生成我們需要的美觀報告。
allure generate [xml_report_path] -o [html_report_path]
//[html_report_path]根據自己需要定義文件夾,作者定義為:/report/html
打開 index.html,之前寫的 case 報告就會呈現在你面前注??:直接用chrome瀏覽器打開報告,報告可能會是空白頁面。
解決辦法:
1、在pycharm中右擊index.html選擇打開方式Open in Browser就可以了。
2、使用Firefox直接打開index.html。
三、定制報告
Feature: 標注主要功能模塊
Story: 標注Features功能模塊下的分支功能
Severity: 標注測試用例的重要級別
Step: 標注測試用例的重要步驟
Issue和TestCase: 標注Issue、Case,可加入URL
attach: 標注增加附件
Environment: 標注環境Environment字段
1、Features定制詳解
# -*- coding: utf-8 -*-
# @Time : 2018/8/17 上午10:10
# @Author : WangJuan
# @File : test_case.py
import allure
import pytest
@allure.feature('test_module_01')
def test_case_01():
"""
用例描述:Test case 01
"""
assert 0
@allure.feature('test_module_02')
def test_case_02():
"""
用例描述:Test case 02
"""
assert 0 == 0
if __name__ == '__main__':
pytest.main(['-s', '-q', '--alluredir', './report/xml'])
添加feature,Report展示見下圖。feature.png
2、Story定制詳解
# -*- coding: utf-8 -*-
# @Time : 2018/8/17 上午10:10
# @Author : WangJuan
# @File : test_case.py
import allure
import pytest
@allure.feature('test_module_01')
@allure.story('test_story_01')
def test_case_01():
"""
用例描述:Test case 01
"""
assert 0
@allure.feature('test_module_01')
@allure.story('test_story_02')
def test_case_02():
"""
用例描述:Test case 02
"""
assert 0 == 0
if __name__ == '__main__':
pytest.main(['-s', '-q', '--alluredir', './report/xml'])
添加story,Report展示見下圖。image.png
3、用例標題和用例描述定制詳解
# -*- coding: utf-8 -*-
# @Time : 2018/8/17 上午10:10
# @Author : WangJuan
# @File : test_case.py
import allure
import pytest
@allure.feature('test_module_01')
@allure.story('test_story_01')
#test_case_01為用例title
def test_case_01():
"""
用例描述:這是用例描述,Test case 01,描述本人
"""
#注釋為用例描述
assert 0
if __name__ == '__main__':
pytest.main(['-s', '-q', '--alluredir', './report/xml'])
添加用例標題和用例描述,Report展示見下圖。image.png
4 、Severity定制詳解
Allure中對嚴重級別的定義:
1、 Blocker級別:中斷缺陷(客戶端程序無響應,無法執行下一步操作)
2、 Critical級別:臨界缺陷( 功能點缺失)
3、 Normal級別:普通缺陷(數值計算錯誤)
4、 Minor級別:次要缺陷(界面錯誤與UI需求不符)
5、 Trivial級別:輕微缺陷(必輸項無提示,或者提示不規范)
# -*- coding: utf-8 -*-
# @Time : 2018/8/17 上午10:10
# @Author : WangJuan
# @File : test_case.py
import allure
import pytest
@allure.feature('test_module_01')
@allure.story('test_story_01')
@allure.severity('blocker')
def test_case_01():
"""
用例描述:Test case 01
"""
assert 0
@allure.feature('test_module_01')
@allure.story('test_story_01')
@allure.severity('critical')
def test_case_02():
"""
用例描述:Test case 02
"""
assert 0 == 0
@allure.feature('test_module_01')
@allure.story('test_story_02')
@allure.severity('normal')
def test_case_03():
"""
用例描述:Test case 03
"""
assert 0
@allure.feature('test_module_01')
@allure.story('test_story_02')
@allure.severity('minor')
def test_case_04():
"""
用例描述:Test case 04
"""
assert 0 == 0
if __name__ == '__main__':
pytest.main(['-s', '-q', '--alluredir', './report/xml'])
添加Severity,Report展示見下圖。image.png
5、Step定制詳解
# -*- coding: utf-8 -*-
# @Time : 2018/8/17 上午10:10
# @Author : WangJuan
# @File : test_case.py
import allure
import pytest
@allure.step("字符串相加:{0},{1}")
# 測試步驟,可通過format機制自動獲取函數參數
def str_add(str1, str2):
if not isinstance(str1, str):
return "%s is not a string" % str1
if not isinstance(str2, str):
return "%s is not a string" % str2
return str1 + str2
@allure.feature('test_module_01')
@allure.story('test_story_01')
@allure.severity('blocker')
def test_case():
str1 = 'hello'
str2 = 'world'
assert str_add(str1, str2) == 'helloworld'
if __name__ == '__main__':
pytest.main(['-s', '-q', '--alluredir', './report/xml'])
添加Step,Report展示見下圖。image.png
6、Issue和TestCase定制詳解
# -*- coding: utf-8 -*-
# @Time : 2018/8/17 上午10:10
# @Author : WangJuan
# @File : test_case.py
import allure
import pytest
@allure.step("字符串相加:{0},{1}") # 測試步驟,可通過format機制自動獲取函數參數
def str_add(str1, str2):
print('hello')
if not isinstance(str1, str):
return "%s is not a string" % str1
if not isinstance(str2, str):
return "%s is not a string" % str2
return str1 + str2
@allure.feature('test_module_01')
@allure.story('test_story_01')
@allure.severity('blocker')
@allure.issue("http://www.baidu.com")
@allure.testcase("http://www.testlink.com")
def test_case():
str1 = 'hello'
str2 = 'world'
assert str_add(str1, str2) == 'helloworld'
if __name__ == '__main__':
pytest.main(['-s', '-q', '--alluredir', './report/xml'])
添加Issue和TestCase,Report展示見下圖。image.png
7、Environment定制詳解
# 具體Environment參數可自行設置
allure.environment(app_package='com.mobile.fm')
allure.environment(app_activity='com.mobile.fm.activity')
allure.environment(device_name='aad464')
allure.environment(platform_name='Android')
添加Environment參數,Report展示見下圖。Environment.png
8、attach定制詳解
file = open('../test.png', 'rb').read()
allure.attach(name='cart_pay_err', body=file, attachment_type=allure.attachment_type.PNG)
在報告中增加附件:
allure.attach(’arg1’,’arg2’,’arg3’):
arg1
:表示添加附件的內容
arg2
:是在報告中顯示的附件名稱
arg3
:表示添加的類型(支持:HTML,JPG,PNG,JSON,OTHER,TEXTXML
)
添加attach參數,Report展示見下圖。image.png
此外,Allure還支持Jenkins Plugin~
感興趣的話,請移步Jenkins構建Allure Report
以上,對你有幫助的話,點贊吧??~~
歡迎關注我的簡書,博客,TesterHome,Github~~~