前言:
最近在研究接口自動化的框架,好的測試報告在整個測試框架起到至關重要的部分。終於被我發現一個超好用的報告框架,不僅報告美觀,而且方便CI集成。
就是它,就是它:Allure Test Report!!!
先上一張報告效果圖:
python版本及必要庫
python 3.5
pytest 3.3.3
pytest-allure-adaptor 1.7.9
一、環境配置
安裝Python依賴庫:
pip3 install pytest
pip3 install pytest-allure-adaptor
安裝 Command Tool:
brew tap qatools/formulas
brew install allure-commandline
官方參考文檔:https://pypi.org/project/pytest-allure-adaptor/
二、生成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
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展示見下圖。
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展示見下圖。
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展示見下圖。
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展示見下圖。
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展示見下圖。
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展示見下圖。
8、attach定制詳解
file = open('../test.png', 'rb').read()
allure.attach('test_img', file, allure.attach_type.PNG)
在報告中增加附件:
allure.attach(’arg1’,’arg2’,’arg3’):
arg1
:是在報告中顯示的附件名稱
arg2
:表示添加附件的內容
arg3
:表示添加的類型(支持:HTML,JPG,PNG,JSON,OTHER,TEXTXML
)
添加attach參數,Report展示見下圖。
此外,Allure還支持Jenkins Plugin,后面我會專門寫一篇博文介紹,感興趣的話請關注我的個人簡書。
以上,對你有幫助的話,點贊吧❤️~~