1. 程式人生 > 實用技巧 >Pytest 系列(八)常用外掛之測試報告

Pytest 系列(八)常用外掛之測試報告

一、pytest-html 生成 html 測試報告

要求:python 3.6+

安裝:pip install pytest-html

文件:https://github.com/pytest-dev/pytest-html、https://www.cnblogs.com/linuxchao/p/linuxchao-pytest-report.html

使用方法:在命令列或 pytest.ini 配置檔案 addopts 中新增選項 --html=測試報告路徑

(1)命令列方式: pytest --html report/report.html --self-contained-html

(2)pytest.ini檔案: addopts = pytest --html report/report.html --self-contained-html

注:預設情況下生成的 html 報告中 css 是分開的,分享報告時將無法正確展示樣式,可以使用 --self-contained-html 將 css 合併到 html 中

二、allure-pytest 生成 allure 測試報告

Allure 框架是一個靈活的輕量級多語言測試報告工具,它不僅以 web 的方式展示了簡介的測試結果,而且允許參與開發過程的每個人從日常執行的測試中最大限度的提取有用資訊。

從 dev/qa 的角度來看,Allure 報告簡化了常見缺陷的統計:失敗的測試可以分為 bug和被中斷的測試,還可以配置日誌、步驟、fixture、附件、計時、執行歷史以及與TMS和BUG管理系統整合,所以,通過以上配置,所有負責的開發人員和測試人員可以儘可能的掌握測試資訊。

從管理者的角度來看,Allure 提供了一個清晰的“大圖”,其中包括已覆蓋的特性、缺陷聚集的位置、執行時間軸的外觀以及許多其他方便的事情。allure 的模組化和可擴充套件性保證了您總是能夠對某些東西進行微調,使 Allure 更適合您,那麼今天我們就來說說如何使報告更加詳細的顯示我們需要的資訊,以及 allure 與 jenkins 的整合。

要生成一份完整的 allure 測試報告,需要先安裝 allure 命令列工具,然後找到資料生成測試報告。

2.1 安裝

需安裝 allure 命令列工具以及 alluer-pytest 外掛

安裝 allure:windows 下進入 https://bintray.com/qameta/maven/allure2

下載 Allure 的zip安裝包解壓,新增 allure(\安裝路徑\bin)到環境變數 path ;使用 allure --version 檢視安裝版本

原始碼:https://github.com/allure-framework/allure2

安裝 alluer-pytest:pip install allure-pytest

文件:https://pypi.org/project/allure-pytest/

2.2 使用方法

(1)allure-pytest 外掛收集測試結果資料:在 pytest 命令列或 pytest.ini 配置檔案 addopts 中新增選項 --alluredir=測試報告儲存路徑

(2)生成測試報告:allure serve 測試報告儲存路徑(執行該命令後將自動開啟瀏覽器瀏覽測試報告)

2.3 Allure 報告結構

2.3.1 Overview:總覽

首頁的部件和小視窗可以拖動和自定義。

  1. Environment

可以通過建立 environment.properties 或者 environment.xml 檔案來配置環境資訊,並把檔案存放到 allure-results(這個目錄是生成最後的 html 報告之前,生成依賴檔案的目錄)目錄下,也就是 --alluredir 後面跟的目錄

# environment.properties 
Browser=Chrome 
Browser.Version=80.0.3987.132 
Stand=test 
ApiUrl=127.0.0.1/login 
python.Version=3.6.7

2.3.2 Categories 缺陷類別

預設是分了 failed 和 error,凡是執行結果是其中一個的都會被歸到類裡面,可以通過這裡快捷檢視哪些用例是 failed 和 error 的

  • Product defects 產品缺陷(測試結果:failed)

  • Test defects 測試缺陷(測試結果:error/broken)

也可以自定義缺陷分類,在生成測試報告之前將 categories.json 檔案新增到 allure-results 目錄即可(和上面 environment.properties 放同一個目錄)

