python ddt數(shù)據(jù)驅(qū)動(dòng)測(cè)試

DDT,數(shù)據(jù)驅(qū)動(dòng)測(cè)試,是 “Data-Driven Tests”的縮寫。

引入ddt測(cè)試必要性

數(shù)據(jù)驅(qū)動(dòng)測(cè)試,可以實(shí)現(xiàn)一個(gè)測(cè)試方法隨著入?yún)⒌牟煌?,從而?shí)現(xiàn)不同場(chǎng)景的用例測(cè)試。相比較單獨(dú)一個(gè)測(cè)試方法實(shí)現(xiàn)一個(gè)測(cè)試用例,無論在代碼的維護(hù)上,執(zhí)行效率上,都有顯著的提高。

舉個(gè)栗子,我最近在寫一個(gè)篩選接口,入?yún)⒂?個(gè)維度,每個(gè)維度分別有4個(gè)、3個(gè)、4個(gè)。如果我按以往的方法寫,一個(gè)測(cè)試方法只測(cè)試一種場(chǎng)景,我需要寫4x3x4=48 個(gè)測(cè)試方法才能遍歷所有場(chǎng)景。顯然,只是參數(shù)的不同,同一個(gè)方法,需要寫48次,會(huì)很浪費(fèi)時(shí)間。如果引入ddt數(shù)據(jù)驅(qū)動(dòng),就可以用一個(gè)測(cè)試方法,以參數(shù)組合傳入的方式,就可以很快完成48個(gè)測(cè)試用例。

ddt 使用方法

python 的unittest 沒有自帶數(shù)據(jù)驅(qū)動(dòng)功能。
如果使用unittest,同時(shí)又想使用數(shù)據(jù)驅(qū)動(dòng),可以使用DDT來完成。
ddt是第三方模塊,使用ddt需要先安裝,使用pip install ddt 完成安裝。

DDT由一個(gè)類裝飾器ddt(用于您的TestCase子類)和兩個(gè)方法裝飾器:
@ddt: 修飾測(cè)試類
@data():
包含與要提供給測(cè)試的值一樣多的參數(shù)。通常,其中的每個(gè)值都data將作為單個(gè)參數(shù)傳遞給測(cè)試方法。
@unpack
當(dāng)傳遞的參數(shù)是復(fù)雜的數(shù)據(jù)結(jié)構(gòu)時(shí)使用。比如使用元組或者列表,添加unpack之后, 它將自動(dòng)將元組和列表解壓縮為多個(gè)參數(shù),并將字典解壓縮為多個(gè)關(guān)鍵字參數(shù)。
@file_data():
將從JSON或YAML文件加載測(cè)試數(shù)據(jù)。只有以“.yml”和“.yaml”結(jié)尾的文件才會(huì)作為YAML文件加載。所有其他文件都作為JSON文件加載。

代碼示例

  import unittest
  from ddt import ddt, data, unpack, file_data     


 @ddt     #  修飾測(cè)試類
 class LearnDdt(unittest.TestCase):

@data([3, 2], [4, 3], [5, 3])                            # 使用data裝飾器,傳入列表參數(shù),使用unpack解壓縮
@unpack
def test_list_extracted_into_arguments(self, first_value, second_value):
    print(first_value, second_value)
    self.assertTrue(first_value > second_value)

@unpack
@data({'first': 1, 'second': 3, 'third': 2},              # 使用data將參數(shù)按字典格式傳入并指定值,解壓縮后將值傳給參數(shù)
      {'first': 4, 'second': 6, 'third': 5})
def test_dicts_extracted_into_kwargs(self, first, second, third):
    print(first, second, third)
    self.assertTrue(first < third < second)

@data((3, 2), (4, 3), (5, 3))                              # 使用data加入動(dòng)態(tài)元組組合參數(shù),解壓縮將元組的值賦值給參數(shù)
@unpack
def test_tuples_extracted_into_arguments(self, first_value, second_value):
    print(first_value, second_value)
    self.assertTrue(first_value > second_value)

@file_data("test_data_dict_dict.json")                      # 使用@file_data 傳入文件參數(shù) - json文件
def test_file_data_json_dict_dict(self, start, end, value):
    print(start, end, value)
    self.assertLess(start, end)
    self.assertLess(value, end)
    self.assertGreater(value, start)

@file_data('test_data_dict.json')                            # 使用@file_data 傳入文參數(shù),讀取的是字典鍵的值
def test_file_data_json_dict(self, value):
    print(value)

@file_data('test_data_list.json')                              # 使用@file_data 傳入文參數(shù),讀取的是列表的元素值
def test_file_data_json_list(self, value):
    print(value)


if __name__ == "__main__":
       unittest.main()

引用文件參數(shù):
文件1:test_data_dict.json

    {
"unsorted_list": [ 10, 12, 15 ],
"sorted_list": [ 15, 12, 50 ]
   }

文件2:test_data_dict_dict.json

   {
"positive_integer_range": {
    "start": 0,
    "end": 2,
    "value": 1
},
"negative_integer_range": {
    "start": -2,
    "end": 0,
    "value": -1
},
"positive_real_range": {
    "start": 0.0,
    "end": 1.0,
    "value": 0.5
},
"negative_real_range": {
    "start": -1.0,
    "end": 0.0,
    "value": -0.5
}
}

文件3:test_data_list.json

[
"Hello",
"Goodbye"
 ]

參考:
https://ddt.readthedocs.io/en/latest/example.html
https://www.cnblogs.com/miniren/p/7099187.html

嘿嘿,覺得這篇文章對(duì)你有幫助的,伸出你愛心的小手手,點(diǎn)個(gè)紅心唄(看到有紅心就高興,說明堅(jiān)持寫筆記還有幫助的~)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。