自动化-ddt数据驱动

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

data1 = ((1, 2, 3), (4, 5, 6))
data2 = [{'name': 'tom', 'score': 90}, {'name': 'jane', 'score': 80}]


@ddt
class MyTest(unittest.TestCase):
    def setUp(self):
        print('start')

    @data(*data1)
    @unpack
    def test_1(self, a, b, c, ):
        print(a, b, c)

    @data(*data2)
    @unpack
    def test_2(self, name, score):
        print('name:', name)
        print('score:', score)

    # ** testdata:将提取到的数据存放在空字典testdata中
    @file_data("test.json")
    def test_3(self, **testdata):
        brand = testdata['brand']
        color = testdata['color']
        price = testdata['price']
        print(brand, color, price)
        print(testdata)

    @file_data("test.json")
    def test_4(self,brand,color,price ):
        print(brand, color, price)

    def tearDown(self):
        print('tearDown')


if __name__ == "__main__":
    unittest.main()
test.json
{
"car1":{
"brand":"BMW",
"color":"red",
"price":"50"
},
"car2":{
"brand":"Benz",
"color":"black",
"price":"34"
}
}
posted @ 2021-01-18 16:14  静惜  阅读(77)  评论(0)    收藏  举报