自定義缺陷引數說明:

  • name:(必填)類別名稱,可以為中文

  • matchedStatuses:(可選)測試用例的執行狀態。預設["failed", "broken", "passed", "skipped", "unknown"]

  • messageRegex:(可選)測試用例執行的錯誤資訊,預設是 .* ,可通過正則去匹配

  • traceRegex:(可選)測試用例執行的錯誤堆疊資訊,預設是 .* ,可通過正則去匹配

示例:categories.json
[
  {
    "name": "Ignored tests", 
    "matchedStatuses": ["skipped"] 
  },
  {
    "name": "Infrastructure problems",
    "matchedStatuses": ["broken", "failed"],
    "messageRegex": ".*bye-bye.*" 
  },
  {
    "name": "Outdated tests",
    "matchedStatuses": ["broken"],
    "traceRegex": ".*FileNotFoundException.*" 
  },
  {
    "name": "Product defects",
    "matchedStatuses": ["failed"]
  },
  {
    "name": "Test defects",
    "matchedStatuses": ["broken"]
  }
]

2.3.3 Suites

測試套件,就是所有用例的層級關係,可以根據 package、module、類、方法來查詢用例

2.3.4 Graphs

測試結果圖形化,包括用例執行結果的分佈圖,優先順序,耗時等

2.3.5 Timeline

可以看到測試用例精確的測試時序(執行順序),包括執行時間

2.3.6 Behaviors

行為驅動,根據 epic、feature、story 來分組測試用例

2.3.7 Packages

按照 package、module 來分組測試用例

2.3.8 Test Case Page

在某些結果概述頁面中,單擊各個測試可以轉到測試用例頁面。該頁面包含許多與測試用例相關的資料:測試期間執行的步驟,計時,附件,測試分類標籤,描述和連結。

2.3.9 Flaky

對於不穩定的測試用例,可以使用 @Flaky 將測試用例標記為不穩定,在測試報告中將標記為 Flaky

2.4 pytest-allure 支援的功能

allure 除了支援 pytest 自帶的特性之外,本身也有強大的特性可以使用。

2.4.1 測試步驟 @allure.step

allure 允許對每個測試用例進行詳細的步驟說明,在測試用例上新增 @allure.step 裝飾器即可。該裝飾器將帶註釋的方法或函式的呼叫與提供的引數一起新增到報表中。

帶有註釋的方法 @allure.step 可以儲存在測試之外,並在需要時匯入。

step 可以包含一條描述行,該行支援傳遞的位置和關鍵字引數的佔位符。關鍵字引數的預設引數也將被捕獲。

可以在 conftest.py 中定義 step,示例:

conftest.py
import allure
import pytest

@allure.step('step in conftest.py')
def conftest_step():
    pass

@pytest.fixture
def fixture_with_conftest_step():
    conftest_step()
test_demo.py
import allure

@allure.step
def passing_step():
    pass

def test_with_step_in_fixture_from_conftest(fixture_with_conftest_step):
    passing_step()

conftest.py 中的 step 顯示在單獨的樹中,如下:

2.4.2 附件 allure.attach

使用 allure.attach 可以給測試報告新增各種附件。

方法一:allure.attach(body, name, attachment_type, extension)

方法二:allure.attach.file(source, name, attachment_type, extension)

  • body:檔案原始內容

  • name:帶有檔名的字串

  • attachment_type:附件型別,可在 allure.attachment_type 中檢視支援的附件型別

  • extension:建立檔案的副檔名

  • source:包含檔案路徑的字串

示例程式碼
import allure

def test_demo():
    allure.attach('A text attacment in module scope fixture', 'text 附件', allure.attachment_type.TEXT)
    allure.attach('<head></head><body> a page </body>', " html ",  allure.attachment_type.HTML)
    allure.attach.file('d:\\1.png', " png 圖片",  allure.attachment_type.PNG)

    assert 1

報告:

2.4.3 描述 allure.description

若想要測試報告更加清晰,可以新增詳細的測試用例描述,有三種方式:

  • @allure.description(str)

  • 在測試用例函式宣告下方新增""" """

  • @allure.description_html(str):相當於傳一個HTML程式碼組成的字串,類似allure.attach()中傳HTML

