python中DDT數(shù)據(jù)驅(qū)動(dòng)的實(shí)現(xiàn)
DDT(Data-Driven Testing,數(shù)據(jù)驅(qū)動(dòng)測(cè)試)是一種軟件測(cè)試方法,通過(guò)外部數(shù)據(jù)源(如Excel、CSV、數(shù)據(jù)庫(kù)等)驅(qū)動(dòng)測(cè)試用例的執(zhí)行。它的核心思想是將測(cè)試數(shù)據(jù)與測(cè)試邏輯分離,從而提高測(cè)試的靈活性和可維護(hù)性。
以下是關(guān)于DDT的詳細(xì)介紹和實(shí)現(xiàn)方法:
1. DDT 的核心概念
測(cè)試數(shù)據(jù)與邏輯分離:
測(cè)試邏輯是固定的,而測(cè)試數(shù)據(jù)可以從外部文件或數(shù)據(jù)庫(kù)中動(dòng)態(tài)加載。
數(shù)據(jù)可以是輸入?yún)?shù)、預(yù)期結(jié)果或配置信息。
數(shù)據(jù)源:
常見(jiàn)的數(shù)據(jù)源包括:CSV文件、Excel文件、JSON文件、數(shù)據(jù)庫(kù)、API等。
測(cè)試用例動(dòng)態(tài)生成:
根據(jù)數(shù)據(jù)源中的每一行數(shù)據(jù),動(dòng)態(tài)生成一個(gè)測(cè)試用例。
2. DDT 的優(yōu)點(diǎn)
提高測(cè)試覆蓋率:
通過(guò)多組數(shù)據(jù)測(cè)試同一邏輯,覆蓋更多場(chǎng)景。
減少代碼重復(fù):
測(cè)試邏輯只需編寫(xiě)一次,數(shù)據(jù)可以動(dòng)態(tài)加載。
易于維護(hù):
當(dāng)測(cè)試數(shù)據(jù)變化時(shí),只需修改數(shù)據(jù)源,而無(wú)需修改測(cè)試代碼。
支持復(fù)雜場(chǎng)景:
可以通過(guò)大量數(shù)據(jù)組合測(cè)試邊界條件和異常情況。
3. DDT 的實(shí)現(xiàn)方法
以下是使用 Python 實(shí)現(xiàn) DDT 的幾種常見(jiàn)方式:
方法 1: unittest 和 ddt 庫(kù)
ddt
是一個(gè) Python 庫(kù),專(zhuān)門(mén)用于數(shù)據(jù)驅(qū)動(dòng)測(cè)試。
安裝 ddt
:
pip install ddt
示例代碼1:
import unittest from ddt import ddt, data, unpack @ddt class TestMathOperations(unittest.TestCase): @data((1, 2, 3), (4, 5, 9), (10, -5, 5)) @unpack def test_addition(self, a, b, expected_result): self.assertEqual(a + b, expected_result) if __name__ == "__main__": unittest.main()
運(yùn)行結(jié)果:
每組數(shù)據(jù)會(huì)生成一個(gè)獨(dú)立的測(cè)試用例。
示例代碼2:
import requests import unittest from ddt import ddt, data test_data = [ {'method':'post', 'url':'http://www.jd.com'}, {'method':'put', 'url':'http://www.jd.com'}, {'method':'put', 'url':'http://www.jd.com'}, ] @ddt class Test(unittest.TestCase): @data(*test_data) def test01(self, case): print('請(qǐng)求', type(case)) res = requests.request(method=case['method'], url=case['url']) print(res) if __name__ == '__main__': unittest.main()
運(yùn)行結(jié)果:
請(qǐng)求 <class 'dict'>
<Response [200]>
請(qǐng)求 <class 'dict'>
<Response [200]>
請(qǐng)求 <class 'dict'><Response [200]>
Ran 3 tests in 0.423sOK
方法 2:使用 pytest 和參數(shù)化
pytest
是一個(gè)功能強(qiáng)大的測(cè)試框架,支持?jǐn)?shù)據(jù)驅(qū)動(dòng)測(cè)試。
示例代碼:
import pytest # 測(cè)試數(shù)據(jù) test_data = [ (1, 2, 3), (4, 5, 9), (10, -5, 5) ] @pytest.mark.parametrize("a, b, expected_result", test_data) def test_addition(a, b, expected_result): assert a + b == expected_result
運(yùn)行結(jié)果:
每組數(shù)據(jù)會(huì)生成一個(gè)獨(dú)立的測(cè)試用例。
方法 3:從外部文件加載數(shù)據(jù)
可以從 CSV、Excel 或 JSON 文件中加載測(cè)試數(shù)據(jù)。
從 CSV 文件加載數(shù)據(jù):
import csv import pytest def load_test_data_from_csv(file_path): test_data = [] with open(file_path, newline='') as csvfile: reader = csv.reader(csvfile) next(reader) # 跳過(guò)表頭 for row in reader: test_data.append(tuple(map(int, row))) # 將數(shù)據(jù)轉(zhuǎn)換為整數(shù) return test_data test_data = load_test_data_from_csv("test_data.csv") @pytest.mark.parametrize("a, b, expected_result", test_data) def test_addition(a, b, expected_result): assert a + b == expected_result
CSV 文件示例 (test_data.csv
):
a,b,expected_result 1,2,3 4,5,9 10,-5,5
從 JSON 文件加載數(shù)據(jù):
import json import pytest def load_test_data_from_json(file_path): with open(file_path) as f: return json.load(f) test_data = load_test_data_from_json("test_data.json") @pytest.mark.parametrize("data", test_data) def test_addition(data): assert data["a"] + data["b"] == data["expected_result"]
JSON 文件示例 (test_data.json
):
[ {"a": 1, "b": 2, "expected_result": 3}, {"a": 4, "b": 5, "expected_result": 9}, {"a": 10, "b": -5, "expected_result": 5} ]
4. DDT 的最佳實(shí)踐
數(shù)據(jù)源管理:
將測(cè)試數(shù)據(jù)存儲(chǔ)在外部文件中,便于維護(hù)和共享。
數(shù)據(jù)格式標(biāo)準(zhǔn)化:
使用統(tǒng)一的格式(如 CSV、JSON)存儲(chǔ)測(cè)試數(shù)據(jù)。
邊界測(cè)試:
在數(shù)據(jù)中包含邊界值和異常值,確保測(cè)試覆蓋全面。
數(shù)據(jù)清理:
在測(cè)試前后清理測(cè)試環(huán)境,避免數(shù)據(jù)污染。
測(cè)試報(bào)告:
生成詳細(xì)的測(cè)試報(bào)告,記錄每組數(shù)據(jù)的測(cè)試結(jié)果。
5. DDT 的應(yīng)用場(chǎng)景
API 測(cè)試:
使用多組輸入?yún)?shù)測(cè)試 API 的響應(yīng)。
UI 測(cè)試:
使用多組數(shù)據(jù)測(cè)試表單提交、登錄等功能。
數(shù)據(jù)庫(kù)測(cè)試:
使用多組數(shù)據(jù)測(cè)試數(shù)據(jù)庫(kù)查詢和寫(xiě)入操作。
性能測(cè)試:
使用多組數(shù)據(jù)模擬不同負(fù)載場(chǎng)景。
到此這篇關(guān)于python中DDT數(shù)據(jù)驅(qū)動(dòng)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)python DDT數(shù)據(jù)驅(qū)動(dòng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python Unittest ddt數(shù)據(jù)驅(qū)動(dòng)的實(shí)現(xiàn)
- python 基于DDT實(shí)現(xiàn)數(shù)據(jù)驅(qū)動(dòng)測(cè)試
- 基于Python的接口自動(dòng)化unittest測(cè)試框架和ddt數(shù)據(jù)驅(qū)動(dòng)詳解
- Python+unittest+DDT實(shí)現(xiàn)數(shù)據(jù)驅(qū)動(dòng)測(cè)試
- python自動(dòng)化測(cè)試之DDT數(shù)據(jù)驅(qū)動(dòng)的實(shí)現(xiàn)代碼
- python ddt數(shù)據(jù)驅(qū)動(dòng)最簡(jiǎn)實(shí)例代碼
- python ddt實(shí)現(xiàn)數(shù)據(jù)驅(qū)動(dòng)
相關(guān)文章
Pyqt+matplotlib 實(shí)現(xiàn)實(shí)時(shí)畫(huà)圖案例
這篇文章主要介紹了Pyqt+matplotlib 實(shí)現(xiàn)實(shí)時(shí)畫(huà)圖案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03python?peewee操作sqlite鎖表的問(wèn)題分析
Peewee是一種簡(jiǎn)單而小的ORM,在使用python?orm?框架?peewee?操作數(shù)據(jù)庫(kù)時(shí)時(shí)常會(huì)拋出以一個(gè)異常,下面我們就來(lái)分享一下具體的原因以及解決辦法吧2023-08-08python實(shí)現(xiàn)超市進(jìn)銷(xiāo)存管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)超市進(jìn)銷(xiāo)存管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06python opencv實(shí)現(xiàn)旋轉(zhuǎn)矩形框裁減功能
這篇文章主要為大家詳細(xì)介紹了python opencv實(shí)現(xiàn)旋轉(zhuǎn)矩形框裁減功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Python編寫(xiě)簡(jiǎn)單的HTML頁(yè)面合并腳本
這篇文章主要介紹了Python編寫(xiě)簡(jiǎn)單的HTML頁(yè)面合并腳本的相關(guān)資料,需要的朋友可以參考下2016-07-07python按照行來(lái)讀取txt文件全部?jī)?nèi)容(去除空行處理掉\t,\n后以列表方式返回)
這篇文章主要介紹了python按照行來(lái)讀取txt文件全部?jī)?nèi)容 ,去除空行,處理掉\t,\n后,以列表方式返回,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06django 開(kāi)發(fā)忘記密碼通過(guò)郵箱找回功能示例
這篇文章主要介紹了django 開(kāi)發(fā)忘記密碼通過(guò)郵箱找回功能示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04python K近鄰算法的kd樹(shù)實(shí)現(xiàn)
這篇文章主要介紹了python K近鄰算法的kd樹(shù)實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09