pytestallure詳解

一、什麼是pytestallure

pytestallure是一個基於pytest測試框架的擴展,它可以為測試結果生成漂亮的HTML報告,讓測試結果更加直觀、易讀。pytestallure的主要功能包括:測試結果展示、歷史記錄比較、執行時間計算、測試結果按模塊分組等。

二、pytestallure的安裝和配置

1、安裝pytestallure支持包:

$ pip install pytest-allure-adaptor

2、安裝allure-cli:

$ brew install allure

3、在項目中加入pytest.ini配置文件,配置pytestallure插件:

[pytest]
addopts = --alluredir ./allure_report
testpaths = ./tests

# 配置allure插件
pytest_allure_adaptor_host = localhost
pytest_allure_adaptor_port = 8080
pytest_allure_adaptor_reporter = allure
pytest_allure_adaptor_reporter_junit_path = ./allure_report/junit.xml
pytest_allure_adaptor_reporter_allure_path = ./allure_report/allure-report
pytest_allure_adaptor_reporter_others_path = ./allure_report/others

三、pytestallure的使用

1、pytestallure的基本用法是通過pytest的命令行參數來調用插件:

$ pytest --alluredir ./allure_report

2、使用pytestmark標記來標記測試用例的信息,例如優先順序、故障原因等:

@pytest.mark.smoke
def test_demo():
    '''這是一個示例測試函數'''
    assert True

3、使用pytest-allure-adaptor提供的裝飾器來分類測試用例的結果:

from allure_commons.types import Severity, Status
import allure

@allure.feature('購物車功能模塊')
@allure.story('加入商品到購物車')
@allure.severity(Severity.CRITICAL)
def test_add_to_cart():
    '''測試加入商品到購物車功能'''
    assert True

@allure.feature('購物車功能模塊')
@allure.story('從購物車刪除商品')
def test_remove_from_cart():
    '''測試從購物車刪除商品功能'''
    assert True

4、使用pytest-allure-adaptor提供的裝飾器來生成測試步驟和截圖:

@allure.step('打開瀏覽器')
def open_browser():
    pass

@allure.step('輸入用戶名和密碼')
def input_username_and_password():
    pass

@allure.step('登錄')
def click_login():
    pass

def test_login():
    '''測試登錄功能'''
    open_browser()
    input_username_and_password()
    click_login()
    allure.attach('登錄成功截圖', '***')

@allure.step('添加商品到購物車')
def add_to_cart():
    pass

@allure.step('檢查購物車是否有商品')
def check_cart():
    pass

def test_shopping_cart():
    '''測試購物車功能'''
    add_to_cart()
    check_cart()
    allure.attach('購物車內容截圖', '***')

5、pytestallure還支持將測試結果按模塊分組、比較歷史記錄等功能,可以通過pytest-allure-adaptor的命令行參數來進行配置。

四、pytestallure實踐

下面是一個利用pytestallure進行web自動化測試的示例代碼:

import allure
from selenium import webdriver

@allure.feature('百度搜索功能')
class TestBaiduSearch:
    def setup_class(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()

    def teardown_class(self):
        self.driver.quit()

    @allure.story('基本搜索')
    def test_basic_search(self):
        self.driver.get('https://www.baidu.com')
        self.driver.find_element_by_id('kw').send_keys('pytest-allure')
        self.driver.find_element_by_id('su').click()
        assert 'pytest-allure' in self.driver.title
        allure.attach(self.driver.get_screenshot_as_png(), '基本搜索截圖', allure.attachment_type.PNG) 

    @allure.story('高級搜索')
    def test_advanced_search(self):
        self.driver.get('https://www.baidu.com')
        self.driver.find_element_by_link_text('高級搜索').click()
        self.driver.find_element_by_name('q1').send_keys('pytest-allure')
        self.driver.find_element_by_id('domain').send_keys('github.com')
        self.driver.find_element_by_id('su').click()
        assert '百度搜索-更詳細的搜索結果' in self.driver.title
        allure.attach(self.driver.get_screenshot_as_png(), '高級搜索截圖', allure.attachment_type.PNG) 

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/190618.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-30 09:05
下一篇 2024-11-30 09:05

相關推薦

  • 神經網路代碼詳解

    神經網路作為一種人工智慧技術,被廣泛應用於語音識別、圖像識別、自然語言處理等領域。而神經網路的模型編寫,離不開代碼。本文將從多個方面詳細闡述神經網路模型編寫的代碼技術。 一、神經網…

    編程 2025-04-25
  • Linux sync詳解

    一、sync概述 sync是Linux中一個非常重要的命令,它可以將文件系統緩存中的內容,強制寫入磁碟中。在執行sync之前,所有的文件系統更新將不會立即寫入磁碟,而是先緩存在內存…

    編程 2025-04-25
  • Linux修改文件名命令詳解

    在Linux系統中,修改文件名是一個很常見的操作。Linux提供了多種方式來修改文件名,這篇文章將介紹Linux修改文件名的詳細操作。 一、mv命令 mv命令是Linux下的常用命…

    編程 2025-04-25
  • C語言貪吃蛇詳解

    一、數據結構和演算法 C語言貪吃蛇主要運用了以下數據結構和演算法: 1. 鏈表 typedef struct body { int x; int y; struct body *nex…

    編程 2025-04-25
  • nginx與apache應用開發詳解

    一、概述 nginx和apache都是常見的web伺服器。nginx是一個高性能的反向代理web伺服器,將負載均衡和緩存集成在了一起,可以動靜分離。apache是一個可擴展的web…

    編程 2025-04-25
  • Python輸入輸出詳解

    一、文件讀寫 Python中文件的讀寫操作是必不可少的基本技能之一。讀寫文件分別使用open()函數中的’r’和’w’參數,讀取文件…

    編程 2025-04-25
  • Python安裝OS庫詳解

    一、OS簡介 OS庫是Python標準庫的一部分,它提供了跨平台的操作系統功能,使得Python可以進行文件操作、進程管理、環境變數讀取等系統級操作。 OS庫中包含了大量的文件和目…

    編程 2025-04-25
  • git config user.name的詳解

    一、為什麼要使用git config user.name? git是一個非常流行的分散式版本控制系統,很多程序員都會用到它。在使用git commit提交代碼時,需要記錄commi…

    編程 2025-04-25
  • MPU6050工作原理詳解

    一、什麼是MPU6050 MPU6050是一種六軸慣性感測器,能夠同時測量加速度和角速度。它由三個感測器組成:一個三軸加速度計和一個三軸陀螺儀。這個組合提供了非常精細的姿態解算,其…

    編程 2025-04-25
  • Java BigDecimal 精度詳解

    一、基礎概念 Java BigDecimal 是一個用於高精度計算的類。普通的 double 或 float 類型只能精確表示有限的數字,而對於需要高精度計算的場景,BigDeci…

    編程 2025-04-25

發表回復

登錄後才能評論