python allure的介紹和使用(持續更新中)

前言:是不是很意外,我又和大家見面了,這個章節咱們學習python allure的使用
1、allure 的介紹
在這裏插入圖片描述
2、allure 的報告概覽
在這裏插入圖片描述
在這裏插入圖片描述
3、allure的安裝
在這裏插入圖片描述
4、使用allure2生成更加精美的測試報告
pip install allure-pytest(安裝這個輔助allure生成測試報告)
pytest --alluredir=指定路徑(指定allure報告數據生成路徑)
allure serve 報告路徑(生成HTML報告,這個會直接在線打開報告)
allure generate ./result/5 -o ./report/5/ --clean(指定生成報告的路徑)
allure open -h 127.0.0.1 -p 8888 ./report/5(啓動本地服務生成鏈接查看報告)
在這裏插入圖片描述
在這裏插入圖片描述
5、allure常用的特性
在這裏插入圖片描述
6、@alllure.feature與@allure.store的關係
在這裏插入圖片描述
7、@allure.step()與with allure.step():的區別
在這裏插入圖片描述
8、allure 用issue與testcase關聯
在這裏插入圖片描述
9、給測試用例劃分優先級
在這裏插入圖片描述
10、給allure 測試報告添加內容(圖片、附件、文本、截圖、HTML等)
在這裏插入圖片描述
11、實戰演練
實例1:

import pytest
import allure
@allure.feature("這是登錄模塊測試用例")
class Test_login():
    @allure.story("用戶名正確,登錄成功")
    @allure.severity(allure.severity_level.BLOCKER)     #阻塞
    def test_logina(self):
        allure.attach("這是一個純文本",name="文本信息",attachment_type=allure.attachment_type.TEXT)    #添加文本
        print("這是登錄,用戶名正確,登錄成功")
        pass

    @allure.story("密碼正確,登錄成功")
    @allure.severity(allure.severity_level.CRITICAL)    #嚴重
    def test_loginb(self):
        allure.attach("<body>這是一個網頁</body>",name="HTML測試模塊",attachment_type=allure.attachment_type.HTML)    #添加網頁

        print("這是登錄,密碼正確,登錄成功")
        pass

    @allure.story("用戶名錯誤,登錄失敗")
    # --allure-link-pattern=issue:https://blog.csdn.net/weixin_44275820/article/details/105169871/issue/{}
    @allure.issue("10086","這是一個bug,需要修復")
    @allure.severity(allure.severity_level.NORMAL)    #正常問題
    def test_loginc(self):
        allure.attach.file("./picture/微信頭像.jpg",name="這是一個圖片",attachment_type=allure.attachment_type.JPG)    #添加圖片
        print("這是登錄,用戶名錯誤,登錄失敗")
        pass

    @allure.story("密碼錯誤,登錄失敗")
    @allure.link("https://blog.csdn.net/weixin_44275820/article/details/105169871",name="我的博客")
    @allure.severity(allure.severity_level.MINOR)    #不太重要
    def test_logind(self):
        with allure.step("點擊用戶名輸入框"):
            print("輸入用戶名")
        with allure.step("點擊輸入密碼輸入框"):
            print("輸入密碼")
        print("點擊登錄按鈕")
        with allure.step("點擊登錄後登錄失敗"):
            assert "1" == 1
            print("這是登錄,密碼錯誤,登錄失敗")
        pass

    Testcase_link = "https://blog.csdn.net/weixin_44275820/article/details/105169871"
    @allure.story("用戶不存在,登錄失敗")
    @allure.testcase(Testcase_link,"我的博客管理平臺")
    @allure.severity(allure.severity_level.TRIVIAL)    #不重要
    def test_logine(self):
        print("這是登錄,用戶不存在,請重新註冊")
        pass

    @allure.story("密碼已鎖定,登錄失敗")
    def test_loginf(self):
        print("這是登錄,密碼已鎖定,請重置密碼")
        pass

    @allure.story("密碼爲空,登錄失敗")
    def test_loging(self):
        print("這是登錄,密碼爲空,請輸入密碼")
        pass

if __name__ =='__main__':
    pytest.main("-v -s")

實例2:

import pytest
import allure
import time
from selenium import webdriver

Testcase_link1 = "https://www.baidu.com"
@allure.testcase(Testcase_link1,"百度,你值得擁有")
@allure.feature("百度搜索")
@pytest.mark.parametrize("search_data",["奔馳","寶馬","保時捷"])
def test_search(search_data):

    with allure.step("打開百度網頁"):
        driver = webdriver.chrome("C:\\Users\liwenliang\AppData\Local\Google\Chrome\Application\chrome.exe")
        driver.get("https://www.baidu.com")

    with allure.step(f"輸入搜索詞",{Testcase_link1}):
        driver.find_element_by_id("KW").send_keys(search_data)
        time.sleep(3)
        driver.find_element_by_id("SU").click()
        time.sleep(3)

    with allure.step("保存圖片"):
        driver.save_screenshot("./result/b.png")
        allure.attach.file("./result/b.png",name="這是保存的圖片",attachment_type=allure.attachment_type.PNG)

    with allure.step("關閉瀏覽器"):
        driver.quit()

if __name__ =='__main__':
    pytest.main("-v -s")

12、數據驅動
數據驅動分爲源數據驅動和步驟數據驅動
在這裏插入圖片描述
13、數據驅動的邏輯
在這裏插入圖片描述
在這裏插入圖片描述
我們這裏直接用yaml做數據驅動,yaml的基礎資料請看一下網址:
https://www.ruanyifeng.com/blog/2016/07/yaml.html
https://yaml.org/spec/1.1/#id857168 1
https://pyyaml.org/wiki/PyYAMLDocumentation

def data():
    with open("test_data.yaml") as f:
        yaml.load(f)

14、allure2的解析過程
安裝allure2
生成allure測試結果 pytest --alluredir=allure .
展示報告 allure serve allure/
生成最終版本的報告 allure generate allure/
使用allure2提供的api,增強報告
截圖、錄像、日誌、鏈接、步驟

待更新。。。。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章