天天看點

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

Allure除了具有Pytest基本狀态外,其他幾乎所有功能也都支援。

1、嚴重性

如果你想對測試用例進行嚴重等級劃分,可以使用 @allure.severity 裝飾器,它可以應用于函數,方法或整個類。

它以 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
           

運作:

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 ===========================================================================
           

報告:

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

你還可以通過 --allure-severities 選項指定嚴重等級運作,多個以逗号分隔。

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 ===========================================================================
           

2、功能

如果你想對測試功能、測試場景進行行為描述,可以分别使用裝飾器: @allure.feature 和 @allure.story 。

示例:

# test_sample.py
import allure

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

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

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

    @allure.story('測試負數正數相加')
    def test_second(self):
        assert add(-3, 4) == 1

    @allure.story('測試正數負數相加')
    def test_three(self):
        assert add(3, -4) == -1

    @allure.story('測試兩個負數相加')
    def test_four(self):
        assert add(-3, -4) == -7
           

運作:

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 ===========================================================================
           

報告:

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

你也可以通過 --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%]

           

3、步驟

如果你想對每個測試調用進行非常詳細的逐漸說明,可以通過 @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]
           

報告:

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

4、标題

如果你想讓測試标題更具可讀性,可以使用 @allure.title 裝飾器,該裝飾器支援參數的占位符并支援動态替換(與@allure.story有點類似)。

示例:

# 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]
           

報告:

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

5、描述

如果你想添加測試的詳細說明,可以通過添加測試方法描述資訊,也可以使用裝飾器 @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
           

報告:

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

6、附件

如果你想在報告中顯示不同類型的附件,可以通過以下兩種方式來實作:

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)
           

報告:

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

7、連結

如果你想關聯缺陷追蹤系統或測試管理系統,你可以使用裝飾器 @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
           

報告:

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

注意:

@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

出處: https://www.cnblogs.com/leozhanggg/p/14059844.html