天天看點

Pytest學習筆記5——Allure測試報告用例較長的描述

目錄

  引言

  Allure裝飾器描述    

  案例解析

  報告生成

  指令行參數

  總結

  引言

  如果做完自動化測試後,生成的結果可讀性不強,那将會是一次失敗的自動化測試。

  pytest自動化測試中,要想報告内容豐富,優雅和可讀性強,就需要與allure結合使用。

  allure報告有很多特性,這些特性主要以裝飾器、函數等的方式使用。

  Allure裝飾器描述    

Pytest學習筆記5——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學習筆記5——Allure測試報告用例較長的描述

  指令行參數

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交流群學習與探索,更多幹貨與你分享。