亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

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

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

  • 可讀性:測(cè)試用例更加清晰易讀,因?yàn)閿?shù)據(jù)被獨(dú)立出來(lái)并以結(jié)構(gòu)化的方式存儲(chǔ)在外部文件中。

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

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

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

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

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

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

首先,創(chuàng)建一個(gè)新的項(xià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.安裝所需庫(kù)

確保安裝所需的庫(kù):

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.編寫測(cè)試用例

在 tests/test_sample.py 中編寫測(cè)試用例:

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.運(yùn)行測(cè)試

現(xiàn)在你可以使用 Pytest 來(lái)運(yùn)行測(cè)試。在命令行中執(zhí)行以下命令:

pytest -v

7.實(shí)際使用示例

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

7.1準(zhǔn)備測(cè)試數(shù)據(jù)

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

UsernamePassword
user1password1
user2password2
user3password3

7.2編寫測(cè)試用例

在 tests/test_sample.py 中編寫測(cè)試用例,使用數(shù)據(jù)驅(qū)動(dòng)的方式來(lái)運(yùn)行測(cè)試:

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  # 導(dǎo)入你的 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}")

    # 使用測(cè)試數(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 注解來(lái)指定參數(shù)化測(cè)試數(shù)據(jù),并在測(cè)試用例中使用這些數(shù)據(jù)來(lái)調(diào)用登錄接口。通過(guò)這種方式,你可以輕松地對(duì)不同的輸入數(shù)據(jù)進(jìn)行測(cè)試,而無(wú)需為每組數(shù)據(jù)編寫單獨(dú)的測(cè)試用例。

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

相關(guān)文章

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

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

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

    打開電腦上的QQ的python代碼

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

    詳解Python如何使用Netmiko進(jìn)行文件傳輸

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

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

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

    Python破解BiliBili滑塊驗(yàn)證碼的思路詳解(完美避開人機(jī)識(shí)別)

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

    記錄Django開發(fā)心得

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

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

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

    Python爬蟲小技巧之偽造隨機(jī)的User-Agent

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

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

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

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

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

最新評(píng)論