日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 数据驱动接口自动化框架_python接口自动化测试 - 数据驱动DDT模块的简单使用...

發布時間:2023/12/4 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 数据驱动接口自动化框架_python接口自动化测试 - 数据驱动DDT模块的简单使用... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

DDT簡單介紹

名稱:Data-Driven Tests,數據驅動測試

作用:由外部數據集合來驅動測試用例的執行

核心的思想:數據和測試代碼分離

應用場景:一組外部數據來執行相同的操作

優點:當測試數據發生大量變化的情況下,測試代碼可以保持不變

實際項目:excel存儲測試數據,ddt讀取測試數據到單元測試框架(測試用例中),輸出到html報告

什么是數據驅動

就是數據的改變從而驅動自動化測試的執行,最終引起測試結果的改變。說的直白些,就是參數化的應用

DDT基礎使用(一):傳遞基礎數據類型

#導入ddt庫下所有內容

from ddt import *

#在測試類前必須首先聲明使用 ddt

@ddtclassimoocTest(unittest.TestCase):#int

@data(1, 2, 3, 4)deftest_int(self, i):print("test_int:", i)#str

@data("1", "2", "3")deftest_str(self, str):print("test_str:", str)

測試結果

test_int: 1test_int:2test_int:3test_int:4test_str:1test_str:2test_str:3

包含知識點

想使用DDT首先要在單元測試類上面加上?@ddt

DDT基礎使用(二):傳遞一個復雜的數據結構

from ddt import *

#在測試類前必須首先聲明使用 ddt

@ddtclassimoocTest(unittest.TestCase):

tuples= ((1, 2, 3), (1, 2, 3))

lists= [[1, 2, 3], [1, 2, 3]]#元組

@data((1, 2, 3), (1, 2, 3))deftest_tuple(self, n):print("test_tuple", n)#列表

@data([1, 2, 3], [1, 2, 3])

@unpackdeftest_list(self, n1, n2, n3):print("test_list", n1, n2, n3)#元組2

@data(*tuples)deftest_tuples(self, n):print("test_tuples", n)#列表2

@data(*lists)

@unpackdeftest_lists(self, n1, n2, n3):print("test_lists", n1, n2, n3)#字典

@data({'value1': 1, 'value2': 2}, {'value1': 1, 'value2': 2})

@unpackdeftest_dict(self, value1, value2):print("test_dict", value1, value2)

測試結果

test_dict 1 2test_dict1 2test_list1 2 3test_list1 2 3test_lists1 2 3test_lists1 2 3test_tuple (1, 2, 3)

test_tuple (1, 2, 3)

test_tuples (1, 2, 3)

test_tuples (1, 2, 3)

包含知識點

@unpack?:當傳遞的是復雜的數據結構時使用。比如使用元組或者列表,添加?@unpack?之后,?ddt?會自動把元組或者列表對應到多個參數上。字典也可以這樣處理

當沒有加unpack時,test_case方法的參數只能填一個;如元組的例子

當你加了unpack時,傳遞的數據量需要一致;如列表例子中,每個列表我都固定傳了三個數據,當你多傳或少傳時會報錯,而test_case方法的參數也要寫三個,需要匹配上

當傳的數據是字典類型時,要注意每個字典的key都要一致,test_case的參數的命名也要一致;如字典的例子,兩個字典的key都是value1和value2,而方法的參數也是

當傳的數據是通過變量的方式,如元組2、列表2,變量前需要加上*

DDT基礎使用(三):傳遞json文件

json文件

{"first": [

{"isRememberMe": "True","password": "111111","username": "root"},"200"],"second": ["{'isRememberMe': True, 'password': '1111111', 'username': 'root'}","406"],"third": [1,2],"four": "123123"}

單元測試類

from ddt import *

#在測試類前必須首先聲明使用 ddt

@ddtclassimoocTest(unittest.TestCase):

@file_data('F:/test/config/testddt.json')deftest_json(self, data):print(data)

測試結果

[{'isRememberMe': 'True', 'password': '111111', 'username': 'root'}, '200']

["{'isRememberMe': True, 'password': '1111111', 'username': 'root'}", '406']

[1, 2, 3, 4]123123

DDT基礎使用(四):傳遞Yaml文件

yaml文件

unsorted_list:- 10

- 15

- 12sorted_list: [15, 12, 50 ]

單元測試類

from ddt import *

#在測試類前必須首先聲明使用 ddt

@ddtclassimoocTest(unittest.TestCase):

@file_data('F:/test/config/testddt.yaml')deftest4(self, data):print("yaml", data)

測試結果

yaml [10, 15, 12]

yaml [15, 12, 50]

文章來源: www.cnblogs.com,作者:小菠蘿測試筆記,版權歸原作者所有,如需轉載,請聯系作者。

原文鏈接:https://www.cnblogs.com/poloyy/p/12274265.html

總結

以上是生活随笔為你收集整理的python 数据驱动接口自动化框架_python接口自动化测试 - 数据驱动DDT模块的简单使用...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。