Pytest自動化測試 - allure報告進階

Allure除了具備Pytest基本狀態外,其餘幾乎全部功能也都支持。html


一、嚴重性

若是你想對測試用例進行嚴重等級劃分,可使用 @allure.severity 裝飾器,它能夠應用於函數,方法或整個類。session

它以 allure.severity_level 枚舉值做爲參數,分別爲:BLOCKER(中斷),CRITICAL(嚴重),NORMAL(常規),MINOR(輕微),TRIVIAL(不重要)。函數

示例:測試

# test_sample.py
import allure


# 兩數相加
def add(x, y):
    return x + y

# 測試類
@allure.severity(allure.severity_level.TRIVIAL)
class TestAdd:

 @allure.severity(allure.severity_level.MINOR) def test_first(self):
        assert add(3, 4) == 7

    @allure.severity(allure.severity_level.NORMAL)
    def test_second(self):
        assert add(-3, 4) == 1

    @allure.severity(allure.severity_level.CRITICAL)
    def test_three(self):
        assert add(3, -4) == -1

    @allure.severity(allure.severity_level.BLOCKER)
    def test_four(self):
        assert add(-3, -4) == -7

運行:this

E:\workspace-py\Pytest>pytest test_sample.py --alluredir=report --clean-alluredir
========================================================================== test session starts ==========================================================================
platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
rootdir: E:\workspace-py\Pytest
plugins: allure-pytest-2.8.18, assume-2.3.3, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
collected 4 items                                                                                                                                                        

test_sample.py ....                                                                                                                                                [100%]

=========================================================================== 4 passed in 0.06s ===========================================================================

報告:spa

 

你還能夠經過 --allure-severities 選項指定嚴重等級運行,多個以逗號分隔。code

E:\workspace-py\Pytest>pytest test_sample.py --allure-severities normal,critical ========================================================================== test session starts ==========================================================================
platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
rootdir: E:\workspace-py\Pytest
plugins: allure-pytest-2.8.18, assume-2.3.3, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
collected 4 items                                                                                                                                                        

test_sample.py ..                                                                                                                                                  [100%]

=========================================================================== 2 passed in 0.02s ===========================================================================

 


二、功能

若是你想對測試功能、測試場景進行行爲描述,能夠分別使用裝飾器:@allure.feature@allure.story orm

示例:htm

# test_sample.py
import allure

# 兩數相加
def add(x, y):
    return x + y

@allure.feature('測試類')
class TestAdd:

    @allure.story('01測試兩個正數相加')
    def test_first(self):
        assert add(3, 4) == 7 @allure.story('02測試負數正數相加')
    def test_second(self):
        assert add(-3, 4) == 1 @allure.story('03測試正數負數相加')
    def test_three(self):
        assert add(3, -4) == -1 @allure.story('04測試兩個負數相加')
    def test_four(self):
        assert add(-3, -4) == -7

運行:blog

E:\workspace-py\Pytest>pytest test_sample.py --alluredir=report --clean-alluredir
========================================================================== test session starts ==========================================================================
platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
rootdir: E:\workspace-py\Pytest
plugins: allure-pytest-2.8.18, assume-2.3.3, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
collected 4 items                                                                                                                                                        

test_sample.py ....                                                                                                                                                [100%]

=========================================================================== 4 passed in 0.06s ===========================================================================

報告:

 

你也能夠經過 --allure-features--allure-stories 選擇指定具體功能和故事運行,多個以逗號分隔。

E:\workspace-py\Pytest>pytest test_sample.py --allure-stories 01測試兩個正數相加 ========================================================================== test session starts ==========================================================================
platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
rootdir: E:\workspace-py\Pytest
plugins: allure-pytest-2.8.18, assume-2.3.3, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
collected 4 items                                                                                                                                                        

test_sample.py .                                                                                                                                                   [100%]

=========================================================================== 1 passed in 0.02s ===========================================================================

 


三、步驟

若是你想對每一個測試調用進行很是詳細的逐步說明,能夠經過 @allure.step 裝飾器來實現(固件一樣支持)。

該裝飾器會將方法或函數的調用與提供的參數一塊兒添加到報表中,而且能夠包含一條描述行,該行支持位置關鍵字參數。

示例:

# test_sample.py
import pytest
import allure

@allure.step('兩數相加:{0} + {y}')
def add(x, y):
    r = x + y
    print_res(r)
    return r

@allure.step def print_res(r):
    print('計算結果:', r)

class TestLearning:
    data = [
        [3, 4, 7],
        [-3, 4, 1],
        [3, -4, -1],
        [-3, -4, -7],
    ]
    @pytest.mark.parametrize("data", data)
    def test_add(self, data):
        assert add(data[0], data[1]) == data[2]

報告:

 


四、標題

若是你想讓測試標題更具可讀性,可使用 @allure.title 裝飾器,該裝飾器支持參數的佔位符並支持動態替換。

示例:

# test_sample.py
import pytest
import allure
def add(x, y):
    return x + y
class TestLearning:
    data = [
        [3, 4, 7],
        [-3, 4, 1],
        [3, -4, -1],
        [-3, -4, -7],
    ]
    @allure.title("測試用例-{data}")
    @pytest.mark.parametrize("data", data)
    def test_add(self, data):
        assert add(data[0], data[1]) == data[2]
報告:

 


五、描述

若是你想添加測試的詳細說明,能夠經過添加測試方法描述信息,也可使用裝飾器 @allure.description@allure.description_html 

示例:

# test_sample.py
import allure

# 被測功能
def add(x, y):
    return x + y

# 測試類
class TestLearning:

    @allure.description('測試正數相加')
    def test_first(self):
        assert add(3, 4) == 7

    @allure.description_html('<h1> 測試負數相加 </h1>')
    def test_second(self):
        """你也能夠在這裏添加用例的描述信息,可是會被allure裝飾器覆蓋"""
        assert add(-3, -4) == -7

報告:

 


六、附件

若是你想在報告中顯示不一樣類型的附件,能夠經過如下兩種方式來實現:
allure.attach(body, name, attachment_type, extension)
allure.attach.file(source, name, attachment_type, extension)

示例:

import allure


def test_multiple_attachments(): allure.attach.file(r'C:\Users\Public\Pictures\Sample Pictures\Koala.jpg', attachment_type=allure.attachment_type.JPG) allure.attach('<head></head><body> 測試頁面 </body>', 'Attach with HTML type', allure.attachment_type.HTML)

報告:

 


七、連接

若是你想關聯缺陷追蹤系統或測試管理系統,你可使用裝飾器 @allure.link@allure.issue@allure.testcase

示例:

import allure

@allure.link('http://www.baidu.com')
def test_with_named_link():
    pass

@allure.issue('101', '缺陷問題描述')
def test_with_issue_link():
    pass

@allure.testcase('http://this.testcase.com', '測試用例標題')
def test_with_testcase_link():
    pass

報告:

注意:

@allure.issue 將提供帶有小錯誤圖標的連接,該描述符將測試用例ID做爲輸入參數,以將其與提供的問題連接類型的連接模板一塊兒使用。
連接模板在 --allure-link-patternPytest 的配置選項中指定。連接模板和類型必須使用冒號指定:
pytest test_sample.py --alluredir=report --allure-link-pattern=issue:http://www.mytesttracker.com/issue/{}

 

官方文檔地址:https://docs.qameta.io/allure/#_pytest

 

做者:Leozhanggg

出處:http://www.javashuo.com/article/p-gfwgfnud-nw.html

本文版權歸做者和博客園共有,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,不然保留追究法律責任的權利。

相關文章
相關標籤/搜索