欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

手把手帶你打造一個Pytest數(shù)據(jù)分離測試框架

 更新時間:2024年03月25日 17:02:25   作者:旦莫  
數(shù)據(jù)分離測試框架是一種測試框架設(shè)計模式,旨在將測試數(shù)據(jù)與測試邏輯分離,以提高測試用例的可維護性、可讀性和復用性,本文就來實現(xiàn)一下,感興趣的可以了解一下

數(shù)據(jù)分離測試框架是一種測試框架設(shè)計模式,旨在將測試數(shù)據(jù)與測試邏輯分離,以提高測試用例的可維護性、可讀性和復用性。這種框架通常用于自動化測試,特別是在接口測試、UI 測試和集成測試中非常有用。

在數(shù)據(jù)分離測試框架中,測試數(shù)據(jù)通常存儲在外部文件(如 Excel、CSV、JSON 等)中,而測試邏輯則編寫在測試用例中。通過將測試數(shù)據(jù)與測試邏輯分開,可以實現(xiàn)以下優(yōu)勢:

  • 易維護性:測試數(shù)據(jù)的變化不會影響測試邏輯,反之亦然。當測試數(shù)據(jù)需要更新時,只需修改數(shù)據(jù)文件而不必修改測試用例代碼。

  • 可讀性:測試用例更加清晰易讀,因為數(shù)據(jù)被獨立出來并以結(jié)構(gòu)化的方式存儲在外部文件中。

  • 復用性:可以重復使用相同的測試邏輯,只需提供不同的測試數(shù)據(jù)即可運行多個測試場景。

  • 擴展性:隨著測試需求的增加,可以很容易地添加新的測試數(shù)據(jù)文件,而無需改動現(xiàn)有的測試用例。

  • 靈活性:可以使用不同類型的數(shù)據(jù)文件進行數(shù)據(jù)分離,根據(jù)具體需求選擇最適合的數(shù)據(jù)存儲格式。

數(shù)據(jù)分離測試框架通常包括數(shù)據(jù)讀取工具、測試邏輯編寫、日志記錄和報告生成等功能。通過有效地組織和管理測試數(shù)據(jù),測試團隊可以更高效地執(zhí)行測試,并快速準確地識別潛在的問題。

開發(fā)一個復雜的數(shù)據(jù)驅(qū)動測試框架涉及到多個方面,包括數(shù)據(jù)讀取、日志記錄、郵件發(fā)送、配置文件使用以及清晰的代碼目錄結(jié)構(gòu)等。讓我們一步一步來完成這個任務(wù)。

1.創(chuàng)建項目目錄結(jié)構(gòu)

首先,創(chuàng)建一個新的項目目錄結(jié)構(gòu),并包含以下子目錄和文件:

data_driven_testing_framework/
    ├── configs/
    │   └── config.ini
    ├── data/
    │   └── test_data.xlsx
    ├── logs/
    ├── tests/
    │   ├── __init__.py
    │   └── test_sample.py
    ├── utils/
    │   ├── __init__.py
    │   ├── excel_reader.py
    │   ├── logger.py
    │   ├── mailer.py
    └── pytest.ini

2.安裝所需庫

確保安裝所需的庫:

pip install pytest openpyxl configparser logging yagmail

3.編寫配置文件

在 configs/config.ini 中定義配置參數(shù):

[EMAIL]
email_address = your_email@example.com
email_password = your_email_password

[LOGGING]
log_file = logs/test.log

4. 編寫工具類

在 utils/excel_reader.py 中編寫 Excel 數(shù)據(jù)讀取工具類:

import openpyxl

class ExcelReader:
    @staticmethod
    def read_data(file_path):
        wb = openpyxl.load_workbook(file_path)
        sheet = wb.active
        data = []
        for row in sheet.iter_rows(min_row=2, values_only=True):
            data.append(row)
        return data

在 utils/logger.py 中編寫日志記錄工具類:

import logging
import configparser

config = configparser.ConfigParser()
config.read('configs/config.ini')

log_file = config['LOGGING']['log_file']

logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

在 utils/mailer.py 中編寫發(fā)送郵件工具類:

import yagmail
import configparser

config = configparser.ConfigParser()
config.read('configs/config.ini')

email_address = config['EMAIL']['email_address']
email_password = config['EMAIL']['email_password']

class Mailer:
    @staticmethod
    def send_email(subject, contents):
        yag = yagmail.SMTP(email_address, email_password)
        yag.send(to=email_address, subject=subject, contents=contents)

5.編寫測試用例

在 tests/test_sample.py 中編寫測試用例:

import pytest
from utils.excel_reader import ExcelReader
from utils.logger import logging
from utils.mailer import Mailer

test_data_file = 'data/test_data.xlsx'

@pytest.mark.parametrize("data", ExcelReader.read_data(test_data_file))
def test_data_driven(data):
    logging.info(f"Running test with data: {data}")
    # Your test logic here

    assert True

def test_send_email():
    Mailer.send_email("Test Email", "This is a test email sent from the data-driven testing framework")

6.運行測試

現(xiàn)在你可以使用 Pytest 來運行測試。在命令行中執(zhí)行以下命令:

pytest -v

7.實際使用示例

