Python—Pytest+Allure定制报告
參考資料:
https://yq.aliyun.com/ziliao/253128
https://www.cnblogs.com/xiaoxi-3-/p/9492534.html
Allure Test Report
一款測試報告框架,不僅報告美觀,而且方便CI集成。
一、環境配置
安裝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報告。
用例執行完成之后會在[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 == 0if __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 == 0if __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 0if __name__ == '__main__':pytest.main(['-s', '-q', '--alluredir', './report/xml'])添加用例標題和用例描述,Report展示見下圖:
4 、Severity定制詳解
Allure中對嚴重級別的定義:
1、 Blocker級別:中斷缺陷(客戶端程序無響應,無法執行下一步操作)
2、 Critical級別:臨界缺陷( 功能點缺失)
3、 Normal級別:普通缺陷(數值計算錯誤)
4、 Minor級別:次要缺陷(界面錯誤與UI需求不符)
5、 Trivial級別:輕微缺陷(必輸項無提示,或者提示不規范)
添加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" % str1if not isinstance(str2, str):return "%s is not a string" % str2return 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" % str1if not isinstance(str2, str):return "%s is not a string" % str2return 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展示見下圖:
總結
以上是生活随笔為你收集整理的Python—Pytest+Allure定制报告的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vhg电路是什么意思_显示装置和电力监测
- 下一篇: python计算算术平方根sqrt()