• 接口自动化测试数据驱动DDT模块使用


    目录

    一、DDT简单介绍

    二、什么是数据驱动

    三、DDT基础使用:传递基础数据类型

    测试结果

    包含知识点

    四、DDT基础使用:传递一个复杂的数据结构 

    测试结果

    包含知识点

    五、DDT基础使用:传递json文件

    json文件

    单元测试类

    测试结果

    六、DDT基础使用:传递Yaml文件

    yaml文件

    单元测试类

    测试结果


    一、DDT简单介绍

    • 名称: Data-Driven Tests,数据驱动测试
    • 作用: 由外部数据集合来驱动测试用例的执行
    • 核心的思想:数据和测试代码分离
    • 应用场景: 一组外部数据来执行相同的操作
    • 优点: 当测试数据发生大量变化的情况下,测试代码可以保持不变
    • 实际项目: excel存储测试数据,ddt读取测试数据到单元测试框架(测试用例中),输出到html报告

    二、什么是数据驱动

    就是数据的改变从而驱动自动化测试的执行,最终引起测试结果的改变。说的直白些,就是参数化的应用

    三、DDT基础使用:传递基础数据类型

    1. # 导入ddt库下所有内容
    2. from ddt import *
    3. # 在测试类前必须首先声明使用 ddt
    4. @ddt
    5. class imoocTest(unittest.TestCase):
    6. # int
    7. @data(1, 2, 3, 4)
    8. def test_int(self, i):
    9. print("test_int:", i)
    10. # str
    11. @data("1", "2", "3")
    12. def test_str(self, str):
    13. print("test_str:", str)

    测试结果

    1. test_int: 1
    2. test_int: 2
    3. test_int: 3
    4. test_int: 4
    5. test_str: 1
    6. test_str: 2
    7. test_str: 3

    包含知识点

    想使用DDT首先要在单元测试类上面加上 @ddt 

    四、DDT基础使用:传递一个复杂的数据结构 

    1. from ddt import *
    2. # 在测试类前必须首先声明使用 ddt
    3. @ddt
    4. class imoocTest(unittest.TestCase):
    5. tuples = ((1, 2, 3), (1, 2, 3))
    6. lists = [[1, 2, 3], [1, 2, 3]]
    7. # 元组
    8. @data((1, 2, 3), (1, 2, 3))
    9. def test_tuple(self, n):
    10. print("test_tuple", n)
    11. # 列表
    12. @data([1, 2, 3], [1, 2, 3])
    13. @unpack
    14. def test_list(self, n1, n2, n3):
    15. print("test_list", n1, n2, n3)
    16. # 元组2
    17. @data(*tuples)
    18. def test_tuples(self, n):
    19. print("test_tuples", n)
    20. # 列表2
    21. @data(*lists)
    22. @unpack
    23. def test_lists(self, n1, n2, n3):
    24. print("test_lists", n1, n2, n3)
    25. # 字典
    26. @data({'value1': 1, 'value2': 2}, {'value1': 1, 'value2': 2})
    27. @unpack
    28. def test_dict(self, value1, value2):
    29. print("test_dict", value1, value2)

    测试结果

    1. test_dict 1 2
    2. test_dict 1 2
    3. test_list 1 2 3
    4. test_list 1 2 3
    5. test_lists 1 2 3
    6. test_lists 1 2 3
    7. test_tuple (1, 2, 3)
    8. test_tuple (1, 2, 3)
    9. test_tuples (1, 2, 3)
    10. test_tuples (1, 2, 3)

    包含知识点

    • @unpack :当传递的是复杂的数据结构时使用。比如使用元组或者列表,添加 @unpack 之后, ddt 会自动把元组或者列表对应到多个参数上。字典也可以这样处理
    • 当没有加unpack时,test_case方法的参数只能填一个;如元组的例子
    • 当你加了unpack时,传递的数据量需要一致;如列表例子中,每个列表我都固定传了三个数据,当你多传或少传时会报错,而test_case方法的参数也要写三个,需要匹配上
    • 当传的数据是字典类型时,要注意每个字典的key都要一致,test_case的参数的命名也要一致;如字典的例子,两个字典的key都是value1和value2,而方法的参数也是
    • 当传的数据是通过变量的方式,如元组2、列表2,变量前需要加上*

    五、DDT基础使用:传递json文件

    json文件

    1. {
    2. "first": [
    3. {
    4. "isRememberMe": "True",
    5. "password": "111111",
    6. "username": "root"
    7. },
    8. "200"
    9. ],
    10. "second": [
    11. "{'isRememberMe': True, 'password': '1111111', 'username': 'root'}",
    12. "406"
    13. ],
    14. "third": [
    15. 1,
    16. 2
    17. ],
    18. "four": "123123"
    19. }

    单元测试类

    1. from ddt import *
    2. # 在测试类前必须首先声明使用 ddt
    3. @ddt
    4. class imoocTest(unittest.TestCase):
    5. @file_data('F:/test/config/testddt.json')
    6. def test_json(self, data):
    7. print(data)

    测试结果

    1. [{'isRememberMe': 'True', 'password': '111111', 'username': 'root'}, '200']
    2. ["{'isRememberMe': True, 'password': '1111111', 'username': 'root'}", '406']
    3. [1, 2]
    4. 123123

    六、DDT基础使用:传递Yaml文件

    yaml文件

    1. unsorted_list:
    2. - 10
    3. - 15
    4. - 12
    5. sorted_list: [ 15, 12, 50 ]

    单元测试类

    1. from ddt import *
    2. # 在测试类前必须首先声明使用 ddt
    3. @ddt
    4. class imoocTest(unittest.TestCase):
    5. @file_data('F:/test/config/testddt.yaml')
    6. def test4(self, data):
    7. print("yaml", data)

    测试结果

    1. yaml [10, 15, 12]
    2. yaml [15, 12, 50]


     

  • 相关阅读:
    【深度学习】Transformer梳理
    【Java经典小游戏】大鱼吃小鱼 (两万字保姆级教程)
    c语言-浅谈指针(2)
    光伏电池建模及温度光照的影响曲线MATLAB仿真
    浅谈 synchronized 锁机制原理 与 Lock 锁机制
    zaabix实现对nginx监控
    Java访问修饰符private、default、protected、public的区别
    分组交换技术
    docker 上传镜像
    React入门笔记(二)
  • 原文地址:https://blog.csdn.net/jj2772367224/article/details/125491770