引言
如果做完自動化測試后,生成的結果可讀性不強,那將會是一次失敗的自動化測試。
pytest自動化測試中,要想報告內容豐富,優雅和可讀性強,就需要與allure結合使用。
allure報告有很多特性,這些特性主要以裝飾器、函數等的方式使用。
Allure裝飾器描述
案例解析
在testcase新建conftest.py文件:
import pytest @pytest.fixture() def action(): print("測試開始".center(30,'*')) yield print("測試結束".center(30,'*'))
在測試文件testcase下新建測試文件test_qq.py文件:
import allure from common import Log import requests @allure.step('這是測試步驟') def step_1(): print("初始化數據") @allure.epic('測試天氣API接口'.center(30,'*')) @allure.feature('測試模塊') @allure.suite('這是套件') class TestHttpbin: """測試模塊httpbin""" def setup(self): """所有用例執行前的條件""" self.logger = Log.MyLog() @allure.severity('normal') @allure.story('故事1:獲取天氣數據') @allure.title('獲取單個城市的天氣') @allure.description('獲取深圳的天氣') @allure.testcase('測試用例地址:www.***.com') @allure.issue('缺陷管理地址:https://www.zentao.net/') @allure.tag('這是tag') def test_001(self,action): """ 測試httpbin接口:get方法 """ step_1() # api:host url = 'https://tianqiapi.com/api' params = {'version':'v6','appid':12253812,'appsecret':'KzT8yOpX'} response = requests.get(url=url,params=params).json() print('接口返回數據: %s'%response) self.logger.info('接口返回數據: %s' % response)
報告生成
# 方法1 # 切換到testcase目錄下 pytest -v test_qq.py --alluredir allure_report allure serve allure_report
運行結果:
命令行參數
pytest運行用例的時候可以加上allure標記用例的參數
--allure-severities=SEVERITIES_SET Comma-separated list of severity names. Tests only with these severities will be run. Possible values are: blocker, critical, normal, minor, trivial. --allure-epics=EPICS_SET Comma-separated list of epic names. Run tests that have at least one of the specified feature labels. --allure-features=FEATURES_SET Comma-separated list of feature names. Run tests that have at least one of the specified feature labels. --allure-stories=STORIES_SET Comma-separated list of story names. Run tests that have at least one of the specified story labels. --allure-link-pattern=LINK_TYPE:LINK_PATTERN Url pattern for link type. Allows short links in test, like 'issue-1'. Text will be formatted to full url with python str.format().
# 選擇運行你要執行epic的用例 pytest --alluredir ./report/allure --allure-epics=epic對大Story的一個描述性標簽 # 選擇運行你要執行features的用例 pytest --alluredir ./report/allure --allure-features=模塊2 # 選擇運行你要執行features的用例 pytest --alluredir ./report/allure --allure-stories="用戶故事:1"
總結
以上就是pytest+allure關於測試用例描述的基本使用方法。
如果對你有幫助或喜歡自動化測試開發的朋友,可以加入右下方QQ交流群學習與探索,更多干貨與你分享。