就是數據的改變從而驅動自動化測試的執行,最終引發測試結果的改變。說的直白些,就是參數化的應用html
# 導入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 json
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)
{ "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
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]