示例程式碼
import pytest
import allure

@allure.description("測試用例描述資訊")
def test_01():
    assert 1

@allure.description_html("<head></head><body> test description </body>")
def test_02():
    assert 1

def test_03():
    """
    測試用例描述資訊
    """
    assert 1

報告:

也可以在測試函式中使用 allure.dynamic.description() 或 allure.dynamic.description_html() 動態更新描述資訊。

示例程式碼
import pytest
import allure

@allure.description("測試用例描述資訊")
def test_01():
    allure.dynamic.description("修改後的測試用例描述資訊")
    assert 1

報告:

2.4.4 標題 allure.title

可以使用@allure.title 裝飾器使測試標題更具可讀性。支援中文,支援引數的佔位符並支援動態替換。

示例程式碼
import pytest
import allure

@allure.title("This test has a custom title")
def test_01():
    assert 1

@allure.title("Parameterized test title: adding {param1} with {param2}")
@pytest.mark.parametrize('param1,param2,expected', [(2, 2, 4), (1, 2, 5)
])
def test_02(param1, param2, expected):
    assert param1 + param2 == expected


@allure.title("This title will be replaced in a test body")
def test_03():
    assert 1
    allure.dynamic.title('After a successful test finish, the title was replaced with this line.')

報告:

2.4.5 allure.link、alluer.issue、allure.testcase

為了將 allure 報告和測試管理系統整合,可以使用 link、issue、testcase

  • link(url, link_type, name=None):提供連結地址

  • issue(url, name=None):提供帶有小錯誤圖示的連結

  • testcase(url, name=None):

issue 和 tescase 其實也是呼叫的 link,只是 LinkType 不一樣。

示例程式碼
import allure

@allure.link('https://www.baidu.com', name='百度一下')
def test_with_link():
    pass

@allure.issue('140', 'Pytest-flaky test retries shows like test steps')
def test_with_issue():
    pass

@allure.testcase('https://github.com/qameta/allure-integrations/issues/8#issuecomment-268313637', 'Test case title')
def test_with_testcase():
    pass

# 也可以將作為輸入引數,與提供的問題連結模板一起使用。連結模板在 --allure-link-patternPytest 的配置選項中指定,型別與連結模板使用冒號分割,型別可以是 link、issue、testcase

pytest directory_with_tests/ --alluredir=/tmp/my_allure_report --allure-link-pattern=issue:http://www.mytesttracker.com/issue/{}

# 以上示例中 allure.issue 的連結地址則為 http://www.mytesttracker.com/issue/140

2.4.6 標記測試用例

@pytest.mark 標記並不會顯示在 allure 報告上,allure 也提供了三種類型的標記裝飾器,它們是可以顯示在 allure 報告上的。如果不加 @allure.feature、@allure.story 時,在測試報告總覽的 FEATURES BY STORIES 欄,測試用例不會分類顯示。

  • BDD樣式的標記裝飾器:@allure.feature()、@allure.story(),其中 story 是 feature 的子集,測試報告上將優先顯示 feature,展開再顯示 story
示例程式碼
import allure

@allure.story('epic_1')
def test_with_epic_1():
    pass

@allure.story('story_1')
def test_with_story_1():
    pass

@allure.story('story_2')
def test_with_story_2():
    pass

想要執行指定的 story 或者 feature 時,執行 pytest 時可以新增選項 --allure-stories、--allure-feature 來指定不同的用例集,用例集之間用逗號 , 分割。

pytest tests.py --allure-stories story_1,story_2 --allure-feature feature_2

  • 優先順序(嚴重程度)標記裝飾器:@allure.severity()可以修飾函式、方法或整個類,以 allure.severity_level 列舉值作為引數,值包括:BLOCKER、CRITICAL、NORMAL、MINOR、TRIVIAL。
示例程式碼
import allure

@allure.severity(allure.severity_level.CRITICAL)
def test_demo():
    pass

執行指定嚴重程度的用例,可以在執行 pytest 命令時新增選項--allure-severities

pytest tests.py --allure-severities normal,critical