从0到1精通自动化,接口自动化测试——数据驱动DDT实战


前言

DDT简介

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

什么是数据驱动

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

1、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)

测试结果

test_int: 1
test_int: 2
test_int: 3
test_int: 4
test_str: 1
test_str: 2
test_str: 3

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

2、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)

测试结果

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)

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

当没有加unpack时,test_case方法的参数只能填一个;如元组的例子

当你加了unpack时,传递的数据量需要一致;如列表例子中,每个列表我都固定传了三个数据,当你多传或少传时会报错,而test_case方法的参数也要写三个,需要匹配上

当传的数据是字典类型时,要注意每个字典的key都要一致,test_case的参数的命名也要一致;如字典的例子,两个字典的key都是value1和value2,而方法的参数也是

当传的数据是通过变量的方式,如元组2、列表2,变量前需要加上*

3、DDT使用-传递 json文件

json文件

{
    
    
  "first": [
    {
    
    
      "isRememberMe": "True",
      "password": "111111",
      "username": "root"
    },
    "200"
  ],
  "second": [
    "{'isRememberMe': True, 'password': '1111111', 'username': 'root'}",
    "406"
  ],
  "third": [
    1,
    2
  ],
  "four": "123123"
}

单元测试类

from ddt import *


# 在测试类前必须首先声明使用 ddt
@ddt
class imoocTest(unittest.TestCase):

    @file_data('F:/test/config/testddt.json')
    def test_json(self, data):
        print(data)

测试结果

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

4、DDT使用-传递Yaml文件

yaml文件

unsorted_list:
  - 10
  - 15
  - 12

sorted_list: [ 15, 12, 50 ]

单元测试类

from ddt import *


# 在测试类前必须首先声明使用 ddt
@ddt
class imoocTest(unittest.TestCase):

    @file_data('F:/test/config/testddt.yaml')
    def test4(self, data):
        print("yaml", data)

测试结果

yaml [10, 15, 12]
yaml [15, 12, 50]
下面是我整理的2023年最全的软件测试工程师学习知识架构体系图

一、Python编程入门到精通

请添加图片描述

二、接口自动化项目实战

请添加图片描述

三、Web自动化项目实战

请添加图片描述

四、App自动化项目实战

请添加图片描述

五、一线大厂简历

请添加图片描述

六、测试开发DevOps体系

请添加图片描述

七、常用自动化测试工具

请添加图片描述

八、JMeter性能测试

请添加图片描述

九、总结(尾部小惊喜)

每一次的努力都不会白费,即使结果不是你想要的,也为你积累了经验和成长。所以,不要害怕失败,不要停止前进。只有持续奋斗,你才能不断超越自己,迎接更美好的未来!

只有不断努力,才能追逐自己的梦想;只有勇敢面对困难,才能站在人生巅峰;只有拼尽全力,才能收获辉煌的人生。所以,不要停下前进的步伐,一定要坚持奋斗,让未来更加美好!

生命中最大的挑战不是遇到困难,而是如何克服自己的惰性和恐惧去迎接挑战。只要你有决心和毅力,任何事情都可以变得可能。相信自己,坚定前行,你会看到成功的曙光。

猜你喜欢

转载自blog.csdn.net/csdnchengxi/article/details/131399976