Python—Pytest+Allure定制报告
參考資料:
https://yq.aliyun.com/ziliao/253128
https://www.cnblogs.com/xiaoxi-3-/p/9492534.html
Allure Test Report
一款測(cè)試報(bào)告框架,不僅報(bào)告美觀,而且方便CI集成。
一、環(huán)境配置
安裝Python依賴庫(kù):
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報(bào)告命令
1、pytest命令基礎(chǔ)上加–alluredir,生成xml報(bào)告。
用例執(zhí)行完成之后會(huì)在[xml_report_path]目錄下生成了一堆xml的report文件,當(dāng)然這不是我們最終想要的美觀報(bào)告。
2、需要使用 Command Tool 來(lái)生成我們需要的美觀報(bào)告。
allure generate [xml_report_path] -o [html_report_path] //[html_report_path]根據(jù)自己需要定義文件夾,作者定義為:/report/html打開 index.html,之前寫的 case 報(bào)告就會(huì)呈現(xiàn)在你面前,如下:
注:直接用chrome瀏覽器打開報(bào)告,報(bào)告可能會(huì)是空白頁(yè)面。
解決辦法:
1、在pycharm中右擊index.html選擇打開方式Open in Browser就可以了。
2、使用Firefox直接打開index.html。
三、定制報(bào)告
Feature: 標(biāo)注主要功能模塊
Story: 標(biāo)注Features功能模塊下的分支功能
Severity: 標(biāo)注測(cè)試用例的重要級(jí)別
Step: 標(biāo)注測(cè)試用例的重要步驟
Issue和TestCase: 標(biāo)注Issue、Case,可加入U(xiǎn)RL
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、用例標(biāo)題和用例描述定制詳解
# -*- 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'])添加用例標(biāo)題和用例描述,Report展示見下圖:
4 、Severity定制詳解
Allure中對(duì)嚴(yán)重級(jí)別的定義:
1、 Blocker級(jí)別:中斷缺陷(客戶端程序無(wú)響應(yīng),無(wú)法執(zhí)行下一步操作)
2、 Critical級(jí)別:臨界缺陷( 功能點(diǎn)缺失)
3、 Normal級(jí)別:普通缺陷(數(shù)值計(jì)算錯(cuò)誤)
4、 Minor級(jí)別:次要缺陷(界面錯(cuò)誤與UI需求不符)
5、 Trivial級(jí)別:輕微缺陷(必輸項(xiàng)無(wú)提示,或者提示不規(guī)范)
添加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}") # 測(cè)試步驟,可通過(guò)format機(jī)制自動(dòng)獲取函數(shù)參數(shù) 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}") # 測(cè)試步驟,可通過(guò)format機(jī)制自動(dòng)獲取函數(shù)參數(shù) 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)在報(bào)告中增加附件:allure.attach(’arg1’,’arg2’,’arg3’):
arg1:是在報(bào)告中顯示的附件名稱
arg2:表示添加附件的內(nèi)容
arg3:表示添加的類型(支持:HTML,JPG,PNG,JSON,OTHER,TEXTXML)
添加attach參數(shù),Report展示見下圖:
總結(jié)
以上是生活随笔為你收集整理的Python—Pytest+Allure定制报告的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vhg电路是什么意思_显示装置和电力监测
- 下一篇: python计算算术平方根sqrt()