DDT模块 Python DDT(data driven tests)模块心得

转自: https://www.cnblogs.com/frost-hit/p/8277637.html

Python DDT(data driven tests)模块心得

 

关于ddt模块的一些心得,主要是看官网的例子,加上一点自己的理解,官网地址:http://ddt.readthedocs.io/en/latest/example.html

ddt(data driven tests)可以让你给一个测试用例传入不同的参数,每个运行一遍,就像是运行了多个测试用例一样。

ddt模块包含了一个类的装饰器ddt和两个方法的装饰器:

data:包含多个你想要传给测试用例的参数;

file_data:会从json或yaml中加载数据;

通常data中包含的每一个值都会作为一个单独的参数传给测试方法,如果这些值是用元组或者列表传进来的,可以用unpack方法将其自动分解成多个参数。

ddt模块常用的就是这些方法,下面给一些例子和解释。例子里面是和unittest模块一起使用的,后续也会写一些unittest模块的介绍。

下面例子里所有的方法都是包含在class FooTestCase里的。

复制代码
import  unittest
from ddt import ddt, data,file_data,unpack

@ddt
class FooTestCase(unittest.TestCase):

    @data(1, 2)
    def test_show_value(self, value):
        print('value is %s' % value)
        print('case is over.\n')
复制代码

运行结果:

复制代码
value is 1
case is over.

value is 2
case is over.

###@data(1,2)里面的参数被单独的传给test_show_value()方法,使其运行两次。
复制代码

使用unpack方法将列表/元组分解的例子:

    @data((1, 2), (3, 4))
    @unpack
    def test_show_value(self, value1,value2):
        print('value is %s and %s' % (value1,value2))
        print('case is over.\n')

运行结果:

复制代码
value is 1 and 2
case is over.

value is 3 and 4
case is over.
###@data((1, 2), (3, 4))中传入的元组(1,2)和(3,4)被unpack方法分解,可以在函数中直接按照两个变量传入。列表也是一样的。
复制代码

使用unpack将字典分解的例子:

    @unpack
    @data({'first':1,'second':2},
          {'first':3,'second':4})
    def test_show_value(self, second, first):
        print ('value is %s and %s'%(first,second))
        print('case is over.\n')

运行结果:

复制代码
value is 1 and 2
case is over.

value is 3 and 4
case is over.
###@data({'first':1,'second':2},{'first':3,'second':4})传入的字典被unpack分解成了多个独立的key-value参数;test_show_value()方法里面使用的
###参数名字必须和字典的key值一致,否则会报unexpected keyword argument异常。
复制代码

ddt模块常用的就是这几个方法,更多的API细节可以到链接的网页查看。

关于ddt模块的一些心得,主要是看官网的例子,加上一点自己的理解,官网地址:http://ddt.readthedocs.io/en/latest/example.html

ddt(data driven tests)可以让你给一个测试用例传入不同的参数,每个运行一遍,就像是运行了多个测试用例一样。

ddt模块包含了一个类的装饰器ddt和两个方法的装饰器:

data:包含多个你想要传给测试用例的参数;

file_data:会从json或yaml中加载数据;

通常data中包含的每一个值都会作为一个单独的参数传给测试方法,如果这些值是用元组或者列表传进来的,可以用unpack方法将其自动分解成多个参数。

ddt模块常用的就是这些方法,下面给一些例子和解释。例子里面是和unittest模块一起使用的,后续也会写一些unittest模块的介绍。

下面例子里所有的方法都是包含在class FooTestCase里的。

复制代码
import  unittest
from ddt import ddt, data,file_data,unpack

@ddt
class FooTestCase(unittest.TestCase):

    @data(1, 2)
    def test_show_value(self, value):
        print('value is %s' % value)
        print('case is over.\n')
复制代码

运行结果:

复制代码
value is 1
case is over.

value is 2
case is over.

###@data(1,2)里面的参数被单独的传给test_show_value()方法,使其运行两次。
复制代码

使用unpack方法将列表/元组分解的例子:

    @data((1, 2), (3, 4))
    @unpack
    def test_show_value(self, value1,value2):
        print('value is %s and %s' % (value1,value2))
        print('case is over.\n')

运行结果:

复制代码
value is 1 and 2
case is over.

value is 3 and 4
case is over.
###@data((1, 2), (3, 4))中传入的元组(1,2)和(3,4)被unpack方法分解,可以在函数中直接按照两个变量传入。列表也是一样的。
复制代码

使用unpack将字典分解的例子:

    @unpack
    @data({'first':1,'second':2},
          {'first':3,'second':4})
    def test_show_value(self, second, first):
        print ('value is %s and %s'%(first,second))
        print('case is over.\n')

运行结果:

复制代码
value is 1 and 2
case is over.

value is 3 and 4
case is over.
###@data({'first':1,'second':2},{'first':3,'second':4})传入的字典被unpack分解成了多个独立的key-value参数;test_show_value()方法里面使用的
###参数名字必须和字典的key值一致,否则会报unexpected keyword argument异常。
复制代码

ddt模块常用的就是这几个方法,更多的API细节可以到链接的网页查看。

猜你喜欢

转载自www.cnblogs.com/cheese320/p/9212725.html
ddt