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


    一、DDT简单介绍

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

    二、什么是数据驱动

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

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

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

    测试结果

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

    包含知识点

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

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

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

    测试结果

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

    包含知识点

    @unpack :当传递的是复杂的数据结构时使用。比如使用元组或者列表,添加 @unpack 之后, ddt 会自动把元组或者列表对应到多个参数上。字典也可以这样处理

    当没有加unpack时,test_case方法的参数只能填一个;如元组的例子
    当你加了unpack时,传递的数据量需要一致;如列表例子中,每个列表我都固定传了三个
    数据,当你多传或少传时会报错,而test_case方法的参数也要写三个,需要匹配上
    当传的数据是字典类型时,要注意每个字典的key都要一致,test_case的参数的命名也要一致;如字典的例子,两个字典的key都是value1和value2,而方法的参数也是
    当传的数据是通过变量的方式,如元组2、列表2,变量前需要加上*

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

    json文件

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

    单元测试类

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

    测试结果

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

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

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

    单元测试类

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

    测试结果

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

    最后感谢每一个认真阅读我文章的人,下面这个网盘链接也是我费了几天时间整理的非常全面的,希望也能帮助到有需要的你!

    在这里插入图片描述

    这些资料,对于想转行做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!凡事要趁早,特别是技术行业,一定要提升技术功底。希望对大家有所帮助……

    如果你不想一个人野蛮生长,找不到系统的资料,问题得不到帮助,坚持几天便放弃的感受的话,可以点击下方小卡片加入我们群,大家可以一起讨论交流,里面会有各种软件测试资料和技术交流。

    点击文末小卡片领取

    敲字不易,如果此文章对你有帮助的话,点个赞收个藏来个关注,给作者一个鼓励。也方便你下次能够快速查找。

    自学推荐B站视频:

    零基础转行软件测试:25天从零基础转行到入职软件测试岗,今天学完,明天就业。【包括功能/接口/自动化/python自动化测试/性能/测试开发】

    自动化测试进阶:2022B站首推超详细python自动化软件测试实战教程,备战金三银四跳槽季,进阶学完暴涨20K

  • 相关阅读:
    独立站定制开发,如何做Google广告引流
    SSM+好乐买超市管理系统 毕业设计-附源码111743
    [计算机网络实验]头歌 实验二 以太网帧、IP报文分析(含部分分析)
    免费好用的Mac电脑磁盘清理工具CleanMyMac
    FPGA - ZYNQ Cache一致性问题
    Android学习笔记 25. Activity与Fragment通信
    Linux常用指令(七)——任务计划
    大数据学习(2)Hadoop-分布式资源计算hive(1)
    【滤波跟踪】基于自适应UKF和UKF算法实现运动刚体的位姿估计附matlab代码
    verilog中$monitor 的用法
  • 原文地址:https://blog.csdn.net/weixin_57805858/article/details/125599979