python数据驱动_ddt入门

https://blog.csdn.net/u013440574/article/details/81903666

官网文档:https://ddt.readthedocs.io/en/latest/

 

DDT (Data-Driven Tests) allows you to multiply one test case by running it with different test data, and make it appear as multiple test cases.一个测试用例可以用不同的测试数据执行多次。

 

DDT consists of a class decorator ddt (for your TestCase subclass) and two method decorators (for your tests that want to be multiplied):

  • data: contains as many arguments as values you want to feed to the test.
  • file_data: will load test data from a JSON or YAML file.

有一个类装饰器 ddt,两个方法装饰器 data, file_data 支持jason和yaml文件

 加了unpack装饰器是把元祖,列表,字典拆开传给测试案例

通常情况下,data中的数据按照一个参数传递给测试用例,如果data中含有:  元组,列表,字典等数据,默认是pack(包裹一起的),即一个列表作为一个变量  传给函数里的变量。如果把列表的数据进行分解,必须加上@unpack。

@data([a,b],[c,d]) 
无@unpack,那么[a,b]一个列表,作为一个参数传给测试用例 
有@unpack,那么[a,b]被分解开,作为两个参数a,b 分别传给测试用例

import unittest
from ddt import ddt,data,unpack

@ddt
class MyTesting(unittest.TestCase):
    def setUp(self):
        pass
    @data([1,2,3])
    def test_1(self,value):
        print('test_1 value is ',value)
    '''
        打印的值是[1,2,3]
    '''

    @data
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值