1. 程式人生 > >python介面自動化測試 - 資料驅動DDT模組的簡單使用

python介面自動化測試 - 資料驅動DDT模組的簡單使用

DDT簡單介紹

  • 名稱:Data-Driven Tests,資料驅動測試
  • 作用:由外部資料集合來驅動測試用例的執行
  • 核心的思想:資料和測試程式碼分離
  • 應用場景:一組外部資料來執行相同的操作
  • 優點:當測試資料發生大量變化的情況下,測試程式碼可以保持不變
  • 實際專案:excel儲存測試資料,ddt讀取測試資料到單元測試框架(測試用例中),輸出到html報告

 

什麼是資料驅動

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

 

DDT基礎使用(一):傳遞基礎資料型別

# 匯入ddt庫下所有內容
from ddt import *


# 在測試類前必須首先宣告使用 ddt
@ddt
class imoocTest(unittest.TestCase):
    
    # int
    @data(1, 2, 3, 4)
    def test_int(self, i):
        print("test_int:", i)

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

測試結果

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

包含知識點

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

 

DDT基礎使用(二):傳遞一個複雜的資料結構 

from ddt import *


# 在測試類前必須首先宣告使用 ddt
@ddt
class imoocTest(unittest.TestCase):
    tuples = ((1, 2, 3), (1, 2, 3))
    lists = [[1, 2, 3], [1, 2, 3]]

    # 元組
    @data((1, 2, 3), (1, 2, 3))
    def test_tuple(self, n):
        print("test_tuple", n)

    # 列表
    @data([1, 2, 3], [1, 2, 3])
    @unpack
    def test_list(self, n1, n2, n3):
        print("test_list", n1, n2, n3)

    # 元組2
    @data(*tuples)
    def test_tuples(self, n):
        print("test_tuples", n)

    # 列表2
    @data(*lists)
    @unpack
    def test_lists(self, n1, n2, n3):
        print("test_lists", n1, n2, n3)

    # 字典
    @data({'value1': 1, 'value2': 2}, {'value1': 1, 'value2': 2})
    @unpack
    def test_dict(self, value1, value2):
        print("test_dict", value1, value2)

測試結果

test_dict 1 2
test_dict 1 2
test_list 1 2 3
test_list 1 2 3
test_lists 1 2 3
test_lists 1 2 3
test_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
@ddt
class imoocTest(unittest.TestCase):

    @file_data('F:/test/config/testddt.json')
    def test_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
  - 12

sorted_list: [ 15, 12, 50 ]

單元測試類

from ddt import *


# 在測試類前必須首先宣告使用 ddt
@ddt
class imoocTest(unittest.TestCase):

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

測試結果

yaml [10, 15, 12]
yaml [15, 12, 50]

&n