pytest中allure的裝飾器的使用及作用


1. allure的常用裝飾器

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

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

裝飾器:@allure.severity(allure. severity_level.用例級別)

用例級別取值(大寫)
Blocker:中斷
Critical :臨界
Normal :普通
minor :次要
Trivial :輕微

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

添加文本裝飾器   @allure.attach("文本", "命名", attachment_type=allure.attachment_type.文本類型)

添加文件裝飾器  @allure.attach.file("文件路徑", name="命名", attachment_type=allure.attachment_type.文件類型)

 

2. 裝飾器的使用方法

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("登錄成功")
import allure


@allure.feature("登錄模塊")
class TestLogin:
    @allure.story("登錄成功")
    def test_login_success(self):
        print("case:登錄成功")
    
    @allure.title("添加用例名稱")
    def test_login_title(self):
        assert True
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
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

 

 

3. 上述的裝飾器的作用

    1. 如果沒有feature  story  step title attach 等裝飾器,模塊、步驟、附件都是沒有的,不容易閱讀,增加了之后,在生成的報告中,Behaviors項,可查看各功能、步驟信息 

    2. 在pytest 框架運行測試用例的時候,可以根據標簽來指定運行標記的用例,比如 

      根據功能名稱選擇:pytest 文件名 --allure-features "功能名稱"
      根據子功能名稱選擇:pytest 文件名 --allure-stories "子功能名稱"

      pytest 文件名 --allure-severities 用例級別(小寫)

    3. attach  和attachfile標簽,是給用例添加文本說明和附件的,如果某個步驟執行失敗了,截圖了,則可以以附件的方式上傳,或者上傳報錯的日志

 

 

 

 

 

原文地址:https://www.cnblogs.com/llbai/p/15651005.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM