python利用ddt做数据驱动

读取csv文件的函数

 

def getCsvData(file):
    # 读取CSV文件
    value_rows = []
    base_dir = str(os.path.dirname(os.path.dirname(__file__)))
    base_dir = base_dir.replace('\\', '/')
    csv_path = base_dir + file   # csv路径
    with open(csv_path) as f:   # , encoding='UTF-8'
        f_csv = csv.reader(f)
        next(f_csv)
        for r in f_csv:
            value_rows.append(r)
    print(value_rows)
    return value_rows

 

@ddt
class RyomsTest(unittest.TestCase):
    """大运营模块测试"""
    def setUp(self):
        pass

    def tearDown(self):
        pass

    @data(*getCsvData("/data/ryoms_app_center.csv"))
    @unpack   # 解包
    # @file_data("/data/ryoms_app_center.csv")
    def test_easy_smart_basic_module_mall(self, data, alert_data):
        """应用中心-参数配置-商场配置-获取商场配置接口/easy-smart-basic/module/mall"""
        print(data)
        url = env_url + "/easy-smart-basic/module/mall"
        data = eval(data)  # 去除csv带来的""引号
        r = requests.post(url, headers=header, json=data)
        print(r.status_code)
        print(r.json())
        result = r.json()
        self.assertEqual(r.status_code, 200)
        self.assertEqual(result['meta']['msg'], alert_data)
        self.assertEqual(result['success'], True)

 

你可能感兴趣的:(python)