1. 程式人生 > 其它 ><9>pytest+allure:allure裝飾器

<9>pytest+allure:allure裝飾器

功能、步驟命名

功能名稱:@allure.feature("功能名稱")
子功能名稱:@allure.story("子功能名稱")
步驟細節:@allure.step("步驟細節")

按名稱選擇用例
根據功能名稱選擇:pytest 檔名 --allure-features "功能名稱"
根據子功能名稱選擇:pytest 檔名 --allure-stories "子功能名稱"

在生成的報告中,Behaviors項,可檢視各功能、步驟資訊

import allure


@allure.feature("登入模組")
class TestLogin:
    @allure.story("登入成功")
    def test_login_success(self):
        print("case:登入成功")

    @allure.story("登入失敗1")
    def test_login_fail_a(self):
        print("case:登入失敗,使用者名稱缺失")

    @allure.story("登入失敗2")
    def test_login_fail_b(self):
        print("case:登入失敗,密碼錯誤")

    @allure.story("登入步驟")
    def test_login_step(self):
        with allure.step("步驟1:開啟應用"):
            pass
        with allure.step("步驟2:登入"):
            pass
        print("登入成功")

用例命名

裝飾器:@allure.title("用例名稱")

import allure


@allure.feature("登入模組")
class TestLogin:
    @allure.story("登入成功")
    def test_login_success(self):
        print("case:登入成功")
    
    @allure.title("新增用例名稱")
    def test_login_title(self):
        assert True

設定用例級別
裝飾器:@allure.severity(allure. severity_level.用例級別)
用例級別取值(大寫)
Blocker:中斷
Critical :臨界
Normal :普通
minor :次要
Trivial :輕微

按用例級別過濾用例:pytest 檔名 --allure-severities 用例級別(小寫)

import allure


@allure.feature("登入模組")
class TestLogin:
    @allure.story("登入成功")
    def test_login_success(self):
        print("case:登入成功")

    @allure.story("用例級別")
    @allure.severity(allure.severity_level.NORMAL)
    def test_login_level(self):
        assert True

附帶連結

裝飾器:@allure.testcase(url,urltitle)

import allure


@allure.feature("登入模組")
class TestLogin:
    @allure.story("連結")
    @allure.testcase("https://www.baidu.com/", "百度")
    def test_login_link(self):
        assert True

新增檔案

新增文字裝飾器
@allure.attach("文字", "命名", attachment_type=allure.attachment_type.文字型別)
新增檔案裝飾器
@allure.attach.file("檔案路徑", name="命名", attachment_type=allure.attachment_type.檔案型別)

import allure


@allure.feature("登入模組")
class TestAllure:
    @allure.story("attcah")
    def test_login_attach(self):
        # 新增文字
        allure.attach("文字", attachment_type=allure.attachment_type.TEXT)
        # 新增網頁
        allure.attach("<body>網頁</body>", "網頁名稱", attachment_type=allure.attachment_type.HTML)
        # 新增圖片
        allure.attach.file("./resource/photo/photo.jpg", name="圖片", attachment_type=allure.attachment_type.JPG)
        # 新增mp4
        allure.attach.file("./resource/video/video.mp4", name="視訊", attachment_type=allure.attachment_type.MP4)
        assert True



開啟web服務,共享測試報告

  1. 生成html測試報告:allure generate log路徑 -o 報告路徑
  2. 啟動web服務:allure open -h 本地IP -p 埠 報告路徑