自动化之参数化

参数化介绍

  • 定义:是指利用不同的测试数据来测试相同的场景,为了提高代码的重用性,增加代码效率而采用一种代码编写的方法,叫参数化,也就是数据驱动。
  • 核心思想:数据和测试代码的分离
  • 优点:当测试数据发生大量变化的情况下,测试代码可以保持不变;

DDT应用

  • ddt: data driven testing,数据驱动,简单来说就是测试数据的参数化。
  • ddt是第三方库,可以通过 pip install ddt来下载安装。
  • ddt模块主要用到ddt,data,unpack,file_data。其中ddt.unpack:传递的是复杂的数据结构时使用,主要用于拆分数据。比如元组或者列表,添加unpack之后,ddt会自动把元组或者列表中的值拆分成多个值,依次传给多个参数接收。

json文件

[
  {
    "usr": "aaa",
    "pwd": "666666"
  },
  {
    "usr": "bbb",
    "pwd": "555555"
  }
]

yaml文件

- usr: aaa
  pwd: '666666'
- usr: bbb
  pwd: '555555'
import json
import unittest
import ddt

with open(r"./data.json", "r", encoding="utf-8") as f:
    data = json.load(f)
    print(data, type(data))

data1 = (1, 2, 3, 4, 5)
data2 = [1, 2, 3, 4, 5]
data3 = {"a": 1, "b": 2}
data4 = [(1, 2, 3), (6, 7, 8)]
print(*data4)


@ddt.ddt
class TestDemo(unittest.TestCase):
    @ddt.data(data1)  # data1 = (1,2,3,4,5)作为整体传给test_001的变量x
    def test_001(self, x):
        print("test_001", x)

    @ddt.data(*data1)  # 解包,将列表中的每个值传给方法的变量
    def test_002(self, x):
        print("test_002", x)

    @ddt.data(data2)  # 将data2作为中体传给方法的变量
    def test_003(self, x):
        print("test_003", x)

    @ddt.data(*data2)  # 解包,将列表中的每个值传给方法的变量
    def test_004(self, x):
        print("test_004", x)

    @ddt.data(data3)  # 将data3作为中体传给方法的变量
    def test_005(self, x):
        print("test_005", x)

    @ddt.data(data3)
    @ddt.unpack  # 字典数据类型如果要将value传到方法中,需要用unpack,方法的变量名必须为字典中的key
    def test_006(self, a, b):  # data3 = {"a":1,"b":2}  形参 必须用 a,b
        print("test_006", a, b)

    @ddt.data(*data4)  # 解包为(1,2,3),(6,7,8)
    @ddt.unpack
    def test_007(self, a, b, c):
        print("test_007", a, b, c)

    @ddt.unpack
    @ddt.data(*data)    #打开文件的操作在class上面得到的结果是[{'usr': 'aaa', 'pwd': '666666'}, {'usr': 'bbb', 'pwd': '555555'}]
    def test_008(self, usr, pwd):  #所以需要先解包得到{'usr': 'aaa', 'pwd': '666666'}, {'usr': 'bbb', 'pwd': '555555'}
        print("test_008", usr, pwd)  #然后在unpack得到每一个value,并且赋值给test_08

    @ddt.unpack
    @ddt.file_data("data1.yaml")   #可以用ddt.file_data直接读取yaml文件
    def test_009(self, usr, pwd):
        print("test_009", usr, pwd)

运行结果:

[{'usr': 'aaa', 'pwd': '666666'}, {'usr': 'bbb', 'pwd': '555555'}] <class 'list'>
(1, 2, 3) (6, 7, 8)
test_001 (1, 2, 3, 4, 5)
test_002 1
test_002 2
test_002 3
test_002 4
test_002 5
test_003 [1, 2, 3, 4, 5]
test_004 1
test_004 2
test_004 3
test_004 4
test_004 5
test_005 {'a': 1, 'b': 2}
test_006 1 2
test_007 1 2 3
test_007 6 7 8
test_008 aaa 666666
test_008 bbb 555555
test_009 aaa 666666
test_009 bbb 555555

注意:要搞清楚 * 和 unpack的区别,比如data=[ { "usr": "aaa", "pwd": "666666" }, { "usr": "bbb", "pwd": "555555" } ],*是用来解包,得到 { "usr": "aaa", "pwd": "666666" }和{ "usr": "bbb", "pwd": "555555" }即两组测试数据,而@ddt.unpack是将每个字典中的value拿出来作为实参传到被装饰的方法中,所以说声明的方法必须要有两个形参usr和pwd来接收,参数名只能是usr和pwd。

举例:如果对[1,2,3]进行解包即*[1,2,3]会得到三组测试数据,而unpack只有一组测试数据,需要3个参数接收。

parameterized

安装parameterized :pip install parameterized

--通过参数的方式来传递数据,从而实现数据和脚本分离,可以实现用例的重复执行,unittest本身不知处参数化,需要通过安装扩展插件parameterized来实现。

  • 导包:from parameterized import parameterized
  • 定义测试方法,将测试方法中的测试数据使用变量表示
  • 组织测试数据格式,[(),(),()],一个元组就是一组数据
  • 参数化,在测试方法上使用装饰器@parameterized.expend(测试数据)
  • 运行代码

 数据在json文件内:

# json文件 testdata.json
[
  {"a":1,"b":2,"expect":3},
  {"a":1,"b":3,"expect":4},
  {"a":1,"b":2,"expect":3},
  {"a":1,"b":2,"expect":5},
  {"a":2,"b":2,"expect":4}
]

 

#工具文件 util.py
import json
#加法方法
def add(a,b):
    return a+b
#读取json文件,转换成[(1,1,2),(),()]格式
def load_data():
    with open(r'testdata.json','r',encoding='utf-8') as f:
        data_list = json.load(f)  #读取文件并转成[{"k":"v"},{"k":"v"}]
        test_data = []
        for item in data_list:
            a = item.get('a')
            b = item.get('b')
            expect = item.get('expect')
            test_data.append((a,b,expect))  #(a,b,expect)=====>(1,2,3)
        return test_data
from parameterized import parameterized  #导入parameterized
import  unittest #导入unittest
from test_unittest.util import add, load_data #导入util中的方法


class TestAdd(unittest.TestCase):
    @parameterized.expand(load_data())
    def test_add(self,a,b,expect):
        self.assertEqual(expect,add(a,b))

suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestAdd))

runner = unittest.TextTestRunner()
runner.run(suite)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值