15. Python DDT数据驱动

什么是DDT
ddt是Python的第三方库。ddt模块提供了创建数据驱动的测试。

安装DDT:

PS D:\Python\Scripts> D:\Python\Scripts\pip.exe install ddt
Collecting ddt
  Downloading https://files.pythonhosted.org/packages/cf/f5/f83dea32dc3fb3be1e5afab8438dce73ed587740a2a061ae2ea56e04a36d/ddt-1.2.1-py2.py3-none-any.whl
Installing collected packages: ddt
Successfully installed ddt-1.2.1
PS D:\Python\Scripts>

新建待测类:

__author__ = 'Hello'
class PrintMsg:
    def fun_1(self,a):
        print("获取的参数是%s"%a)

    def fun_2(self,a,b):
        print("获取的第A个参数是%s"%a)
        print("获取的第B个参数是%s"%b)

    def fun_3(self,a,b,c):
        print("获取的第A个参数是%s"%a)
        print("获取的第B个参数是%s"%b)
        print("获取的第C个参数是%s"%c)

写一个测试类:

__author__ = 'Hello'
import unittest
from class_ddt.print_msg import PrintMsg

class TestPrintMsg(unittest.TestCase):

    def setUp(self):
        print("测试开始:")

    def test_fun_1(self):
        PrintMsg().fun_1("大土匪")

    def tearDown(self):
        print("测试结束!")

if __name__=="__main__":
    unittest.main

运行结果:

D:\Python\python.exe "D:\Work\Tools\JetBrains\PyCharm Community Edition 3.4.4\helpers\pycharm\utrunner.py" D:\Work\Tools\python_workspace\python_2017\class_ddt\test_print_msg.py::TestPrintMsg::test_fun_1 true
Testing started at 11:27 ...
D:\Work\Tools\JetBrains\PyCharm Community Edition 3.4.4\helpers\pycharm\utrunner.py:2: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp
测试开始:
获取的参数是大土匪
测试结束!

Process finished with exit code 0

我们发现,此时的单元测试成功了。但是我们在尝试修改测试数据的时候,需要在代码内进行每一次改动,很不方便。此时,我们引入一个东西:DDT。

我们的单元测试没法传递具体的测试数据,来检测我们的测试用例,DDT帮我们解决了这个问题。
有了DDT它会根据你传递进来的参数决定要生成几个测试用例。

__author__ = 'Hello'
import unittest
from class_ddt.print_msg import PrintMsg
from ddt import ddt,data,unpack     #引入装饰器

@ddt
class TestPrintMsg_2(unittest.TestCase):

    def setUp(self):
        print("测试开始!")

    @data("第一次","第二次","第三次")    #根据逗号进行拆分,数据条数决定执行次数
    def test_fun_1(self,a):     #参数化
        PrintMsg().fun_1(a)

    def tearDown(self):
        print("测试结束!")

if __name__ == "__main__":
    unittest.main

执行结果如下:

D:\Python\python.exe "D:\Work\Tools\JetBrains\PyCharm Community Edition 3.4.4\helpers\pycharm\utrunner.py" D:\Work\Tools\python_workspace\python_2017\class_ddt\test_print_msg_2.py::TestPrintMsg_2 true
Testing started at 11:03 ...
D:\Work\Tools\JetBrains\PyCharm Community Edition 3.4.4\helpers\pycharm\utrunner.py:2: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp
测试开始!
获取的参数是第一次
测试结束!
测试开始!
获取的参数是第二次
测试结束!
测试开始!
获取的参数是第三次
测试结束!

Process finished with exit code 0

传递参数的形式:
当只需要传递一个参数时:
1.

@data(1,2,3,4)   :根据逗号进行拆分,有几个数据,就执行几次测试,此处执行四次。    
@data([1,2],[3,4])  :执行两次。
@data({"name":"summer","age":"18"},{"name":"nick","age":"20"})    :执行2次。

注意:如果数不是以列表形式传递进来的话,传递进来的数据,根据逗号进行数据的拆分。
传递进来的数据可以是单个数据,多个子列表,也可以是多个子字典。
2. 数据是列表形式:

dict=[{"name":"summer","age":"18"},{"name":"nick","age":"20"}]
@data([1,2],[3,4])  :运行2次。
@data(dict) :运行一次。
@data(*dict)    :运行两次,此处通过*对数据进行拆分。
posted @ 2020-02-04 15:51  土匪哥的歌儿  阅读(85)  评论(0编辑  收藏  举报