在接口測試中,你可以使用這個框架來執(zhí)行數(shù)據(jù)驅(qū)動測試。例如,你可以從 Excel 文件中讀取測試數(shù)據(jù),然后在測試用例中使用這些數(shù)據(jù)來調(diào)用接口,并斷言結(jié)果是否符合預(yù)期。

7.1準備測試數(shù)據(jù)

首先,準備一個 Excel 文件,例如 test_data.xlsx,其中包含了不同的測試數(shù)據(jù)。假設(shè)我們要測試一個登錄接口,測試數(shù)據(jù)文件內(nèi)容如下:

UsernamePassword
user1password1
user2password2
user3password3

7.2編寫測試用例

在 tests/test_sample.py 中編寫測試用例,使用數(shù)據(jù)驅(qū)動的方式來運行測試:

import pytest
from utils.excel_reader import ExcelReader
from utils.logger import logging
from utils.mailer import Mailer
from your_api_client_module import APIClient  # 導入你的 API 客戶端模塊

test_data_file = 'data/test_data.xlsx'

@pytest.mark.parametrize("username, password", ExcelReader.read_data(test_data_file))
def test_login_api(username, password):
    logging.info(f"Running test with data: Username - {username}, Password - {password}")

    # 使用測試數(shù)據(jù)調(diào)用登錄接口
    api_client = APIClient()
    response = api_client.login(username, password)

    # 斷言登錄結(jié)果是否符合預(yù)期
    assert response.status_code == 200
    assert 'token' in response.json()

def test_send_email():
    Mailer.send_email("Test Email", "This is a test email sent from the data-driven testing framework")

在上面的示例中,我們使用 @pytest.mark.parametrize 注解來指定參數(shù)化測試數(shù)據(jù),并在測試用例中使用這些數(shù)據(jù)來調(diào)用登錄接口。通過這種方式,你可以輕松地對不同的輸入數(shù)據(jù)進行測試,而無需為每組數(shù)據(jù)編寫單獨的測試用例。

到此這篇關(guān)于手把手帶你打造一個Pytest數(shù)據(jù)分離測試框架的文章就介紹到這了,更多相關(guān)Pytest數(shù)據(jù)分離測試內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Windows下實現(xiàn)將Pascal VOC轉(zhuǎn)化為TFRecords

    Windows下實現(xiàn)將Pascal VOC轉(zhuǎn)化為TFRecords

    今天小編就為大家分享一篇Windows下實現(xiàn)將Pascal VOC轉(zhuǎn)化為TFRecords,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • 打開電腦上的QQ的python代碼

    打開電腦上的QQ的python代碼

    使用python打開電腦上的QQ,方法很簡單,調(diào)用os模塊,然后os.startfile即可
    2013-02-02
  • 詳解Python如何使用Netmiko進行文件傳輸

    詳解Python如何使用Netmiko進行文件傳輸

    Netmiko是一個用于連接和管理各種網(wǎng)絡(luò)設(shè)備的Python庫,它是Paramiko的一個擴展。本文就來講講如何利用Netmiko實現(xiàn)文件傳輸功能吧
    2023-05-05
  • 使用python requests模塊發(fā)送http請求及接收響應(yīng)的方法

    使用python requests模塊發(fā)送http請求及接收響應(yīng)的方法

    用 python 編寫 http request 消息代碼時,建議用requests庫,因為requests比urllib內(nèi)置庫更為簡捷,requests可以直接構(gòu)造get,post請求并發(fā)送,本文給大家介紹了使用python requests模塊發(fā)送http請求及接收響應(yīng)的方法,需要的朋友可以參考下
    2024-03-03
  • Python破解BiliBili滑塊驗證碼的思路詳解(完美避開人機識別)

    Python破解BiliBili滑塊驗證碼的思路詳解(完美避開人機識別)

    這篇文章主要介紹了Python破解BiliBili滑塊驗證碼的思路,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • 記錄Django開發(fā)心得

    記錄Django開發(fā)心得

    學習使用Django也有一個月了,也該整理整理了,給自己,也給大家分享一下哈(再次鄙視國內(nèi)的IT技術(shù)文檔/問答環(huán)境,抄抄抄,你們TM自己寫點不行?。。。。。?當然我剛學Django/Python,也歡迎指點錯誤的地方,
    2014-07-07
  • 如何實現(xiàn)在jupyter notebook中播放視頻(不停地展示圖片)

    如何實現(xiàn)在jupyter notebook中播放視頻(不停地展示圖片)

    這篇文章主要介紹了如何實現(xiàn)在jupyter notebook中播放視頻(不停地展示圖片),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • Python爬蟲小技巧之偽造隨機的User-Agent

    Python爬蟲小技巧之偽造隨機的User-Agent

    這篇文章主要給大家介紹了關(guān)于Python爬蟲小技巧之偽造隨機的User-Agent的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-09-09
  • Django使用Celery實現(xiàn)異步發(fā)送郵件

    Django使用Celery實現(xiàn)異步發(fā)送郵件

    這篇文章主要為大家詳細介紹了Django如何使用Celery實現(xiàn)異步發(fā)送郵件的功能,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2023-04-04
  • Python sort 自定義函數(shù)排序問題

    Python sort 自定義函數(shù)排序問題

    這篇文章主要介紹了Python sort 自定義函數(shù)排序問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09

最新評論