数据驱动

1、什么是数据驱动

业务流程是固定的,变化的是业务数据,即使用场景为:业务流程不变,但是请求数据不一样

2、使用数据驱动

在这里讲解两种数据驱动的方式

方式1:第三方库ddt

pip install ddt

import unittest
from ddt import ddt, data
@ddt
class Test_Location_Api(unittest.TestCase):
    test_cases = [{'lng': '120.58', 'lat': '31.3', 'type': 1},
                  {'lng': '119.4', 'lat': '32.4', 'type': 3},
                  {'lng': '120.15', 'lat': '33.35', 'type': 5},
                  {'lng': '121.47', 'lat': '31.23', 'type': 6}]

    @data(*test_cases) # 装饰器拆包,可理解为循环遍历test_cases
    def test_location_01(self, test_case):# test_case为循环遍历test_cases中的每个元素
        pass

方式2:unittestreport封装的list_data

直接使用之前讲解的unittestreport封装好了的装饰器

import unittest
from unittestreport import ddt, list_data

@ddt
class Test_Location_Api(unittest.TestCase):
    test_cases = [{'lng': '120.58', 'lat': '31.3', 'type': 1},
                  {'lng': '119.4', 'lat': '32.4', 'type': 3},
                  {'lng': '120.15', 'lat': '33.35', 'type': 5},
                  {'lng': '121.47', 'lat': '31.23', 'type': 6}]

    @list_data(test_cases)# 这里无需打上*号,list_data中已经处理好
    def test_location_01(self, test_case):
       pass
posted @ 2022-05-19 15:21  少年不太冷2  阅读(89)  评论(0)    收藏  举报