一分钟明白 Python-unittest ddt数据驱动

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

def myList(a, b):
    r = [a, b]
    return r

#读取cvs文件数据
def getCsv(file_name):
    rows=[]
    with open(file_name,encoding='utf-8') as f:
        readers = csv.reader(f)
        for row in readers:
            rows.append(row)
    return rows

@ddt
class MyTestCase(unittest.TestCase):
    """测试框架使用Unittest和DDT相结合的方式"""
    def setUp(self):
        pass

    @data(['key1','1'],['key2','2'],['key3','3'])
    @unpack
    def test_data_list(self,key,value):
        print(key)
        print(value)

    @data(3,4,12,23)
    def test_data(self,value):
        '''单个值'''
        print(value)

    @data(myList(1,2))
    def test_data_fun(self,value):
        '''函数'''
        print(value)

    @file_data("test_data_dict.json")
    def test_data_file_data_dict(self,value1):
        '''加载dict_json文件'''
        print(value1)

    @file_data("test_data_list.json")
    def test_data_file_data_list(self,value1):
        '''加载dict_json文件'''
        print(value1)

    @data(*getCsv('mycsv.csv'))
    @unpack
    def test_data_file_data_list(self,v1,v2,v3):
        '''加载csv文件'''
        print(v1)
        print(v2)
        print(v3)

    def tearDown(self):
        pass
    
if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(MyTestCase)
    unittest.TextTestRunner(verbosity=2).run(suite)

你可能感兴趣的:(UI,自动化面试题,python,unittest,ddt数据驱动)